MCPcopy Create free account
hub / github.com/Effect-TS/effect / getClassSchemaFactory

Function getClassSchemaFactory

packages/effect/src/Schema.ts:14183–14229  ·  view source on GitHub ↗
(
  from: S,
  identifier: string,
  annotations: Annotations.Declaration<any, readonly [S]> | undefined
)

Source from the content-addressed store, hash-verified

14181}
14182
14183function getClassSchemaFactory<S extends Constraint>(
14184 from: S,
14185 identifier: string,
14186 annotations: Annotations.Declaration<any, readonly [S]> | undefined
14187) {
14188 let memo: decodeTo<declareConstructor<any, S["Encoded"], readonly [S]>, S> | undefined
14189 return <Self extends (new(...args: ReadonlyArray<any>) => any) & { readonly identifier: string }>(
14190 self: Self
14191 ): decodeTo<declareConstructor<Self, S["Encoded"], readonly [S]>, S> => {
14192 if (memo !== undefined) {
14193 return memo
14194 }
14195 const ClassTypeId = getClassTypeId(identifier)
14196 const isClassValue: Predicate.Predicate<unknown> = (input) =>
14197 input instanceof self || Predicate.hasProperty(input, ClassTypeId)
14198 const transformation = getClassTransformation(self)
14199 const to = make<declareConstructor<Self, S["Encoded"], readonly [S]>>(
14200 new SchemaAST.Declaration(
14201 [from.ast],
14202 () => (input, ast, options) => {
14203 return isClassValue(input) ?
14204 Effect.succeed(input) :
14205 Effect.fail(new SchemaIssue.InvalidType(ast, input, options))
14206 },
14207 {
14208 identifier,
14209 [InternalAnnotations.CONSTRUCTOR_ANNOTATION_KEY]: (
14210 [from]: readonly [SchemaAST.AST]
14211 ): SchemaAST.ConstructorDescriptor => ({
14212 isConstructed: isClassValue,
14213 link: new SchemaAST.Link(from, transformation)
14214 }),
14215 toCodec: ([from]: readonly [ConstraintCodec<S["Encoded"], S["Encoded"]>]) =>
14216 new SchemaAST.Link(from.ast, transformation),
14217 toArbitrary: ([from]: readonly [Annotations.ToArbitrary.TypeParameter<S["Type"]>]) => () => ({
14218 arbitrary: from.arbitrary.map((args: S["Type"]) => new self(args)),
14219 terminal: from.terminal?.map((args: S["Type"]) => new self(args))
14220 }),
14221 toFormatter: ([from]: readonly [Formatter<S["Type"]>]) => (t: Self) => `${self.identifier}(${from(t)})`,
14222 [InternalAnnotations.SENTINELS_ANNOTATION_KEY]: SchemaAST.collectSentinels(from.ast),
14223 ...annotations
14224 }
14225 )
14226 )
14227 return memo = decodeTo<declareConstructor<Self, S["Encoded"], readonly [S]>, S>(to, transformation)(from)
14228 }
14229}
14230
14231function isStruct(schema: Struct.Fields | Struct<Struct.Fields>): schema is Struct<Struct.Fields> {
14232 return isSchema(schema)

Callers 1

makeClassFunction · 0.85

Calls 9

getClassTypeIdFunction · 0.85
getClassTransformationFunction · 0.85
isClassValueFunction · 0.85
fromFunction · 0.85
makeFunction · 0.70
decodeToFunction · 0.70
succeedMethod · 0.45
failMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected