Skip to main content
Version: 2.x

Apache Thrift Codecs

Introductionโ€‹

Apache Thrift is an open-source framework that allows seamless communication and data sharing between different programming languages and platforms. In this section, we will explore how to derive Apache Thrift codecs from a ZIO Schema.

Installationโ€‹

To derive Apache Thrift codecs from a ZIO Schema, we need to add the following dependency to our build.sbt file:

libraryDependencies += "dev.zio" %% "zio-schema-thrift" % "1.7.5"

BinaryCodecโ€‹

The ThriftCodec object inside the zio.schema.codec package provides the thriftCodec operator which allows us to derive Protobuf codecs from a ZIO Schema:

object ThriftCodec {
implicit def thriftCodec[A](implicit schema: Schema[A]): BinaryCodec[A] = ???
}

Exampleโ€‹

Let's try an example:

import zio._
import zio.schema.codec._
import zio.schema.{DeriveSchema, Schema}

case class Person(name: String, age: Int)

object Person {
implicit val schema: Schema[Person] =
DeriveSchema.gen

implicit val thriftCodec: BinaryCodec[Person] =
ThriftCodec.thriftCodec(schema)
}

object Main extends ZIOAppDefault {
def run = for {
_ <- ZIO.debug("Apache Thrift Codec Example:")
person: Person = Person("John", 42)
encoded: Chunk[Byte] = Person.thriftCodec.encode(person)
_ <- ZIO.debug(s"person object encoded to Thrift's binary format: ${toHex(encoded)}")
decoded <- ZIO.fromEither(Person.thriftCodec.decode(encoded))
_ <- ZIO.debug(s"Thrift object decoded to Person class: $decoded")
} yield ()

def toHex(bytes: Chunk[Byte]): String =
bytes.map("%02x".format(_)).mkString(" ")
}

Here is the output of running the above program:

Apache Thrift Codec Example:
person object encoded to Thrift's binary format: 0b 00 01 00 00 00 04 4a 6f 68 6e 08 00 02 00 00 00 2a 00
Thrift object decoded to Person class: Person(John,42)

ZIO Chat

๐Ÿ”ฅ Ignite your ZIO journey with ZIO Chat! ๐Ÿ”ฅ Ready to unravel the intricacies of ZIO? As your trusty sidekick, I'm here to light the way. No matter if you're wandering through functional forests or traversing effectful landscapes, I've got answers to keep you on track. Your ZIO adventure starts here โ€“ ask me anything and let's explore together!