/attempt #517
Title
Add structural schemas and .structural API (issue #517)
Summary
Implements structural schemas and the new Schema.structural API to derive structural
views for nominal types with a safe DynamicValue-backed fallback. This PR provides
platform-aware derivation for Scala 3 and Scala 2.13, runtime adapters to expose
structural views, tests, and docs.
What this PR does
ToStructural typeclass derivation (Scala 3 and Scala 2 macro implementations).Schema.structural extension that returns a structural view of a derived schema.Selectable adapter from DynamicValue.Dynamic adapter from DynamicValue.docs/reference/ describing the .structural API and feature flags..gitignore entry for generated trait sources.Why
DynamicValue-backed schema to guarantee correctness and cross-build safety.Local verification Commands used:
# Scala 3 JVM
sbt schemaJVM/test
# Scala 2.13 cross-build
sbt ++2.13.18 schemaJVM/test
Results:
Important notes / Limitations
Schema[GeneratedTrait] construction at the type-level, but constructing
a fully typed Schema[GeneratedTrait] is not yet implemented in all cases; in those situations the macro
falls back to a DynamicValue-backed schema and emits an informational message. This avoids unsafe casts and
keeps the derivation correct across Scala versions.scala-3/ and scala-2/ directories to keep the cross-build green..gitignore under /target/structural-generated/).How to review (key places)
schema/shared/src/main/scala-3/zio/blocks/schema/ToStructuralVersionSpecific.scalaschema/shared/src/main/scala-2/zio/blocks/schema/ToStructuralVersionSpecific.scalaschema/shared/src/main/scala-3/zio/blocks/schema/StructuralRuntime.scalaschema/shared/src/main/scala-2/zio/blocks/schema/StructuralRuntime.scalaschema/shared/src/test/... (Scala 3 tests)schema/shared/src/test/scala-2/... (Scala 2 tests if present)docs/reference/ — structural usage and feature flag explanation.Usage example
import zio.blocks.schema.Schema
val structSchema = Schema.derived[MyCaseClass].structural
// Use runtime adapter to access structural fields via Selectable (Scala 3) or Dynamic (Scala 2)
Feature flags (opt-in)
-Dzio.blocks.structural.enableNamedTrait=true-Dzio.blocks.structural.emitNamedTraitSymbol=true-Dzio.blocks.structural.emitToSource=true|falseBounty claim instructions (per bounty rules)
/attempt #517 to indicate work begun./claim #517 in this PR body to claim the bounty. (Included below.)sbt schemaJVM/test),Schema.derived[T].structural in the REPL or a tiny main.
Attach the video file to this PR or provide a link to a hosted recording in the PR comments./claim #517
Dipak Kumar
@dkrray772-gmail-com
anzal787
@anzal787
ZIO
@ZIO