Last active
May 25, 2024 10:20
-
-
Save dacr/e6425a4dba3d5106ebfa33f80720fb98 to your computer and use it in GitHub Desktop.
ZIO learning - learning&testing zio-schemas features / published by https://github.com/dacr/code-examples-manager #735e9391-0449-4f38-9d67-f4f86105a395/5ef3dbb8b68acc471edd984481dadfd3a3dc9403
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// summary : ZIO learning - learning&testing zio-schemas features | |
// keywords : scala, zio, learning, schema, pure-functional, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 735e9391-0449-4f38-9d67-f4f86105a395 | |
// created-on : 2022-11-25T15:46:03+01:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.4.2" | |
//> using dep "dev.zio::zio:2.0.13" | |
//> using dep "dev.zio::zio-schema:0.4.11" | |
//> using dep "dev.zio::zio-schema-derivation:0.4.11" | |
//> using dep "dev.zio::zio-schema-protobuf:0.4.11" | |
//> using dep "dev.zio::zio-schema-json:0.4.11" | |
// --------------------- | |
import zio.* | |
import zio.ZIO.* | |
import zio.schema.{DeriveSchema, Schema} | |
import zio.schema.codec.JsonCodec.* | |
import zio.schema.syntax.* | |
//import zio.prelude.* | |
import java.time.Instant | |
import java.nio.charset.Charset | |
case class Address(town: String, country: String, planet: Option[String]) | |
case class Person(name: String, age: Int, birthDate: Instant, addresses: List[Address]) | |
object Person { | |
given schema: Schema[Person] = DeriveSchema.gen | |
} | |
object Encapsulated extends ZIOAppDefault { | |
val json = | |
"""{ | |
| "name":"joe", | |
| "age":42, | |
| "birthDate":"2021-04-09T17:19:17.000Z", | |
| "addresses":[ {"town":"london","country":"uk"} ] | |
|}""".stripMargin | |
def run = | |
for { | |
_ <- logInfo(s"experimenting...") | |
decoder = jsonDecoder[Person](Person.schema) | |
person <- from(decoder.decodeJson(json)) | |
_ <- Console.printLine(person.toString) | |
} yield () | |
} | |
Encapsulated.main(Array.empty) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment