Skip to content

Commit

Permalink
make VariantSchema constructor apis internal (#3482)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart authored Aug 19, 2024
1 parent c35c814 commit dba570a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 70 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-apricots-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/experimental": patch
---

make VariantSchema constructor apis internal
97 changes: 27 additions & 70 deletions packages/experimental/src/VariantSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,74 +124,6 @@ export declare namespace Field {
}
}

const StructProto = {
pipe() {
return pipeArguments(this, arguments)
}
}

/**
* @since 1.0.0
* @category constructors
*/
export const Struct = <const A extends Field.Fields>(fields: A): Struct<A> => {
const self = Object.create(StructProto)
self[TypeId] = fields
return self
}

const FieldProto = {
[FieldTypeId]: FieldTypeId,
pipe() {
return pipeArguments(this, arguments)
}
}

/**
* @since 1.0.0
* @category constructors
*/
export const Field = <const A extends Field.Config>(schemas: A): Field<A> => {
const self = Object.create(FieldProto)
self.schemas = schemas
return self
}

/**
* @since 1.0.0
* @category constructors
*/
export const fieldEvolve: {
<
Self extends Field<any>,
Mapping extends {
readonly [K in keyof Self["schemas"]]?: (variant: Self["schemas"][K]) => Field.ValueAny
}
>(f: Mapping): (self: Self) => Field<
{
readonly [K in keyof Self["schemas"]]: K extends keyof Mapping
? Mapping[K] extends (arg: any) => any ? ReturnType<Mapping[K]> : Self["schemas"][K]
: Self["schemas"][K]
}
>
<
Self extends Field<any>,
Mapping extends {
readonly [K in keyof Self["schemas"]]?: (variant: Self["schemas"][K]) => Field.ValueAny
}
>(self: Self, f: Mapping): Field<
{
readonly [K in keyof Self["schemas"]]: K extends keyof Mapping
? Mapping[K] extends (arg: any) => any ? ReturnType<Mapping[K]> : Self["schemas"][K]
: Self["schemas"][K]
}
>
} = dual(
2,
(self: Field<any>, f: Record<string, (schema: Field.ValueAny) => Field.ValueAny>): Field<any> =>
Field(Struct_.evolve(self.schemas, f))
)

/**
* @since 1.0.0
* @category extractors
Expand Down Expand Up @@ -261,8 +193,8 @@ type RequiredKeys<T> = {
}[keyof T]

/**
* @category models
* @since 1.0.0
* @category models
*/
export interface Class<
Self,
Expand Down Expand Up @@ -439,7 +371,7 @@ export const make = <
const field = FieldTypeId in self ? self : Field(Object.fromEntries(
options.variants.map((variant) => [variant, self])
))
return fieldEvolve(field, f)
return Field(Struct_.evolve(field.schemas, f))
}
)
return {
Expand Down Expand Up @@ -481,3 +413,28 @@ export const Overrideable = <From, IFrom, RFrom, To, ITo, R>(
decode: (_) => ParseResult.succeed(undefined),
encode: (dt) => options.generate(dt === undefined ? Option.none() : Option.some(dt))
}).pipe(Schema.propertySignature, Schema.withConstructorDefault(constUndefined))

const StructProto = {
pipe() {
return pipeArguments(this, arguments)
}
}

const Struct = <const A extends Field.Fields>(fields: A): Struct<A> => {
const self = Object.create(StructProto)
self[TypeId] = fields
return self
}

const FieldProto = {
[FieldTypeId]: FieldTypeId,
pipe() {
return pipeArguments(this, arguments)
}
}

const Field = <const A extends Field.Config>(schemas: A): Field<A> => {
const self = Object.create(FieldProto)
self.schemas = schemas
return self
}

0 comments on commit dba570a

Please sign in to comment.