Skip to content

Commit

Permalink
wip.
Browse files Browse the repository at this point in the history
  • Loading branch information
khajavi committed Jul 4, 2023
1 parent c389f5b commit c1a8d35
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 96 deletions.
42 changes: 42 additions & 0 deletions docs/combining-different-encoders.md
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
}
```
90 changes: 7 additions & 83 deletions docs/our-first-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ id: our-first-schema
title: "Our First Schema"
---

ZIO Schema provides macros to help you create `Schema`s out of your data types. But before using the macros,
we should take a look at how to do this the manual way.
ZIO Schema provides macros to help you create `Schema`s out of our data types. But before using the macros, we should take a look at how to do this the manual way.

### The Domain
## The Domain

Like in [Overview](index.md), we define our example domain like this:
As described in the [Overview](index.md) section, we define the example domain as follows:

```scala
object Domain {
Expand All @@ -28,11 +27,9 @@ object Domain {

### Manual construction of a Schema

This part is similar to other libraries that you might know, e.g. for JSON processing.
Basically, you create a `Schema` for every data type in your domain:
This part is similar to other libraries that we might know, e.g. for JSON processing. Basically, we create a `Schema` for every data type in our domain:

```scala

object ManualConstruction {
import zio.schema.Schema._
import Domain._
Expand Down Expand Up @@ -85,7 +82,6 @@ object ManualConstruction {
extractField2 = c => c.paymentMethod
)
}

```

### Macro derivation
Expand All @@ -104,11 +100,9 @@ object MacroConstruction {
}
```

## Applying it to our domain

### Json example
## Applying it to Our Domain

Lets put this all together in a small sample:
Let's put this all together in a small sample:

```scala
object JsonSample extends zio.App {
Expand All @@ -129,77 +123,7 @@ object JsonSample extends zio.App {
```

When we run this, we get our expected result printed out:

```json
{"name":"Michelle","age":32}
```

### 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
}
```


### Combining different encoders

Let's take a look at a roundtrip 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 roundtrip.

```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
}
```
31 changes: 31 additions & 0 deletions docs/protobuf-example.md
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
}
```
4 changes: 3 additions & 1 deletion docs/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ const sidebars = {
"motivation",
"getting-started",
"transforming-schemas",
"codecs"
"codecs",
"protobuf-example",
"combining-different-encoders"
]
}
]
Expand Down
24 changes: 12 additions & 12 deletions docs/use-cases.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ title: "ZIO Schema Use cases"
sidebar_label: "Use cases"
---

ZIO Schema allows you to create representations of your data types as values.
ZIO Schema allows us to create representations of our data types as values.

Once you have a representation of your data types, you can use it to
- serialize and deserialize your types
- validate your types
- transform your types
- create instances of your types
Once we have a representation of our data types, we can use it to
- Serialize and deserialize our types
- Validate our types
- Transform our types
- Create instances of your types

You can then use one of the various codecs (or create your own) to serialize and deserialize your types.
We can then use one of the various codecs (or create our own) to serialize and deserialize your types.

Example of possible codecs are:

Expand All @@ -39,8 +39,8 @@ Example use cases that are possible:
- Creating diffs from arbitrary data structures
- Creating migrations / evolutions e.g. of Events used in Event-Sourcing
- Transformation pipelines, e.g.
1. convert from protobuf to object, e.g. `PersonDTO`,
2. transform to another representation, e.g. `Person`,
3. validate
4. transform to JSON `JsonObject`
5. serialize to `String`
1. Convert from protobuf to object, e.g. `PersonDTO`,
2. Transform to another representation, e.g. `Person`,
3. Validate
4. Transform to JSON `JsonObject`
5. Serialize to `String`

0 comments on commit c1a8d35

Please sign in to comment.