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

Function emitGoUntaggedUnionInterface

scripts/codegen/go.ts:2711–2763  ·  view source on GitHub ↗
(typeName: string, schema: JSONSchema7, ctx: GoCodegenCtx, variants?: GoUntaggedUnionVariant[])

Source from the content-addressed store, hash-verified

2709}
2710
2711function emitGoUntaggedUnionInterface(typeName: string, schema: JSONSchema7, ctx: GoCodegenCtx, variants?: GoUntaggedUnionVariant[]): boolean {
2712 if (ctx.generatedNames.has(typeName)) return true;
2713 variants ??= goUntaggedUnionVariants(typeName, schema, ctx);
2714 if (!variants) return false;
2715
2716 ctx.generatedNames.add(typeName);
2717 const unmarshalFuncName = goUnexportedFunctionName("unmarshal", typeName);
2718 const markerName = toGoUnexportedIdentifier(typeName);
2719 ctx.discriminatedUnions.set(typeName, { typeName, unmarshalFuncName });
2720
2721 const lines: string[] = [];
2722 if (schema.description) {
2723 pushGoCommentForContext(lines, schema.description, ctx);
2724 }
2725 if (isSchemaExperimental(schema)) {
2726 pushGoExperimentalTypeComment(lines, typeName, ctx);
2727 }
2728 if (isSchemaDeprecated(schema)) {
2729 pushGoCommentForContext(lines, `Deprecated: ${typeName} is deprecated and will be removed in a future version.`, ctx);
2730 }
2731 lines.push(`type ${typeName} interface {`);
2732 lines.push(`\t${markerName}()`);
2733 lines.push(`}`);
2734
2735 for (const variant of [...variants].sort((left, right) => compareGoTypeNames(left.typeName, right.typeName))) {
2736 lines.push(``);
2737 if (variant.typeDefinition) {
2738 lines.push(variant.typeDefinition);
2739 lines.push(``);
2740 }
2741 lines.push(`func (${variant.typeName}) ${markerName}() {}`);
2742 }
2743
2744 const unmarshalLines: string[] = [];
2745 unmarshalLines.push(`func ${unmarshalFuncName}(data []byte) (${typeName}, error) {`);
2746 unmarshalLines.push(`\tif string(data) == "null" {`);
2747 unmarshalLines.push(`\t\treturn nil, nil`);
2748 unmarshalLines.push(`\t}`);
2749 for (const variant of variants) {
2750 unmarshalLines.push(`\t{`);
2751 unmarshalLines.push(`\t\tvar value ${variant.goType}`);
2752 unmarshalLines.push(`\t\tif err := json.Unmarshal(data, &value); err == nil {`);
2753 unmarshalLines.push(`\t\t\treturn ${variant.returnExpr}, nil`);
2754 unmarshalLines.push(`\t\t}`);
2755 unmarshalLines.push(`\t}`);
2756 }
2757 unmarshalLines.push(`\treturn nil, errors.New("data did not match any union variant for ${typeName}")`);
2758 unmarshalLines.push(`}`);
2759 pushGoEncodingBlock(unmarshalLines, ctx);
2760
2761 ctx.structs.push(lines.join("\n"));
2762 return true;
2763}
2764
2765function planGoUnion(typeName: string, schema: JSONSchema7, ctx: GoCodegenCtx, includeWrapper: boolean = false): GoUnionPlan | undefined {
2766 const members = goNonNullUnionMembers(schema);

Callers 1

emitGoUnionPlanFunction · 0.85

Calls 13

goUntaggedUnionVariantsFunction · 0.85
goUnexportedFunctionNameFunction · 0.85
toGoUnexportedIdentifierFunction · 0.85
pushGoCommentForContextFunction · 0.85
isSchemaExperimentalFunction · 0.85
isSchemaDeprecatedFunction · 0.85
compareGoTypeNamesFunction · 0.85
pushGoEncodingBlockFunction · 0.85
joinMethod · 0.80
addMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…