johnhungerford / generic-schema

2 stars 1 forks source link

Select inner component by type #73

Closed johnhungerford closed 2 years ago

johnhungerford commented 2 years ago

Retrieve a field or subtype from a schema or a schema component by type

val sch = Schema.derived[Outer]

val component = sch.retrieveType[Inner]

The above syntax should retrieve the first, when traversing the schema. To retrieve subsequent types:

val component = sch.retrieveTypeN[Inner](2)

You should be able to modify a component by type as well:

val newSch1 = sch.modifyComponentType[Inner](_.withDescription("new description"))
val newSch2 = sch.modifyComponentTypeN[Inner](2)(_.withDescription("new description"))

As a bonus, include type level selection:

val component = sch.retrieveType[Inner /:/ Nested]
val component1 = sch.retrieveTypeN[Inner /:/ Nested](2)

val newSch1 = sch.modifyComponentType[Inner /:/ Nested](_.withDescription("new description"))
val newSch2 = sch.modifyComponentType[Inner /:/ Nested](2)(_.withDescription("new description"))

// or even

val newSch3 = sch.modifyComponentType[Inner *:* 2 /:/ Nested *:* 1](_withDescription("new description"))

where *:* N says get Nth component of that type

johnhungerford commented 2 years ago

Rather than iterate over the types as a flat list (or in addition?), let's make types a selectable feature and just use the same selection mechanism as indices and field names. We'll define a functions to generate the appropriate type selectors:

val newSch1. = sch(tpe[Inner] / "field_name" / tpeN[Nested](1))

tpe will allow you to select the first component of that type, and tpeN will allow you to select the Nth component of that type.

In addition, let's replace the retrieveType method from above with extractSchema and extractSchemas, where extractSchema returns the first schema of a given type encountered in traversing the schema components, and extractSchemas returns a tuple of all schemas of a given type within the schema.

johnhungerford commented 2 years ago

closed with #78