MCPcopy Index your code
hub / github.com/anomalyco/opencode / structuralType

Function structuralType

packages/httpapi-codegen/src/index.ts:556–584  ·  view source on GitHub ↗
(schema: Schema.Top)

Source from the content-addressed store, hash-verified

554}
555
556function structuralType(schema: Schema.Top) {
557 const document = SchemaRepresentation.toCodeDocument(SchemaRepresentation.fromASTs([schema.ast]))
558 if (
559 document.artifacts.some(
560 (artifact) =>
561 artifact._tag !== "Import" || artifact.importDeclaration !== 'import type * as Brand from "effect/Brand"',
562 ) ||
563 Object.keys(document.references.recursives).length > 0
564 ) {
565 throw new GenerationError({ reason: "Referenced Promise types are not implemented" })
566 }
567 const references = new Map(
568 document.references.nonRecursives.map((reference) => [reference.$ref, reference.code.Type]),
569 )
570 const expand = (type: string, seen = new Set<string>()): string => {
571 for (const [reference, value] of references) {
572 const pattern = `(?<![A-Za-z0-9_$.'"])${reference.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(?![A-Za-z0-9_$.'"])`
573 if (!new RegExp(pattern).test(type)) continue
574 if (seen.has(reference)) {
575 throw new GenerationError({ reason: `Recursive Promise types are not implemented: ${reference}` })
576 }
577 type = type.replace(new RegExp(pattern, "g"), `(${expand(value, new Set([...seen, reference]))})`)
578 }
579 return type
580 }
581 return expand(document.codes[0].Type)
582 .replaceAll(/ & Brand\.Brand<"[^"]+">/g, "")
583 .replaceAll("Schema.Json", "JsonValue")
584}
585
586function promisePath(path: string, input: ReadonlyArray<InputField>) {
587 if (path.includes("*")) throw new GenerationError({ reason: `Unsupported Promise path wildcard: ${path}` })

Callers 1

typeOfFunction · 0.85

Calls 1

expandFunction · 0.70

Tested by

no test coverage detected