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

Function compileLayout

packages/effect/src/encoding/SchemaBinary.ts:2420–2812  ·  view source on GitHub ↗
(root: SchemaAST.AST)

Source from the content-addressed store, hash-verified

2418}
2419
2420function compileLayout(root: SchemaAST.AST): CompiledLayout {
2421 const exact = isExact(root)
2422 const exitSuccess = !exact && isExitWithExactSuccess(root)
2423 root = SchemaAST.toEncoded(root)
2424 const memo = new Map<SchemaAST.AST, Layout>()
2425 let recursive = false
2426 let stringSlots = 0
2427
2428 const leaf = (kind: LeafKind): LeafLayout => ({ _: kind, slot: kind === "string" ? stringSlots++ : 0 })
2429
2430 function compile(ast: SchemaAST.AST): Layout {
2431 const hit = memo.get(ast)
2432 if (hit !== undefined) return hit
2433 const layout = go(ast)
2434 memo.set(ast, layout)
2435 return layout
2436 }
2437
2438 function go(ast: SchemaAST.AST): Layout {
2439 switch (ast._tag) {
2440 case "String":
2441 case "TemplateLiteral":
2442 return leaf("string")
2443 case "Symbol":
2444 case "UniqueSymbol":
2445 return leaf("symbol")
2446 case "Boolean":
2447 return leaf("bool")
2448 case "Null":
2449 return leaf("null")
2450 case "Undefined":
2451 case "Void":
2452 return leaf("undefined")
2453 case "Number": {
2454 const fixed = fixedWidthKind(ast)
2455 if (fixed === "i32" || fixed === "u32") return leaf(fixed)
2456 return leaf(provesInteger(ast) ? "int" : "number")
2457 }
2458 case "BigInt": {
2459 const fixed = fixedWidthKind(ast)
2460 return leaf(fixed === "i64" || fixed === "u64" ? fixed : "bigint")
2461 }
2462 case "Literal":
2463 return literalLayout(leaf(literalKind(ast.literal)), [ast.literal])
2464 case "Unknown":
2465 case "Any":
2466 case "ObjectKeyword":
2467 return leaf("json")
2468 case "Never":
2469 return { _: "never", ast }
2470 case "Enum":
2471 return compileUnion(ast, flattenMembers(enumsToLiterals(ast)))
2472 case "Suspend": {
2473 recursive = true
2474 const layout = compile(ast.thunk())
2475 memo.set(ast, layout)
2476 return layout
2477 }

Callers 1

SchemaBinary.tsFile · 0.85

Calls 3

isExitWithExactSuccessFunction · 0.85
isExactFunction · 0.70
compileFunction · 0.70

Tested by

no test coverage detected