Support constructor registry in DynamicValue.toTypedValue for AST-rebuilt schemas (#511) - #1137
Open
chengdazhi wants to merge 1 commit into
Open
Support constructor registry in DynamicValue.toTypedValue for AST-rebuilt schemas (#511)#1137chengdazhi wants to merge 1 commit into
chengdazhi wants to merge 1 commit into
Conversation
…uilt schemas (zio#511) Schemas materialized from a MetaSchema (schema.ast.toSchema) represent records as Schema.GenericRecord and lose the information needed to construct the original typed representation, so converting a dynamic record with such a schema yields a raw ListMap instead of the case class. Add an overload of toTypedValue that takes a constructor registry (PartialFunction[TypeId, DynamicValue.Constructor[_]]), as suggested by @jdegoes in the issue discussion. The registry is threaded through the whole conversion, so records are reconstructed at any nesting level: nested records, options, sequences, maps, eithers, tuples and enum cases. Records missing from the registry keep the existing generic record (ListMap) behaviour. Also add DynamicValue.Constructor with helpers to build a registry directly from record schemas.
|
|
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
/claim 511
Closes #511
Problem
When a schema is rebuilt from its AST (
schema.ast.toSchema), records are materialized asSchema.GenericRecord, which only knows how to build aListMap[String, _]. Converting a dynamic record with such a schema therefore yields a rawListMapinstead of the original case class — the typed constructor was lost when the schema was turned into aMetaSchema.Solution
Implements the registry approach suggested by @jdegoes in the issue discussion (#511 (comment)):
DynamicValue.toTypedValue[A](registry: PartialFunction[TypeId, DynamicValue.Constructor[_]])(implicit schema: Schema[A]): Either[DecodeError, A].DynamicValue.Constructor[A], which builds anAfrom the decoded field values of a record, withConstructor.fromSchemaandConstructor.registryhelpers to build a registry directly from record schemas (including the implicit derived ones, so no reflection is involved).Option, sequences, sets, maps,Either, tuples and enum cases — not just at the top level (this is where the naive attempt in dynamic value to typed using a registry #725 stops).ListMap[String, _]), so the change is fully backwards compatible;decodeStructureretains its original signature.Usage with the reproduction from the issue:
Tests
DynamicValueSpeccovering: top-level records rebuilt from their AST, fallback to generic records when no constructor is registered, nested records /Option/Chunk, enum cases, and failure when the dynamic record does not match the registered constructor.zioSchemaJVM/test,zioSchemaDerivationJVM/test,testsJVM/teston Scala 2.12.21, 2.13.18 and 3.3.8;zioSchemaJS/test,testsJS/testOnly zio.schema.DynamicValueSpecon 2.13.18;zioSchemaNative/compile,testsNative/compile;scalafmtCheckAll,scalafix --checkon the touched modules;zioSchemaJVM/mimaReportBinaryIssues(no problems).Note: this PR was prepared with the assistance of an AI coding agent; the changes were reviewed and verified by running the checks listed above.