-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
95 additions
and
96 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
id: combining-different-encoders | ||
title: "Combining Different Encoders" | ||
--- | ||
|
||
Let's take a look at a round-trip converting an object to JSON and back, then converting it to a protobuf and back. This is a simple example, but it shows how to combine different encoders to achieve a round-trip. | ||
|
||
```scala | ||
object CombiningExample extends zio.App { | ||
import zio.schema.codec.JsonCodec | ||
import zio.schema.codec.ProtobufCodec | ||
import ManualConstruction._ | ||
import zio.stream.ZStream | ||
|
||
override def run(args: List[String]): UIO[ExitCode] = for { | ||
_ <- ZIO.unit | ||
_ <- ZIO.debug("combining roundtrip") | ||
person = Person("Michelle", 32) | ||
|
||
personToJson = JsonCodec.encoder[Person](schemaPerson) | ||
jsonToPerson = JsonCodec.decoder[Person](schemaPerson) | ||
|
||
personToProto = ProtobufCodec.encoder[Person](schemaPerson) | ||
protoToPerson = ProtobufCodec.decoder[Person](schemaPerson) | ||
|
||
newPerson <- ZStream(person) | ||
.tap(v => ZIO.debug("input object is: " + v)) | ||
.transduce(personToJson) | ||
.transduce(jsonToPerson) | ||
.tap(v => ZIO.debug("object after json roundtrip: " + v)) | ||
.transduce(personToProto) | ||
.transduce(protoToPerson) | ||
.tap(v => ZIO.debug("person after protobuf roundtrip: " + v)) | ||
.runHead | ||
.some | ||
.catchAll(error => ZIO.debug(error)) | ||
_ <- ZIO.debug("is old person the new person? " + (person == newPerson).toString) | ||
_ <- ZIO.debug("old person: " + person) | ||
_ <- ZIO.debug("new person: " + newPerson) | ||
} yield ExitCode.success | ||
} | ||
``` |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--- | ||
id: protobuf-example | ||
title: "Protobuf Example" | ||
--- | ||
|
||
```scala | ||
object ProtobufExample extends zio.App { | ||
import zio.schema.codec.ProtobufCodec | ||
import ManualConstruction._ | ||
import zio.stream.ZStream | ||
|
||
override def run(args: List[String]): UIO[ExitCode] = for { | ||
_ <- ZIO.unit | ||
_ <- ZIO.debug("protobuf roundtrip") | ||
person = Person("Michelle", 32) | ||
|
||
personToProto = ProtobufCodec.encoder[Person](schemaPerson) | ||
protoToPerson = ProtobufCodec.decoder[Person](schemaPerson) | ||
|
||
newPerson <- ZStream(person) | ||
.transduce(personToProto) | ||
.transduce(protoToPerson) | ||
.runHead | ||
.some | ||
.catchAll(error => ZIO.debug(error)) | ||
_ <- ZIO.debug("is old person the new person? " + (person == newPerson).toString) | ||
_ <- ZIO.debug("old person: " + person) | ||
_ <- ZIO.debug("new person: " + newPerson) | ||
} yield ExitCode.success | ||
} | ||
``` |
This file contains 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
This file contains 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