-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revamping Documentation: Enhancing, Restructuring and Covering More T…
…opics (#581) * refactor docs. * add manual and automatic derivation. * integration with zio streams. * add more packages to installation section. * add comment for fail constructor. * primitives. * transforming schemas. * sequence section. * map collection. * schema for set collection. * record section. * add case class section. * refactor. * generic record. * either. * enumeration. * chunk and vector. * fail. * tuples. * update readme. * refactor sidebars. * add more resources for zio schema. * getting the default value of a schema. * sidebar label for operations. * reorder sidebar items. * generate ordering for schemas. * fix mdoc errors. * diffing and patching. * automatic migration. * schema serialization. * fix typo. * add more resources. * mapping dto to domain object. * example for Schema#migrate method. * improve sentences. * add nuttycombe talk to resource section. * dynamic data representation. * dynamic value migration. * schema migration. * derive ordering. * separate article for getting the default value. * separate article for diffing and patching. * separate other articles. * validation section. * update dynamic data representation article. * reified optics. * avro codecs. * add bson codecs. * improve apache avro doc. * add zio-schema-bson to doc's dependencies. * add json codec article. * add message pack section. * add protobuf section. * update message pack. * add apache thrift section to codecs. * refactor. * refactor. * organize sidebar. * overview of all operations. * update introduction section. * update readme. * update readme. * update readme.
- Loading branch information
Showing
33 changed files
with
2,782 additions
and
633 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
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,91 @@ | ||
--- | ||
id: automatic-schema-derivation | ||
title: "Automatic Schema Derivation" | ||
--- | ||
|
||
Automatic schema derivation is the process of generating schema definitions for data types automatically, without the need to manually write them. It allows us to generate the schema for a data type based on its structure and annotations. | ||
|
||
Instead of manually specifying the schema for each data type, we can rely on automatic schema derivation to generate the schema for us. This approach can save time and reduce the potential for errors, especially when dealing with complex data models. | ||
|
||
By leveraging reflection and type introspection using macros, automatic schema derivation analyzes the structure of the data type and its fields, including their names, types, and annotations. It then generates the corresponding schema definition based on this analysis. | ||
|
||
ZIO streamlines schema derivation through its `zio-schema-derivation` package, which utilizes the capabilities of Scala macros to automatically derive schemas. In order to use automatic schema derivation, we neeed to add the following line to our `build.sbt` file: | ||
|
||
```scala | ||
libraryDependencies += "dev.zio" %% "zio-schema-derivation" % @VERSION@ | ||
``` | ||
|
||
Once again, let's revisit our domain models: | ||
|
||
```scala mdoc:compile-only | ||
final case class Person(name: String, age: Int) | ||
|
||
sealed trait PaymentMethod | ||
|
||
object PaymentMethod { | ||
final case class CreditCard(number: String, expirationMonth: Int, expirationYear: Int) extends PaymentMethod | ||
final case class WireTransfer(accountNumber: String, bankCode: String) extends PaymentMethod | ||
} | ||
|
||
final case class Customer(person: Person, paymentMethod: PaymentMethod) | ||
``` | ||
|
||
We can easily use auto derivation to create schemas: | ||
|
||
```scala | ||
import zio.schema._ | ||
import zio.schema.codec._ | ||
|
||
final case class Person(name: String, age: Int) | ||
|
||
object Person { | ||
implicit val schema: Schema[Person] = DeriveSchema.gen[Person] | ||
} | ||
|
||
sealed trait PaymentMethod | ||
|
||
object PaymentMethod { | ||
|
||
implicit val schema: Schema[PaymentMethod] = | ||
DeriveSchema.gen[PaymentMethod] | ||
|
||
final case class CreditCard( | ||
number: String, | ||
expirationMonth: Int, | ||
expirationYear: Int | ||
) extends PaymentMethod | ||
|
||
final case class WireTransfer(accountNumber: String, bankCode: String) | ||
extends PaymentMethod | ||
} | ||
|
||
final case class Customer(person: Person, paymentMethod: PaymentMethod) | ||
|
||
object Customer { | ||
implicit val schema: Schema[Customer] = DeriveSchema.gen[Customer] | ||
} | ||
``` | ||
|
||
Now we can write an example that demonstrates a roundtrip test for protobuf codecs: | ||
|
||
```scala | ||
// Create a customer instance | ||
val customer = | ||
Customer( | ||
person = Person("John Doe", 42), | ||
paymentMethod = PaymentMethod.CreditCard("1000100010001000", 6, 2024) | ||
) | ||
|
||
// Create binary codec from customer | ||
val customerCodec: BinaryCodec[Customer] = | ||
ProtobufCodec.protobufCodec[Customer] | ||
|
||
// Encode the customer object | ||
val encodedCustomer: Chunk[Byte] = customerCodec.encode(customer) | ||
|
||
// Decode the byte array back to the person instance | ||
val decodedCustomer: Either[DecodeError, Customer] = | ||
customerCodec.decode(encodedCustomer) | ||
|
||
assert(Right(customer) == decodedCustomer) | ||
``` |
Oops, something went wrong.