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

Function encoder

packages/effect/src/encoding/SchemaBinary.ts:413–452  ·  view source on GitHub ↗
(
  schema: S,
  options?: SchemaAST.ParseOptions & Options & StreamOptions
)

Source from the content-addressed store, hash-verified

411 * @since 4.0.0
412 */
413export function encoder<S extends Schema.Constraint>(
414 schema: S,
415 options?: SchemaAST.ParseOptions & Options & StreamOptions
416): Encoder {
417 const { exact, layout, target } = compileTarget(schema)
418 const dictionary = requireDictionary(exact, options)
419 const encode = encodeUnknownSync(schema, options)
420 const encodeMany = encodeManyUnknownSync(schema, options)
421 if (!dictionary) return { encode, encodeMany }
422 const mode = compileMode(layout, options?.fingerprint)
423 const parseOptions: SchemaAST.ParseOptions = options ?? SchemaAST.defaultParseOptions
424 const validate = parseOptions.onExcessProperty === "error"
425 ? Schema.encodeUnknownSync(
426 target as Schema.ConstraintEncoder<unknown, never>,
427 options
428 ) as (value: unknown) => unknown
429 : undefined
430 const dict = makeDictWrite()
431 const run = <A>(f: () => A): A => {
432 try {
433 return f()
434 } catch (e) {
435 throw e instanceof IssueError ? new Schema.SchemaError(e.issue) : e
436 }
437 }
438 return {
439 encode: (value) =>
440 run(() => encodeFrame(layout, validate === undefined ? value : validate(value), parseOptions, mode, dict)),
441 encodeMany: (values) =>
442 run(() =>
443 encodeFrames(
444 layout,
445 validate === undefined ? values : values.map(validate),
446 parseOptions,
447 mode,
448 dict
449 )
450 )
451 }
452}
453
454/**
455 * The writer returned by {@link encoder}.

Callers

nothing calls this directly

Calls 11

compileTargetFunction · 0.85
requireDictionaryFunction · 0.85
encodeManyUnknownSyncFunction · 0.85
compileModeFunction · 0.85
makeDictWriteFunction · 0.85
encodeFramesFunction · 0.85
encodeUnknownSyncFunction · 0.70
runFunction · 0.70
encodeFrameFunction · 0.70
validateFunction · 0.70
mapMethod · 0.45

Tested by

no test coverage detected