https://github.com/note/dhallj-magnolia
Dhallj Encoder and Decoder derivation using Magnolia
https://github.com/note/dhallj-magnolia
Last synced: 4 months ago
JSON representation
Dhallj Encoder and Decoder derivation using Magnolia
- Host: GitHub
- URL: https://github.com/note/dhallj-magnolia
- Owner: note
- License: mit
- Created: 2020-11-13T16:28:02.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-08-13T00:41:09.000Z (almost 2 years ago)
- Last Synced: 2025-06-11T02:49:57.856Z (about 1 year ago)
- Language: Scala
- Size: 160 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 16
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# dhallj-magnolia
This library provides generic derivation of [dhallj](https://github.com/travisbrown/dhallj) `Encoder` and `Decoder` typeclasses using [magnolia](https://github.com/softwaremill/magnolia).
To use dhallj-magnolia add the following dependency to your `build.sbt`:
```
libraryDependencies += "pl.msitko" %% "dhallj-magnolia" % "0.1.3"
```
## Limitations of Scala 3 version of the library
Dhallj-magnolia is published for both Scala 2.13 and Scala 3. Limitations of Scala 3 branch of the library:
* Your project consuming dhallj-magnolia has to have the compilation flag `-Yretain-trees` enabled if you rely on default values for case classes
* Derivation doesn't work for value classes
## Examples
Consider the following ADT:
```scala
sealed trait Bar
final case class Bar1(a: Int) extends Bar
final case class Bar2(b: String) extends Bar
final case class Foo(a: Int, b: String, bar: Bar)
```
### Automatic Encoder derivation
```scala
import org.dhallj.codec.syntax._
import pl.msitko.dhallj.generic.encoder.auto._
val input = Foo(12, "abc", Bar2("abcd"))
input.asExpr.toString
// """{a = 12, b = "abc", bar = (.Bar2) {b = "abcd"}}"""
```
Additionally, dhallj's `Encoder[A]` has `dhallType` method which prints out dhall type for `A`. So, following the example, we can:
```scala
import org.dhallj.codec.Encoder
println(Encoder[Foo].dhallType(None, None))
// {a : Natural, b : Text, bar : }
```
### Automatic Decoder derivation
```scala
import org.dhallj.syntax._
import pl.msitko.dhallj.generic.decoder.auto._
val input = """{a = 12, b = "abc", bar = (.Bar2) {b = "abcd"}}"""
for {
parsed <- input.parseExpr
decoded <- parsed.normalize().as[Foo]
} yield decoded
// Right(Foo(12,abc,Bar2(abcd)))
```
### Semi-automatic Encoder derivation
```scala
import org.dhallj.codec.syntax._
import pl.msitko.dhallj.generic.encoder.semiauto._
implicit val barEncoder = deriveEncoder[Bar]
implicit val fooEncoder = deriveEncoder[Foo]
val input = Foo(12, "abc", Bar2("abcd"))
input.asExpr.toString
// """{a = 12, b = "abc", bar = (.Bar2) {b = "abcd"}}"""
```
### Semi-automatic Decoder derivation
```scala
import org.dhallj.syntax._
import pl.msitko.dhallj.generic.decoder.semiauto._
implicit val barDecoder = deriveDecoder[Bar]
implicit val fooDecoder = deriveDecoder[Foo]
val input = """{a = 12, b = "abc", bar = (.Bar2) {b = "abcd"}}"""
for {
parsed <- input.parseExpr
decoded <- parsed.normalize().as[Foo]
} yield decoded
// Right(Foo(12,abc,Bar2(abcd)))
```