Skip to content

Commit

Permalink
fix: handle missing collection fields in JSON decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
onyedikachi-david committed Jun 8, 2024
1 parent c80948f commit 71cb585
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dev.zio.schema.example.example1
import zio._
import zio.schema.{ DeriveSchema, Schema, TypeId }
import zio.stream.ZPipeline
import zio.schema.codec.JsonCodec

/**
* Example 1 of ZIO-Schema:
Expand Down Expand Up @@ -129,6 +130,7 @@ object MacroConstruction {

implicit val schemaPerson: Schema[Person] = DeriveSchema.gen[Person]


val schemaPaymentMethod: Schema[PaymentMethod] = DeriveSchema.gen[PaymentMethod]

val schemaCustomer: Schema[Customer] = DeriveSchema.gen[Customer]
Expand All @@ -142,13 +144,15 @@ object JsonSample extends zio.ZIOAppDefault {
import ManualConstruction._
import zio.schema.codec.JsonCodec
import zio.stream.ZStream
implicit val defaultConfig: JsonCodec.Config = JsonCodec.Config.default


override def run: ZIO[Environment with ZIOAppArgs, Any, Any] =
for {
_ <- ZIO.unit
person = Person("Michelle", 32)
personToJsonTransducer = JsonCodec
.schemaBasedBinaryCodec[Person](schemaPerson)
.schemaBasedBinaryCodec[Person](schemaPerson, defaultConfig)
.streamEncoder
_ <- ZStream(person)
.via(personToJsonTransducer)
Expand Down Expand Up @@ -187,15 +191,18 @@ object CombiningExample extends ZIOAppDefault {
import ManualConstruction._
import zio.schema.codec.{ JsonCodec, ProtobufCodec }
import zio.stream.ZStream
implicit val defaultConfig: JsonCodec.Config = JsonCodec.Config.default



override def run: ZIO[Environment with ZIOAppArgs, Any, Any] =
for {
_ <- ZIO.unit
_ <- ZIO.debug("combining roundtrip")
person = Person("Michelle", 32)

personToJson = JsonCodec.schemaBasedBinaryCodec[Person](schemaPerson).streamEncoder
jsonToPerson = JsonCodec.schemaBasedBinaryCodec[Person](schemaPerson).streamDecoder
personToJson = JsonCodec.schemaBasedBinaryCodec[Person](schemaPerson, defaultConfig).streamEncoder
jsonToPerson = JsonCodec.schemaBasedBinaryCodec[Person](schemaPerson, defaultConfig).streamDecoder

personToProto = ProtobufCodec.protobufCodec[Person](schemaPerson).streamEncoder
protoToPerson = ProtobufCodec.protobufCodec[Person](schemaPerson).streamDecoder
Expand All @@ -222,19 +229,21 @@ object DictionaryExample extends ZIOAppDefault {
import MacroConstruction._
import zio.schema.codec.JsonCodec
import zio.stream.ZStream
implicit val defaultConfig: JsonCodec.Config = JsonCodec.Config.default

override def run: ZIO[Environment with ZIOAppArgs, Any, Any] =
for {
_ <- ZIO.unit
person = Person("Mike", 32)
dictionary = Map("m" -> person)
dictionaryToJson = JsonCodec
.schemaBasedBinaryCodec[scala.collection.immutable.Map[String, Person]](
schemaPersonDictionaryFromMacro
schemaPersonDictionaryFromMacro, defaultConfig
)
.streamEncoder
jsonToDictionary = JsonCodec
.schemaBasedBinaryCodec[scala.collection.immutable.Map[String, Person]](
schemaPersonDictionaryFromMacro
schemaPersonDictionaryFromMacro, defaultConfig
)
.streamDecoder
newPersonDictionary <- ZStream(dictionary)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,15 @@ import dev.zio.schema.example.example2.Domain._
object JsonSample extends zio.ZIOAppDefault {
import zio.schema.codec.JsonCodec
import zio.stream.ZStream
implicit val defaultConfig: JsonCodec.Config = JsonCodec.Config.default


override def run: ZIO[Environment with ZIOAppArgs, Any, Any] =
for {
_ <- ZIO.unit
person = Person("Michelle", 32)
personToJsonPipeline = JsonCodec
.schemaBasedBinaryCodec[Person](Person.schema)
.schemaBasedBinaryCodec[Person](Person.schema, defaultConfig)
.streamEncoder
_ <- ZStream(person)
.via(personToJsonPipeline)
Expand Down Expand Up @@ -196,15 +198,17 @@ object ProtobufExample extends ZIOAppDefault {
object CombiningExample extends zio.ZIOAppDefault {
import zio.schema.codec.{ JsonCodec, ProtobufCodec }
import zio.stream.ZStream
implicit val defaultConfig: JsonCodec.Config = JsonCodec.Config.default


override def run: ZIO[Environment with ZIOAppArgs, Any, Any] =
for {
_ <- ZIO.unit
_ <- ZIO.debug("combining roundtrip")
person = Person("Michelle", 32)

personToJson = JsonCodec.schemaBasedBinaryCodec[Person](Person.schema).streamEncoder
jsonToPerson = JsonCodec.schemaBasedBinaryCodec[Person](Person.schema).streamDecoder
personToJson = JsonCodec.schemaBasedBinaryCodec[Person](Person.schema, defaultConfig).streamEncoder
jsonToPerson = JsonCodec.schemaBasedBinaryCodec[Person](Person.schema, defaultConfig).streamDecoder

personToProto = ProtobufCodec.protobufCodec[Person](Person.schema).streamEncoder
protoToPerson = ProtobufCodec.protobufCodec[Person](Person.schema).streamDecoder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ private[example3] object Domain {
object Example3 extends ZIOAppDefault {
import dev.zio.schema.example.example3.Domain._
import zio.schema.codec.JsonCodec
implicit val defaultConfig: JsonCodec.Config = JsonCodec.Config.default


val personTransformation: Schema[Person] = PersonDTO.schema.transform[Person](
(dto: PersonDTO) => Person(dto.firstname + " " + dto.lastname, dto.years),
Expand All @@ -74,20 +76,20 @@ object Example3 extends ZIOAppDefault {

// get objects from JSON
personDTO <- ZIO.fromEither(
JsonCodec.schemaBasedBinaryCodec[PersonDTO](PersonDTO.schema).decode(chunks)
JsonCodec.schemaBasedBinaryCodec[PersonDTO](PersonDTO.schema, defaultConfig).decode(chunks)
)
person <- ZIO.fromEither(
JsonCodec.schemaBasedBinaryCodec[Person](personTransformation).decode(chunks)
JsonCodec.schemaBasedBinaryCodec[Person](personTransformation, defaultConfig).decode(chunks)
)
_ <- ZIO.debug("PersonDTO : " + personDTO)
_ <- ZIO.debug("Person : " + person)

// get JSON from Objects
personJson = new String(
JsonCodec.schemaBasedBinaryCodec[Person](Person.schema).encode(person).toArray
JsonCodec.schemaBasedBinaryCodec[Person](Person.schema, defaultConfig).encode(person).toArray
)
personDTOJson = new String(
JsonCodec.schemaBasedBinaryCodec[Person](personTransformation).encode(person).toArray
JsonCodec.schemaBasedBinaryCodec[Person](personTransformation, defaultConfig).encode(person).toArray
)
_ <- ZIO.debug("Person JSON: " + personJson)
_ <- ZIO.debug("PersonDTO JSON: " + personDTOJson)
Expand Down
Loading

0 comments on commit 71cb585

Please sign in to comment.