Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/hnaderi/named-codec

Scala3 codec adapter that separates types and payloads
https://github.com/hnaderi/named-codec

adapter codec encoder-decoder messaging scala scala3

Last synced: 3 months ago
JSON representation

Scala3 codec adapter that separates types and payloads

Awesome Lists containing this project

README

        

# named-codec
Scala3 codec adapter that separates types and payloads
Cats friendly

[![named-codec-core Scala version support](https://index.scala-lang.org/hnaderi/named-codec/named-codec/latest.svg?style=flat-square)](https://index.scala-lang.org/hnaderi/named-codec/named-codec)
[![javadoc](https://javadoc.io/badge2/dev.hnaderi/named-codec-docs_3/scaladoc.svg?style=flat-square)](https://javadoc.io/doc/dev.hnaderi/named-codec-docs_3)
GitHub Workflow Status
GitHub
[![Scala Steward badge](https://img.shields.io/badge/Scala_Steward-helping-blue.svg?style=flat-square&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAQCAMAAAARSr4IAAAAVFBMVEUAAACHjojlOy5NWlrKzcYRKjGFjIbp293YycuLa3pYY2LSqql4f3pCUFTgSjNodYRmcXUsPD/NTTbjRS+2jomhgnzNc223cGvZS0HaSD0XLjbaSjElhIr+AAAAAXRSTlMAQObYZgAAAHlJREFUCNdNyosOwyAIhWHAQS1Vt7a77/3fcxxdmv0xwmckutAR1nkm4ggbyEcg/wWmlGLDAA3oL50xi6fk5ffZ3E2E3QfZDCcCN2YtbEWZt+Drc6u6rlqv7Uk0LdKqqr5rk2UCRXOk0vmQKGfc94nOJyQjouF9H/wCc9gECEYfONoAAAAASUVORK5CYII=)](https://scala-steward.org)

this is a tiny utility library that provides a simple codec adapter,
that helps with creating codec that encode/decode type name separately.

This is mostly helpful in messaging applications, where payload and message types are separated;
or for scenarios that you need to store a separate type name to enable type filtering.

### Usage

This library is currently available for Scala binary version 3.3 on both JVM, JS and native.

To use the latest version, include the following in your `build.sbt`:

```scala
libraryDependencies ++= Seq(
"dev.hnaderi" %% "named-codec" % "@VERSION@"
)

// or circe module directly

libraryDependencies ++= Seq(
"dev.hnaderi" %% "named-codec-circe" % "@VERSION@"
)
```

```scala
enum Data {
case A
case B(i: Int)
case C(s: String, i: Int)
}

import io.circe.generic.auto.*
import dev.hnaderi.namedcodec.*

val codec = CirceAdapter.of[Data]

codec.encode(Data.C("string", 101))

// EncodedMessage(name = "C", data = { "s": "string", i: 101 })
```