Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#241, #242 Capture name of product and enum to schema #246

Merged
merged 19 commits into from
Aug 10, 2022

Conversation

sviezypan
Copy link
Contributor

@sviezypan sviezypan commented May 8, 2022

This PR implements features #241 and #242. However instead of adding name to all records and enums, TypeId was added so its possible to get fully qualified name like:

package zio.example {
  case class Person(age: Int)
  val personSchema = DeriveSchema.gen[Person]

  assertTrue(personSchema.id.fullyQualified == "zio.example.Person")
}

I am still missing macro schema generation for scala 3 which was recently added (scala 2 works fine)

In order to be able to convert from Schema.Record to DynamicValue.Record and back without loosing information TypeId was introduced also on DynamicValue.
Because of that, however, I don't know how to write Schema for DynamicValue.Record as Schema[DynamicValue] describes DynamicValue itself, not underlying untyped data structure. So even though for example DynamicValue is representation of
Address(street: String, number:Int, postalCode: String)
its Schema is a CaseClass1

Schema.CaseClass1[Map[String, DynamicValue], DynamicValue.Record](
        TypeId.parse("zio.scheema.DynamicValue.Record"),
        Schema.Field("values", Schema.defer(Schema.map(Schema.primitive[String], DynamicValueSchema()))),
        map => DynamicValue.Record(TypeId.Structural, ListMap(map.toSeq: _*)),
        record => record.values
      )

As you can see Schema provides a way how to construct data structure, which in this case is DynamicValue, however we don't know the name of data structure that DynamicValue represents (Address) - we just know how to construct it. The same applies also for enums.

Files in zio-schema-thrift/shared/src/test/scala-2/zio/schema/codec/generated/ were auto generated by new version of Thrift compiler 0.16.0 - no need to be reviewed.

@sviezypan sviezypan requested a review from a team as a code owner May 8, 2022 20:31
@jdegoes
Copy link
Member

jdegoes commented May 12, 2022

Thank you for your work on this! This is a highly sought-after addition to ZIO Schema.

So even though for example DynamicValue is representation of Address(street: String, number:Int, postalCode: String)
its Schema is a CaseClass1

I think you have to use a hidden field inside the map, which stores the typeId for the record.

You can see it would affect this:

        map => DynamicValue.Record(/* extract out type ID from map */, ListMap(map.toSeq: _*)),
        record => record.values ++ /* store record type id into map */

The build is still failing, can you please take a look? 🙏

override type Accessors[Lens[_, _], Prism[_, _], Traversal[_, _]] = (Lens[(A, B), A], Lens[(A, B), B])

val first = "_1"
val second = "_2"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am using these strings as singleton type parameters. If I inline them it fails to compile on 2.12

@sviezypan
Copy link
Contributor Author

hey @jdegoes, took me some time to resolve issues for scala 2.12 and 3 but its ready to review now. Please check also the comment I made about extracting strings that I use for singleton types.

Prism[some.type, Option[A], Some[A]] // ok
Prism["Some", Option[A], Some[A]] // fails on 2.12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants