* Emit a Go struct definition from an object schema.
(
typeName: string,
schema: JSONSchema7,
ctx: GoCodegenCtx,
description?: string
)
| 1189 | * Emit a Go struct definition from an object schema. |
| 1190 | */ |
| 1191 | function 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 | |
| 1237 | function goObjectSchemaForMatch(schema: JSONSchema7, ctx: GoCodegenCtx): JSONSchema7 | undefined { |
| 1238 | const resolved = resolveSchema(schema, ctx.definitions) ?? schema; |
no test coverage detected
searching dependent graphs…