Effect CommunityEC
Effect Community3y ago
14 replies
TheStockBroker

Encoding without undoing transformation

Ok last thing, I promise!
I have a transforming schema
const Foo = Schema.struct(...)
const Bar = Schema.struct(...)

const FooWithBar = pipe(Foo, Schema.extends(Schema.struct({ bar: Bar })))

const fromFoo = Schema.transformResult(
  Schema.from(Foo),
  FooWithBar,
  foo => pipe(
    Effect.succeed(foo),
    Effect.bind("bar", ({id}) => getBarOfFoo(id)))
  ),
  ParseResult.succeed
)

Decoding Foo --> FooWithBar works great!
Encoding FooWithBar ---> Foo same deal! amazing!
but how would I Encode FooWithBar --> JsonFriendlyFooWithBar?
and decode JsonFriendlyFooWithBar ---> FooWithBar?

Will I have to define a new schema that is identical to FooWithBar just not based on transformations? My use case is quite complicated, with multiple layers of transformations like these, so that would SUCK. Is there a cleaner way?
Was this page helpful?