MCPcopy Index your code
hub / github.com/github/copilot-sdk / emitGoUnionWrapperStruct

Function emitGoUnionWrapperStruct

scripts/codegen/go.ts:2838–2947  ·  view source on GitHub ↗
(typeName: string, schema: JSONSchema7, ctx: GoCodegenCtx)

Source from the content-addressed store, hash-verified

2836}
2837
2838function emitGoUnionWrapperStruct(typeName: string, schema: JSONSchema7, ctx: GoCodegenCtx): void {
2839 if (ctx.generatedNames.has(typeName)) return;
2840 ctx.generatedNames.add(typeName);
2841
2842 const members = goNonNullUnionMembers(schema);
2843 const lines: string[] = [];
2844 if (schema.description) {
2845 pushGoCommentForContext(lines, schema.description, ctx);
2846 }
2847 if (isSchemaExperimental(schema)) {
2848 pushGoExperimentalTypeComment(lines, typeName, ctx);
2849 }
2850 if (isSchemaDeprecated(schema)) {
2851 pushGoCommentForContext(lines, `Deprecated: ${typeName} is deprecated and will be removed in a future version.`, ctx);
2852 }
2853 lines.push(`type ${typeName} struct {`);
2854
2855 const emittedFields = new Set<string>();
2856 const fields: { name: string; type: string; member: JSONSchema7 }[] = [];
2857 for (const member of members) {
2858 const fieldNameBase = goUnionFieldName(member, ctx);
2859 let fieldName = fieldNameBase;
2860 let suffix = 2;
2861 while (emittedFields.has(fieldName)) {
2862 fieldName = `${fieldNameBase}${suffix++}`;
2863 }
2864 emittedFields.add(fieldName);
2865 const fieldType = goUnionFieldType(member, fieldName, typeName, ctx);
2866 fields.push({ name: fieldName, type: fieldType, member });
2867 }
2868
2869 fields.sort((left, right) => compareGoFieldNames(left.name, right.name));
2870 for (const field of fields) {
2871 lines.push(`\t${field.name} ${field.type}`);
2872 }
2873
2874 lines.push(`}`);
2875 const encodingLines: string[] = [];
2876 const matchFunctionsByField = new Map<string, string>();
2877 const objectVariantSchemas = fields.map((field) => ({
2878 field,
2879 schema: goObjectUnionMemberSchema(field.member, ctx),
2880 }));
2881 if (objectVariantSchemas.length > 1 && objectVariantSchemas.every((variant) => variant.schema !== undefined)) {
2882 const matchVariants: GoDiscriminatedUnionVariant[] = objectVariantSchemas.map(({ field, schema }) => ({
2883 schema: schema!,
2884 typeName: `${typeName}${field.name}`,
2885 discriminatorValues: [],
2886 }));
2887 for (const variant of matchVariants) {
2888 pushGoEncodingBlock(goVariantMatchFunctionLines(variant, matchVariants, "", ctx), ctx);
2889 }
2890 for (const [index, variant] of matchVariants.entries()) {
2891 matchFunctionsByField.set(objectVariantSchemas[index].field.name, goVariantMatchFuncName(variant.typeName));
2892 }
2893 }
2894 encodingLines.push(`func (r ${typeName}) MarshalJSON() ([]byte, error) {`);
2895 for (const field of fields) {

Callers 1

emitGoUnionPlanFunction · 0.85

Calls 15

goNonNullUnionMembersFunction · 0.85
pushGoCommentForContextFunction · 0.85
isSchemaExperimentalFunction · 0.85
isSchemaDeprecatedFunction · 0.85
goUnionFieldNameFunction · 0.85
goUnionFieldTypeFunction · 0.85
compareGoFieldNamesFunction · 0.85
pushGoEncodingBlockFunction · 0.85
goVariantMatchFuncNameFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…