Skip to content

Instantly share code, notes, and snippets.

View calvinlfer's full-sized avatar

Calvin Lee Fernandes calvinlfer

View GitHub Profile
@calvinlfer
calvinlfer / Http4sExampleEndpoint.scala
Last active July 15, 2025 18:59
HTTP4S customizable Request/Response Logger Middleware
import org.http4s.*
import org.http4s.dsl.*
import zio.{durationInt as _, *}
import zio.interop.catz.*
import com.comcast.ip4s.*
import fs2.*
import org.http4s.ember.server.EmberServerBuilder
import fs2.io.net.Network
val example =
@calvinlfer
calvinlfer / JsonSchemaDeserializer.scala
Created July 11, 2025 18:33
Circe JsonSchema + Confluent Schema Registry based Serdes for Scala
import cats.effect.Resource
import com.fasterxml.jackson.databind.JsonNode
import fs2.kafka.*
import io.circe.Decoder
import io.circe.jackson.jacksonToCirce
import io.confluent.kafka.schemaregistry.client.{CachedSchemaRegistryClient, SchemaRegistryClient}
import io.confluent.kafka.serializers.json.KafkaJsonSchemaDeserializer
import io.confluent.kafka.serializers.json.KafkaJsonSchemaDeserializerConfig.*
import zio.*
import zio.interop.catz.*
@calvinlfer
calvinlfer / ZioHttpPlayground.scala
Last active June 28, 2025 14:00
ZIO HTTP Websocket example: simplified chat room
//> using dep dev.zio::zio:2.1.19
//> using dep dev.zio::zio-interop-cats::23.1.0.5
//> using dep dev.zio::zio-http:3.3.3
import zio.http.*
import zio.*
import zio.stream.*
import zio.http.WebSocketFrame.*
final class ChatRoom(in: Queue[WebSocketFrame], out: Hub[WebSocketChannelEvent]):
@calvinlfer
calvinlfer / Playground.scala
Last active February 21, 2025 17:45
Ship ZIO Metrics to OpenTelemetry (OLTP gRPC) using zio-telemetry-opentelemetry by manually providing the instrumentation (this portion connects ZIO Metrics to the ZIO Opentelemetry machinery). I also have an example that uses auto-instrumentation
import zio.*
import zio.telemetry.opentelemetry.OpenTelemetry
import io.opentelemetry.sdk.OpenTelemetrySdk
import io.opentelemetry.api.metrics.MeterProvider
import io.opentelemetry.sdk.metrics.SdkMeterProvider
import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporter
import io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporter
import io.opentelemetry.sdk.metrics.`export`.PeriodicMetricReader
import io.opentelemetry.sdk.resources.Resource as OltpResource
import zio.metrics.Metric
@calvinlfer
calvinlfer / partition.scala
Created January 28, 2025 23:28 — forked from soujiro32167/partition.scala
A generic partitioning of a list of enums
//> using scala 3.6.3
import scala.deriving.Mirror
import scala.compiletime.{erasedValue}
/** Type-level function that transforms a type-level tuple of types
* into a corresponding tuple of List[...] at the value level.
*
* E.g., if Ts is (T1, T2, T3), Partitioned[Ts] is (List[T1], List[T2], List[T3]).
*/
@calvinlfer
calvinlfer / HelidonExample.scala
Created December 11, 2024 20:50
Integrating the Helidon Kafka client with FS2 Streams using the interop flow package
import cats.syntax.parallel.*
import cats.effect.*
import fs2.*
import fs2.interop.flow.*
import io.helidon.config.Config
import io.helidon.messaging.connectors.kafka.KafkaConfigBuilder.AutoOffsetReset
import io.helidon.messaging.connectors.kafka.{KafkaConnector as HelidonKafkaConnector, KafkaMessage, KafkaPublisher as HelidonKafkaConsumer}
import org.apache.kafka.common.serialization.StringDeserializer
import org.reactivestreams.FlowAdapters
@calvinlfer
calvinlfer / example.sh
Created November 20, 2024 17:58
SBT on Alpine Linux using Coursier
apk add --update openjdk21 curl
# Use JAR based launcher: https://get-coursier.io/docs/cli-installation#linux--macos
curl -fLo coursier https://github.com/coursier/launchers/raw/master/coursier
chmod +x coursier
# Install SBT
./coursier install sbt
# https://get-coursier.io/docs/cli-launch#java-options
@calvinlfer
calvinlfer / InterruptibleReadLineExample.scala
Created September 27, 2024 20:14
An example of how to achieve an interruptible readLine with ZIO
import zio.*
import scala.Console as SConsole
import scala.io.StdIn
import java.io.{BufferedReader, IOException}
import scala.util.Try
object InterruptibleReadLineExample extends ZIOAppDefault {
def altReadLine(reader: BufferedReader = SConsole.in) =
ZIO
@calvinlfer
calvinlfer / KyoPlayground.scala
Created September 21, 2024 23:26
Modelling lazy infinite streams in Kyo 0.12.x (https://getkyo.io/#/?id=stream-composable-data-processing)
import kyo.*
import kyo.Emit.Ack
object Playground extends KyoApp:
def recursiveStream(start: Int): Stream[Int, IO] =
def go(n: Int): Ack < Emit[Chunk[Int]] =
Emit.andMap(Chunk(n)):
case Ack.Stop => Ack.Stop
case Ack.Continue(_) => go(n + 1)
@calvinlfer
calvinlfer / Avro4sDeserializer.scala
Created June 8, 2024 17:37
FS2 Kafka Avro4S support (Schema Registry aware)
import com.sksamuel.avro4s.{Decoder, SchemaFor}
import fs2.kafka.{Deserializer, ValueDeserializer, KeyDeserializer}
import fs2.kafka.vulcan.AvroSettings
import cats.effect.{Sync, Resource}
import java.nio.ByteBuffer
import io.confluent.kafka.schemaregistry.avro.AvroSchema
final class Avro4sDeserializer[A >: Null](
private val decoder: Decoder[A],
private val schemaFor: SchemaFor[A]