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

Function renderSchemas

packages/httpapi-codegen/src/index.ts:1123–1170  ·  view source on GitHub ↗
(slots: ReadonlyArray<Slot>)

Source from the content-addressed store, hash-verified

1121}
1122
1123function renderSchemas(slots: ReadonlyArray<Slot>) {
1124 if (slots.length === 0) return ""
1125 const classes = new Map(
1126 slots.flatMap((slot, index) => {
1127 const tagged = taggedErrorFields(slot.schema)
1128 return tagged === undefined ? [] : [[index, tagged] as const]
1129 }),
1130 )
1131 const expanded = [
1132 ...slots.map((slot, index) => (classes.has(index) ? { name: slot.name, schema: Schema.Never } : slot)),
1133 ...Array.from(classes.values()).flatMap((tagged, classIndex) =>
1134 tagged.fields.map(([name, schema]) => ({ name: `Class${classIndex}${name}`, schema })),
1135 ),
1136 ]
1137 const [first, ...rest] = expanded
1138 const document = SchemaRepresentation.toCodeDocument(
1139 SchemaRepresentation.fromASTs([first.schema.ast, ...rest.map((slot) => slot.schema.ast)]),
1140 )
1141 const artifacts = document.artifacts.flatMap((artifact) => {
1142 if (artifact._tag === "Import") return [artifact.importDeclaration]
1143 if (artifact._tag === "Enum") return [artifact.generation.runtime]
1144 return [`const ${artifact.identifier} = ${artifact.generation.runtime}`]
1145 })
1146 const references = [
1147 ...document.references.nonRecursives.map(({ $ref, code }) => `const ${$ref} = ${code.runtime}`),
1148 ...Object.entries(document.references.recursives).map(
1149 ([$ref, code]) => `type ${$ref} = ${code.Type}\nconst ${$ref}: Schema.Codec<${$ref}> = ${code.runtime}`,
1150 ),
1151 ]
1152 let fieldIndex = slots.length
1153 const declarations = slots.map((slot, index) => {
1154 const tagged = classes.get(index)
1155 if (tagged === undefined) return `const ${slot.name} = ${document.codes[index].runtime}`
1156 const fields = tagged.fields
1157 .map(([name]) => `${JSON.stringify(name)}: ${document.codes[fieldIndex++].runtime}`)
1158 .join(", ")
1159 const annotations = Object.entries({
1160 httpApiStatus: resolveHttpApiStatus(slot.schema.ast),
1161 "~httpApiEncoding": resolveHttpApiEncoding(slot.schema.ast),
1162 }).filter((entry) => entry[1] !== undefined)
1163 const annotate =
1164 annotations.length === 0
1165 ? ""
1166 : `.annotate({ ${annotations.map(([key, value]) => `${JSON.stringify(key)}: ${JSON.stringify(value)}`).join(", ")} })`
1167 return `class ${slot.name}Class extends Schema.TaggedErrorClass<${slot.name}Class>(${JSON.stringify(tagged.identifier)})(${JSON.stringify(tagged.tag)}, { ${fields} }) {}\nconst ${slot.name} = ${slot.name}Class${annotate}`
1168 })
1169 return [...artifacts, ...references, ...declarations].join("\n\n")
1170}
1171
1172function renderClient(groups: ReadonlyArray<Group>) {
1173 const imports = groups

Callers 1

renderGroupFunction · 0.85

Calls 4

taggedErrorFieldsFunction · 0.85
getMethod · 0.65
fromMethod · 0.45
valuesMethod · 0.45

Tested by

no test coverage detected