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

Function makeArrayBase

packages/effect/src/schema/SchemaCompiler/runtime.ts:100–153  ·  view source on GitHub ↗
(
  ast: SchemaAST.Arrays,
  compile: Compiler,
  compileField: Compiler,
  generate: GenerateArray
)

Source from the content-addressed store, hash-verified

98}
99
100const makeArrayBase = (
101 ast: SchemaAST.Arrays,
102 compile: Compiler,
103 compileField: Compiler,
104 generate: GenerateArray
105): SchemaIssueParser => {
106 let element: { readonly ast: SchemaAST.AST; readonly parser: SchemaIssueParser } | undefined
107 const getElement = () => (element ??= {
108 ast: ast.rest[0],
109 parser: compileField(ast.rest[0])
110 })
111 let fallback: SchemaIssueParser | undefined
112 const runFallback: SchemaIssueParser = (input, options) =>
113 (fallback ??= ast.getParser(compile, compileField))(input, options)
114 const run = generate({
115 getElement: () => getElement().parser,
116 step: SchemaAST.stepArray,
117 resume: (state, item, index, pending, end) =>
118 Effect.flatMap(
119 Effect.exit(pending),
120 (exit) =>
121 SchemaAST.stepArray(state, item, exit, index) ??
122 SchemaAST.parseArray(state, state.input, index + 1, end) ?? Effect.void
123 )
124 })
125 const specialized = Effect.fnUntracedEager(function*(input: unknown, options: SchemaAST.ParseOptions) {
126 if (input === InternalParser.missing) return InternalParser.missing
127 if (!Array.isArray(input)) {
128 return yield* Effect.fail(new SchemaIssue.InvalidType(ast, input, options))
129 }
130 const descriptor = getElement()
131 const len = input.length
132 const state: ArrayParserState = {
133 ast,
134 getParser: () => descriptor,
135 input,
136 len,
137 tailThreshold: len,
138 output: new globalThis.Array(len),
139 issues: undefined,
140 options
141 }
142 const effect = run(state, input, 0, len)
143 if (effect) yield* effect
144 if (state.issues) {
145 return yield* Effect.fail(new SchemaIssue.Composite(ast, state.issues, input, options))
146 }
147 return state.output
148 })
149 return (input, options) =>
150 options.concurrency !== undefined && resolveConcurrency(options.concurrency) !== 1
151 ? runFallback(input, options)
152 : specialized(input, options)
153}
154
155const decode = (
156 ast: SchemaAST.AST,

Callers 2

decodeFunction · 0.85
makeFunction · 0.85

Calls 7

getElementFunction · 0.85
runFallbackFunction · 0.85
exitMethod · 0.80
parseArrayMethod · 0.80
failMethod · 0.65
generateFunction · 0.50
runFunction · 0.50

Tested by

no test coverage detected