(definitions: Record<string, JSONSchema7>, definitionCollections: DefinitionCollections)
| 3057 | } |
| 3058 | |
| 3059 | function generateGoRpcTypeCode(definitions: Record<string, JSONSchema7>, definitionCollections: DefinitionCollections): GoGeneratedTypeCode { |
| 3060 | const ctx: GoCodegenCtx = { |
| 3061 | structs: [], |
| 3062 | encoding: [], |
| 3063 | enums: [], |
| 3064 | enumsByName: new Map(), |
| 3065 | discriminatedUnions: new Map(), |
| 3066 | generatedNames: new Set(), |
| 3067 | definitions: definitionCollections, |
| 3068 | packageName: "rpc", |
| 3069 | }; |
| 3070 | ctx.skipDefinitionTypeNames = collectGoDiscriminatedUnionVariantDefinitionTypeNames(definitions, ctx); |
| 3071 | const schemaKeysByTypeName = new Map<string, string>(); |
| 3072 | const entries = Object.entries(definitions) |
| 3073 | .sort(([left], [right]) => goDefinitionName(left).localeCompare(goDefinitionName(right))); |
| 3074 | |
| 3075 | for (const [definitionName, definition] of entries) { |
| 3076 | const typeName = goDefinitionName(definitionName); |
| 3077 | if (ctx.skipDefinitionTypeNames.has(typeName)) continue; |
| 3078 | const schemaKey = stableStringify(resolveSchema(definition, definitionCollections) ?? definition); |
| 3079 | const existingSchemaKey = schemaKeysByTypeName.get(typeName); |
| 3080 | if (existingSchemaKey && existingSchemaKey !== schemaKey) { |
| 3081 | throw new Error(`Conflicting Go RPC type name "${typeName}" for different schemas. Add a schema title/withTypeName to disambiguate.`); |
| 3082 | } |
| 3083 | schemaKeysByTypeName.set(typeName, schemaKey); |
| 3084 | emitGoRpcDefinition(definitionName, definition, ctx); |
| 3085 | } |
| 3086 | |
| 3087 | const lines: string[] = []; |
| 3088 | pushGoCodeBlocks(lines, sortedGoDeclaredTypeBlocks(ctx.structs)); |
| 3089 | pushGoCodeBlocks(lines, sortedGoDeclaredTypeBlocks(ctx.enums)); |
| 3090 | |
| 3091 | return { |
| 3092 | typeCode: joinGoCode(lines), |
| 3093 | encodingCode: goEncodingBlocksCode(ctx.encoding), |
| 3094 | discriminatedUnions: new Map(ctx.discriminatedUnions), |
| 3095 | }; |
| 3096 | } |
| 3097 | |
| 3098 | function goDeclaredTypeName(code: string): string { |
| 3099 | return /^type\s+(\w+)\b/m.exec(code)?.[1] ?? code; |
no test coverage detected
searching dependent graphs…