MCPcopy Create free account
hub / github.com/arktypeio/arktype / writeStructure

Function writeStructure

ark/type/standalone/combinators.ts:821–938  ·  view source on GitHub ↗
(
	js: CombinatorCompiler,
	structure: StructureNode
)

Source from the content-addressed store, hash-verified

819}
820
821const writeStructure = (
822 js: CombinatorCompiler,
823 structure: StructureNode
824): string => {
825 const ref = (node: BaseRoot) => readValue(js, node)
826 if (structure.sequence) {
827 const element = structure.sequence.variadic!
828 return writeCall(
829 !element.tfs ? "elementsOf"
830 : element.hasAlias || element.hasMorph ? "failingElementsOf"
831 : "transformingElementsOf",
832 ref(element),
833 element.tfs && element.hasAlias ? "1" : undefined
834 )
835 }
836 const refs = structure.inOrder.map(prop => ref(prop.value))
837 // in arktype's order; a required key expects its value's brief, and says its
838 // value's message, each of which its checker holds
839 const entries = structure.inOrder.map((prop, i) => {
840 const checker = refs[i]
841 return `${writeKey(prop.key as string)}: ${
842 prop.is("optional") ?
843 !prop.hasDef() ? writeCall("optional", checker)
844 : isLiteralDefault(prop) ?
845 writeCall(
846 prop.defaultsUndefined ? "replacingUndefined" : "optional",
847 checker,
848 writeLiteral(js, prop.default)
849 )
850 : writeCall(
851 "writtenBy",
852 checker,
853 prop.defMorph.length === 2 ?
854 `(data, ctx) => ${js.ref(prop.defMorph)}(data, ctx)`
855 : `data => ${js.ref(prop.defMorph)}(data)`,
856 prop.defaultsUndefined ? "1" : undefined
857 )
858 : writeRequired(prop, checker)
859 }`
860 })
861 const declared = writeObject(entries)
862 if (structure.index) {
863 const [{ signature, value }] = structure.index
864 if (
865 structure.index.length === 1 &&
866 !entries.length &&
867 !structure.handlesUndeclared &&
868 signature.is("domain") &&
869 signature.domain === "string"
870 )
871 return writeCall("recordOf", ref(value))
872 return writeCall(
873 "indexed",
874 `[${structure.index.map(({ signature, value }) => `[${readChecker(js, signature)}, ${ref(value)}]`).join(", ")}]`,
875 entries.length ? declared : structure.handlesUndeclared && "void 0",
876 structure.handlesUndeclared &&
877 JSON.stringify(removedSiteOf(structure)!.description)
878 )

Callers 1

Calls 15

writeCallFunction · 0.85
writeKeyFunction · 0.85
isLiteralDefaultFunction · 0.85
writeLiteralFunction · 0.85
writeRequiredFunction · 0.85
writeObjectFunction · 0.85
readCheckerFunction · 0.85
removedSiteOfFunction · 0.85
writeSitedAllowsFunction · 0.85
writeSitedKeepFunction · 0.85
writeSitedWriteFunction · 0.85
isMethod · 0.80

Tested by

no test coverage detected