Skip to content

[schema] introduce module#1517

Open
fwbrasil wants to merge 2 commits intomainfrom
kyo-schema-pr
Open

[schema] introduce module#1517
fwbrasil wants to merge 2 commits intomainfrom
kyo-schema-pr

Conversation

@fwbrasil
Copy link
Copy Markdown
Collaborator

From the module's README:

kyo-schema

Define a case class and get JSON serialization, Protobuf encoding, field validation, type-safe lenses, structural diffs, and more, all derived from the type's structure. No annotations, no boilerplate. Works across JVM, JavaScript, and Scala Native.

The module depends only on kyo-data (pure data structures) and has no dependency on Kyo's effect runtime, so it can be adopted as a standalone library.

// Schema-based JSON and Protobuf
Json.encode(alice)      // {"id":1,"name":"Alice","password":"secret","address":{"city":"Portland","zip":"97201"}}
Protobuf.encode(alice)  // Span[Byte] (binary)

// Type-safe lenses reach any depth
Schema[User].focus(_.address.city).update(alice)(_.toUpperCase)
// User(1, "Alice", "secret", Address("PORTLAND", "97201"))

// Type-level reshaping: drop sensitive fields, rename, add computed
val publicView = Schema[User]
    .drop(_.password)
    .rename(_.name, "displayName")
// Encodes alice as: {"id":1,"displayName":"Alice","address":{"city":"Portland","zip":"97201"}}

// Diffs as data: capture just what changed, ship, replay
val renamed = alice.copy(name = "Alicia")
val cs = Changeset(alice, renamed) // Produces a changeset with the new name patch
cs.applyTo(alice)                  // Result.Success(renamed)

Everything flows from Schema[A], the central type that captures a type's structure at compile time. It's the single source of truth that powers serialization, validation, navigation, and conversion.

The module provides these primitives:

Primitive Purpose
Json / Protobuf Serialize to JSON strings or Protocol Buffers bytes
Focus Type-safe lens for reading, writing, and modifying fields at any depth
Compare Read-only field-by-field comparison of two values
Modify Batched field mutations applied as a single unit
Changeset Serializable diff that can be stored, transmitted, and replayed
Builder Incremental, type-safe case class construction
Convert Bidirectional conversion between structurally compatible types
Structure Runtime type description and untyped value trees

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.

1 participant