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

Function makeObjectBase

packages/effect/src/schema/SchemaCompiler/runtime.ts:53–98  ·  view source on GitHub ↗
(
  ast: SchemaAST.Objects,
  compile: Compiler,
  compileField: Compiler,
  generate: GenerateObject
)

Source from the content-addressed store, hash-verified

51 ) => Effect.Effect<void, SchemaIssue.Issue, any>
52}) => typeof SchemaAST.parseArray
53const makeObjectBase = (
54 ast: SchemaAST.Objects,
55 compile: Compiler,
56 compileField: Compiler,
57 generate: GenerateObject
58): SchemaIssueParser => {
59 let properties: Array<ParsedProperty> | undefined
60 const getProperties = (): Array<ParsedProperty> => {
61 if (properties !== undefined) return properties
62 const parsers = new Map<SchemaAST.AST, SchemaIssueParser>()
63 return properties = ast.propertySignatures.map((property) => {
64 let parser = parsers.get(property.type)
65 if (parser === undefined) {
66 parser = compileField(property.type)
67 parsers.set(property.type, parser)
68 }
69 return {
70 parser,
71 name: property.name,
72 type: property.type,
73 ownOnly: SchemaAST.requiresOwnProperty(property.name)
74 }
75 })
76 }
77 let fallback: SchemaIssueParser | undefined
78 const runFallback: SchemaIssueParser = (input, options) =>
79 (fallback ??= ast.getParser(compile, compileField))(input, options)
80 const resume = (
81 snapshot: ObjectParserState,
82 index: number,
83 pending: Effect.Effect<unknown, SchemaIssue.Issue, any>
84 ): Effect.Effect<unknown, SchemaIssue.Issue, any> => {
85 const property = properties![index]
86 return Effect.flatMap(Effect.exit(pending), (exit): Effect.Effect<unknown, SchemaIssue.Issue, any> => {
87 // Each execution gets its own output; `issues` is still undefined here
88 // because `errors: "all"` is routed to the fallback before suspending.
89 const state: ObjectParserState = { ...snapshot, out: { ...snapshot.out } }
90 const terminal = SchemaAST.stepProperty(state, property, exit)
91 if (terminal) return terminal
92 const done = () => InternalParser.succeed(state.out)
93 const effect = SchemaAST.parseProperties(state, properties!.slice(index + 1))
94 return effect ? Effect.flatMapEager(effect, done) : done()
95 })
96 }
97 return generate({ ast, getProperties, fallback: runFallback, resume, step: SchemaAST.stepProperty })
98}
99
100const makeArrayBase = (
101 ast: SchemaAST.Arrays,

Callers 2

decodeFunction · 0.85
makeFunction · 0.85

Calls 1

generateFunction · 0.50

Tested by

no test coverage detected