UNPKG

zod-to-x

Version:

Multi language types generation from Zod schemas.

101 lines (100 loc) 4.05 kB
import { ASTAliasedTypes, ASTEnum, ASTIntersection, ASTNode, ASTObject, ASTUnion, Zod2X } from "../../core"; import { IZod2GoOpt } from "./options"; /** * Transpiler for Zod schemas to Go structs and types. */ export declare class Zod2Go extends Zod2X<IZod2GoOpt> { protected readonly commentKey = "//"; protected lib: { timePackage: string; jsonPackage: string; fmtPackage: string; }; constructor(opt?: IZod2GoOpt); protected runBefore(): void; protected runAfter(): void; /** * Consolidates all collected imports into a proper Go import block. * Single import → `import "pkg"`, multiple → `import (\n\t"pkg"\n)`. */ private _consolidateImports; protected addImportFromFile(filename: string, namespace: string): string; protected getTypeFromExternalNamespace(namespace: string, typeName: string): string; protected addExtendedType(name: string, parentNamespace: string, aliasOf: string, opt?: { type?: "union" | "alias"; isInternal?: boolean; templates?: string; declaredTemplates?: string; }): void; protected getGenericTemplatesTranslation(data: ASTNode): string | undefined; /** * Emits an alias/extension declaration early when a node references another layered type. */ protected checkExtendedTypeInclusion(data: ASTNode, type?: "union" | "alias"): boolean; protected getStringType: () => string; protected getBooleanType: () => string; protected getAnyType: () => string; protected getDateType: () => string; protected getNumberType: (isInt: boolean, range: { min?: number; max?: number; }) => string; protected getLiteralStringType(value: string | number | boolean, parentEnumNameKey?: [string, string]): string | number; /** Ex: []TypeA, [][]TypeA */ protected getArrayType(arrayType: string, arrayDeep: number): string; /** Ex: map[TypeA]struct{} */ protected getSetType: (itemType: string) => string; /** Ex: map[KeyType]ValueType */ protected getMapType: (keyType: string, valueType: string) => string; /** Ex: map[KeyType]ValueType */ protected getRecordType: (keyType: string, valueType: string) => string; /** Go has no native tuple; use []any */ protected getTupleType: (_itemsType: string[]) => string; /** Go has no native union; use any */ protected getUnionType: (_itemsType: string[]) => string; /** Handled entirely in transpileIntersection via struct embedding */ protected getIntersectionType: () => string; protected transpileAliasedType(data: ASTAliasedTypes): void; /** * Emit a Go enum using a typed string or int alias + const block. * * All-string values: * type EnumItem string * const ( * EnumItemEnum1 EnumItem = "Enum1" * ) * * All-int values: * type EnumItem int * const ( * EnumItemNativeEnum1 EnumItem = 1 * ) * * Mixed (int + string): untyped constants with warning comment. */ protected transpileEnum(data: ASTEnum): void; /** * Go union: emit `type Name any` with a comment listing possible types. * * For discriminated unions: emit a marker interface + marker stubs on each * member type + an `UnmarshalXxx` helper that dispatches on the discriminant * key using a `json.RawMessage` probe (uniform for string, bool, and number * discriminant values). */ protected transpileUnion(data: ASTUnion): void; /** * Go intersection: struct embedding. * * type IntersectionItem struct { * ObjectItem * OtherObjectItem * } */ protected transpileIntersection(data: ASTIntersection): void; protected transpileStruct(data: ASTObject): void; /** Render a Go struct body for an ASTObject. */ private _transpileStructBody; /** Render a single struct field: `FieldName Type \`json:"key"\`` */ private _transpileMember; private _buildJsonTag; }