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

Function emitGoStruct

scripts/codegen/go.ts:1191–1235  ·  view source on GitHub ↗

* Emit a Go struct definition from an object schema.

(
    typeName: string,
    schema: JSONSchema7,
    ctx: GoCodegenCtx,
    description?: string
)

Source from the content-addressed store, hash-verified

1189 * Emit a Go struct definition from an object schema.
1190 */
1191function emitGoStruct(
1192 typeName: string,
1193 schema: JSONSchema7,
1194 ctx: GoCodegenCtx,
1195 description?: string
1196): void {
1197 if (ctx.generatedNames.has(typeName)) return;
1198 ctx.generatedNames.add(typeName);
1199
1200 const required = new Set(schema.required || []);
1201 const lines: string[] = [];
1202 const desc = description || schema.description;
1203 if (desc) {
1204 pushGoCommentForContext(lines, desc, ctx);
1205 }
1206 if (isSchemaExperimental(schema)) {
1207 pushGoExperimentalTypeComment(lines, typeName, ctx);
1208 }
1209 if (isSchemaDeprecated(schema)) {
1210 pushGoCommentForContext(lines, `Deprecated: ${typeName} is deprecated and will be removed in a future version.`, ctx);
1211 }
1212 lines.push(`type ${typeName} struct {`);
1213
1214 const fields: GoStructField[] = [];
1215
1216 for (const [propName, propSchema] of sortByGoFieldName(Object.entries(schema.properties || {}))) {
1217 if (typeof propSchema !== "object") continue;
1218 const prop = propSchema as JSONSchema7;
1219 const isReq = required.has(propName);
1220 const goName = toGoFieldName(propName);
1221 const goType = resolveGoPropertyType(prop, typeName, propName, isReq, ctx);
1222
1223 if (prop.description) {
1224 pushGoCommentForContext(lines, prop.description, ctx, "\t");
1225 }
1226 pushGoFieldMarkers(lines, prop, goName, ctx);
1227 const jsonTag = goJSONTag(propName, isReq, goType);
1228 lines.push(`\t${goName} ${goType} \`${jsonTag}\``);
1229 fields.push({ propName, goName, goType, jsonTag });
1230 }
1231
1232 lines.push(`}`);
1233 pushGoStructUnmarshalJSON(lines, typeName, fields, ctx);
1234 ctx.structs.push(lines.join("\n"));
1235}
1236
1237function goObjectSchemaForMatch(schema: JSONSchema7, ctx: GoCodegenCtx): JSONSchema7 | undefined {
1238 const resolved = resolveSchema(schema, ctx.definitions) ?? schema;

Callers 3

resolveGoPropertyTypeFunction · 0.85
goUntaggedUnionVariantFunction · 0.85
emitGoRpcDefinitionFunction · 0.85

Calls 13

pushGoCommentForContextFunction · 0.85
isSchemaExperimentalFunction · 0.85
isSchemaDeprecatedFunction · 0.85
sortByGoFieldNameFunction · 0.85
toGoFieldNameFunction · 0.85
resolveGoPropertyTypeFunction · 0.85
pushGoFieldMarkersFunction · 0.85
goJSONTagFunction · 0.85
joinMethod · 0.80
addMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…