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

Provide direct support for Map #142

Merged
merged 5 commits into from
Nov 14, 2021
Merged

Conversation

pierangeloc
Copy link
Contributor

Resolves #138

@pierangeloc pierangeloc requested a review from a team as a code owner October 30, 2021 15:48
@CLAassistant
Copy link

CLAassistant commented Oct 30, 2021

CLA assistant check
All committers have signed the CLA.

@pierangeloc
Copy link
Contributor Author

@thinkharderdev While working on the Json serialization, and looking also at the Map as supported by DynamoDB (or by other formats such as Postgres hstore), it makes probably most sense to support Schema not for Map[K, V] with K and V having a Schema, but just for Map[String, V] with V having a Schema.
Should we apply this restriction?

@zalbia zalbia mentioned this pull request Nov 1, 2021
2 tasks
@thinkharderdev
Copy link
Contributor

@thinkharderdev While working on the Json serialization, and looking also at the Map as supported by DynamoDB (or by other formats such as Postgres hstore), it makes probably most sense to support Schema not for Map[K, V] with K and V having a Schema, but just for Map[String, V] with V having a Schema.
Should we apply this restriction?

I think we do want to support Map[K,V]. For JSON serialization (and also for other codecs) we would encode this as an array of tuples. So something like

case class Key(keyName: String, data: Int)
case class Value(value: String)

case class Foo(map: Map[Key,Value]

would get JSON encoded as

{"map":[{"left":{"keyName":"k","data":0},"right":{"value":"something"}}]

@pierangeloc pierangeloc force-pushed the support-map branch 3 times, most recently from 02fad4b to 580b3c9 Compare November 6, 2021 17:00
@pierangeloc
Copy link
Contributor Author

@thinkharderdev I implemented the codecs as you suggested, and added unit tests

@thinkharderdev
Copy link
Contributor

Looks great! I think you need to run Scalafmt again though to fix the listing errors.

Unit tests for json serdes
Unit tests for protobuf serdes
@pierangeloc
Copy link
Contributor Author

@thinkharderdev thanks, rebased and reformatted!

@thinkharderdev
Copy link
Contributor

@thinkharderdev thanks, rebased and reformatted!

Seems like there is another linter error

@pierangeloc
Copy link
Contributor Author

build.sbt was unformatted. I also added a small paragraph for contributors in the readme.md

@thinkharderdev
Copy link
Contributor

build.sbt was unformatted. I also added a small paragraph for contributors in the readme.md

Linter is still failing :). I think you want to run sbt prepare instead of sbt fmt. The latter will only run scalafmt while the former will run scalafix and scalafmt.

README.md Outdated
Comment on lines 50 to 52
sbt test

sbt fmt
Copy link
Contributor

Choose a reason for hiding this comment

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

I would reverse the order here and change sbt fmt to sbt prepare, so:

sbt prepare
sbt test

since on rare occassions scalafix can make changes that cause compilation to fail.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@thinkharderdev thanks, now the linter should be happy 🤞

Copy link
Contributor

@thinkharderdev thinkharderdev left a comment

Choose a reason for hiding this comment

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

Awesome work!

@thinkharderdev thinkharderdev merged commit b94ac75 into zio:main Nov 14, 2021
jdegoes pushed a commit that referenced this pull request Dec 14, 2021
* added basic documentation with example

* added more dependencies to docs

* incorporated feedback by @thinkharderdev

* fixed wording 'ZIO-Schema' -> 'ZIO Schema'

* fixed naming of other zio libraries

* fix sbt publish setting

* added examples subproject

* docs: fixed mdoc link and added version placeholder in overview/index.md

* docs: incorporated suggestions by @TobiasPfeifer

* scalafmt for build.sbt

* incorporated suggestions by @thinkharderdev

* Support for annotations on all types (#147)

* Add addonation to Schema[A] interface and add as field in Schema.Sequence

* Add annotation field to Schema.Transform

* Add annotation field to Schema.Primitive

* Add annotations field to Schema.Optional

* Add annotations field to Schema.Fail

* Add annotations field to Schema.Tuple

* Add annotations field to EitherSchema

* Add annotations field to Schema.Lazy

* Add annotations field to Schema.Meta

* Add remaining assertion equality checks to equalsSchema

* Remove annotations from Record[A] (since it exists in Schema[A] already) and add annotations field to Schema.GenericRecord

* Fix test case for schema generation on annotated ADT

* Fix handling of annotations for Lazy (and thus failing tests)

* Wherever we accept an annotations chunk in the constructor/method we default it to Chunk.empty

* Add `def annotate(annotation: Any): Schema[A]` to Schema

* Replace usage of Chunk.appended with :+ to fix 2.12.12 compilation

* Provide direct support for Map (#142)

* Provide direct support for Map
Unit tests for json serdes
Unit tests for protobuf serdes

* run scalafmt

* Rebased/reformatted

* Rebased/reformatted, and added a minimum contribution guide

* Run sbt `prepare test` instead of `fmt test`

* Add defaultValue to Schema  (#80)

* feat(schema): add first draft defaultValue implementation for Schema

* feat(schema): add missing test cases

* feat(schema): change default value for BoolType to false

* feat(schema): fold default values in Record type appropriately

* fix(zio-schema): fix failing test

* feat(zio-schema): add defaultValue implementation for MapSchema

* patch(zio-schema): remove orElse for Scala 2.12 compat

* first commit

* [zio-schema-examples] sbt setup

* [zio-schema-examples] readme init

* [zio-schema-examples] example 1

* [zio-schema-examples] example 2

* [zio-schema-examples] example 3 (WIP)

* [zio-schema-examples] example 3

* [zio-schema-examples] Example3: Don't share domain with other samples

* [zio-schema-examples] Example4 transforming DTOs

* [zio-schema-examples] Example5 diffing

* [zio-schema-examples] example 4: fixed map/flatmap in example

* [zio-schema-examples] example 4: another try to converting DTOs

* [zio-schema-examples] example 6 reified optics stub

* [zio-schema-examples] example 6 reified optics examples

* [zio-schema-examples] example 1 + 2: schema fixes for newest zio-schema

* [zio-schema-examples] Problem 7: Initial code

* [zio-schema-examples] Example 6: adding employee now works

* [zio-schema-examples] Example 7: added notes on performance requirements

* [zio-schema-examples] Example 7: added parameter list to methods, fixed names

* [zio-schema-examples] Example 7: added Person and Profile schema as implicits

* [zio-schema-examples] Example 7: renamed method for viewing, added Runner

* [zio-schema-examples] Example 7: sketched toDynamicValue method

* [zio-schema-examples] Example 7: implemented toDynamicValue method

* [zio-schema-examples] Example 7: primitive impl of decode using toDV

* [zio-schema-examples] Example 7: produce multiple solutions, pick working one

* [zio-schema-examples] Example 7: introduce QueryParam type alias and compile-function

* [zio-schema-examples] Example 7: return static function on error case

* [zio-schema-examples] Example 7: initial impl of Primitive(standardtype)

* [zio-schema-examples] Example 7: implemented fail

* [zio-schema-examples] Example 7: simple impl of lazy

* [zio-schema-examples] Example 7: better lazy impl with `lazy val`

* [zio-schema-examples] Example 7: impl of Meta

* [zio-schema-examples] Example 7: impl of CaseClass1

* [zio-schema-examples] Example 7: impl of CaseClass2

* [zio-schema-examples] Example 7: impl of CaseClass3

* [zio-schema-examples] Example 7: extracted compiler to builder

* [zio-schema-examples] Example 7: implemented Transform

* [zio-schema-examples] * Example for an Encoder and Decoder using the DynamicValue API
* Add scalafmt
* Update zio-schema 0.1.2
* Update examples to account for annotations

* revert local changes

* moved sources to appropriate place

* fixed example

* [zio-schema-examples] reverted changes to build.sbt

* [zio-schema-examples] removed additional build.sbt, fixed problems in example7, added explicit import in build.sbt

* [zio-schema-examples] scalaFmt applied

* Fix build for scala 2.12 and apply linter

* Fix compilation errors for scala 2.12

* Unused imports

Co-authored-by: Dominik Dorn <dominik@dominikdorn.com>
Co-authored-by: Alexander van Olst <alexvanolst@gmail.com>
Co-authored-by: Pierangelo Cecchetto <pierangeloc@gmail.com>
Co-authored-by: Maxwell Brown <maxwellbrown1990@gmail.com>
Co-authored-by: calvinlfer <calvin.l.fer@gmail.com>
landlockedsurfer pushed a commit to landlockedsurfer/zio-schema that referenced this pull request May 28, 2022
* Provide direct support for Map
Unit tests for json serdes
Unit tests for protobuf serdes

* run scalafmt

* Rebased/reformatted

* Rebased/reformatted, and added a minimum contribution guide

* Run sbt `prepare test` instead of `fmt test`
landlockedsurfer pushed a commit to landlockedsurfer/zio-schema that referenced this pull request May 28, 2022
* added basic documentation with example

* added more dependencies to docs

* incorporated feedback by @thinkharderdev

* fixed wording 'ZIO-Schema' -> 'ZIO Schema'

* fixed naming of other zio libraries

* fix sbt publish setting

* added examples subproject

* docs: fixed mdoc link and added version placeholder in overview/index.md

* docs: incorporated suggestions by @TobiasPfeifer

* scalafmt for build.sbt

* incorporated suggestions by @thinkharderdev

* Support for annotations on all types (zio#147)

* Add addonation to Schema[A] interface and add as field in Schema.Sequence

* Add annotation field to Schema.Transform

* Add annotation field to Schema.Primitive

* Add annotations field to Schema.Optional

* Add annotations field to Schema.Fail

* Add annotations field to Schema.Tuple

* Add annotations field to EitherSchema

* Add annotations field to Schema.Lazy

* Add annotations field to Schema.Meta

* Add remaining assertion equality checks to equalsSchema

* Remove annotations from Record[A] (since it exists in Schema[A] already) and add annotations field to Schema.GenericRecord

* Fix test case for schema generation on annotated ADT

* Fix handling of annotations for Lazy (and thus failing tests)

* Wherever we accept an annotations chunk in the constructor/method we default it to Chunk.empty

* Add `def annotate(annotation: Any): Schema[A]` to Schema

* Replace usage of Chunk.appended with :+ to fix 2.12.12 compilation

* Provide direct support for Map (zio#142)

* Provide direct support for Map
Unit tests for json serdes
Unit tests for protobuf serdes

* run scalafmt

* Rebased/reformatted

* Rebased/reformatted, and added a minimum contribution guide

* Run sbt `prepare test` instead of `fmt test`

* Add defaultValue to Schema  (zio#80)

* feat(schema): add first draft defaultValue implementation for Schema

* feat(schema): add missing test cases

* feat(schema): change default value for BoolType to false

* feat(schema): fold default values in Record type appropriately

* fix(zio-schema): fix failing test

* feat(zio-schema): add defaultValue implementation for MapSchema

* patch(zio-schema): remove orElse for Scala 2.12 compat

* first commit

* [zio-schema-examples] sbt setup

* [zio-schema-examples] readme init

* [zio-schema-examples] example 1

* [zio-schema-examples] example 2

* [zio-schema-examples] example 3 (WIP)

* [zio-schema-examples] example 3

* [zio-schema-examples] Example3: Don't share domain with other samples

* [zio-schema-examples] Example4 transforming DTOs

* [zio-schema-examples] Example5 diffing

* [zio-schema-examples] example 4: fixed map/flatmap in example

* [zio-schema-examples] example 4: another try to converting DTOs

* [zio-schema-examples] example 6 reified optics stub

* [zio-schema-examples] example 6 reified optics examples

* [zio-schema-examples] example 1 + 2: schema fixes for newest zio-schema

* [zio-schema-examples] Problem 7: Initial code

* [zio-schema-examples] Example 6: adding employee now works

* [zio-schema-examples] Example 7: added notes on performance requirements

* [zio-schema-examples] Example 7: added parameter list to methods, fixed names

* [zio-schema-examples] Example 7: added Person and Profile schema as implicits

* [zio-schema-examples] Example 7: renamed method for viewing, added Runner

* [zio-schema-examples] Example 7: sketched toDynamicValue method

* [zio-schema-examples] Example 7: implemented toDynamicValue method

* [zio-schema-examples] Example 7: primitive impl of decode using toDV

* [zio-schema-examples] Example 7: produce multiple solutions, pick working one

* [zio-schema-examples] Example 7: introduce QueryParam type alias and compile-function

* [zio-schema-examples] Example 7: return static function on error case

* [zio-schema-examples] Example 7: initial impl of Primitive(standardtype)

* [zio-schema-examples] Example 7: implemented fail

* [zio-schema-examples] Example 7: simple impl of lazy

* [zio-schema-examples] Example 7: better lazy impl with `lazy val`

* [zio-schema-examples] Example 7: impl of Meta

* [zio-schema-examples] Example 7: impl of CaseClass1

* [zio-schema-examples] Example 7: impl of CaseClass2

* [zio-schema-examples] Example 7: impl of CaseClass3

* [zio-schema-examples] Example 7: extracted compiler to builder

* [zio-schema-examples] Example 7: implemented Transform

* [zio-schema-examples] * Example for an Encoder and Decoder using the DynamicValue API
* Add scalafmt
* Update zio-schema 0.1.2
* Update examples to account for annotations

* revert local changes

* moved sources to appropriate place

* fixed example

* [zio-schema-examples] reverted changes to build.sbt

* [zio-schema-examples] removed additional build.sbt, fixed problems in example7, added explicit import in build.sbt

* [zio-schema-examples] scalaFmt applied

* Fix build for scala 2.12 and apply linter

* Fix compilation errors for scala 2.12

* Unused imports

Co-authored-by: Dominik Dorn <dominik@dominikdorn.com>
Co-authored-by: Alexander van Olst <alexvanolst@gmail.com>
Co-authored-by: Pierangelo Cecchetto <pierangeloc@gmail.com>
Co-authored-by: Maxwell Brown <maxwellbrown1990@gmail.com>
Co-authored-by: calvinlfer <calvin.l.fer@gmail.com>
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.

Support native schema for Map
3 participants