(
typeName: string,
variants: JSONSchema7[],
ctx: GoCodegenCtx,
description?: string,
experimental = false
)
| 2325 | } |
| 2326 | |
| 2327 | function emitGoFlattenedObjectUnion( |
| 2328 | typeName: string, |
| 2329 | variants: JSONSchema7[], |
| 2330 | ctx: GoCodegenCtx, |
| 2331 | description?: string, |
| 2332 | experimental = false |
| 2333 | ): void { |
| 2334 | if (ctx.generatedNames.has(typeName)) return; |
| 2335 | ctx.generatedNames.add(typeName); |
| 2336 | |
| 2337 | const objectVariants = variants |
| 2338 | .map((variant) => goObjectUnionMemberSchema(variant, ctx)) |
| 2339 | .filter((variant): variant is JSONSchema7 => variant !== undefined); |
| 2340 | const allProps = new Map<string, { schemas: JSONSchema7[]; requiredInAll: boolean; presentCount: number }>(); |
| 2341 | |
| 2342 | for (const variant of objectVariants) { |
| 2343 | const required = new Set(variant.required || []); |
| 2344 | for (const [propName, propSchema] of Object.entries(variant.properties || {})) { |
| 2345 | if (typeof propSchema !== "object") continue; |
| 2346 | const existing = allProps.get(propName); |
| 2347 | if (existing) { |
| 2348 | existing.schemas.push(propSchema as JSONSchema7); |
| 2349 | existing.presentCount++; |
| 2350 | if (!required.has(propName)) { |
| 2351 | existing.requiredInAll = false; |
| 2352 | } |
| 2353 | } else { |
| 2354 | allProps.set(propName, { |
| 2355 | schemas: [propSchema as JSONSchema7], |
| 2356 | requiredInAll: required.has(propName), |
| 2357 | presentCount: 1, |
| 2358 | }); |
| 2359 | } |
| 2360 | } |
| 2361 | } |
| 2362 | |
| 2363 | const lines: string[] = []; |
| 2364 | if (description) { |
| 2365 | pushGoCommentForContext(lines, description, ctx); |
| 2366 | } |
| 2367 | if (experimental) { |
| 2368 | pushGoExperimentalTypeComment(lines, typeName, ctx); |
| 2369 | } |
| 2370 | lines.push(`type ${typeName} struct {`); |
| 2371 | |
| 2372 | const fields: GoStructField[] = []; |
| 2373 | |
| 2374 | for (const [propName, info] of sortByGoFieldName([...allProps.entries()])) { |
| 2375 | const goName = toGoFieldName(propName); |
| 2376 | const mergedSchema = mergeGoFlattenedPropertySchema(typeName, propName, info.schemas, ctx); |
| 2377 | const requiredInAll = info.requiredInAll && info.presentCount === objectVariants.length; |
| 2378 | const goType = resolveGoPropertyType(mergedSchema, typeName, propName, requiredInAll, ctx); |
| 2379 | const description = info.schemas.find((schema) => schema.description)?.description; |
| 2380 | if (description) { |
| 2381 | pushGoCommentForContext(lines, description, ctx, "\t"); |
| 2382 | } |
| 2383 | if (info.schemas.some((schema) => isSchemaDeprecated(schema))) { |
| 2384 | pushGoCommentForContext(lines, `Deprecated: ${goName} is deprecated.`, ctx, "\t"); |
no test coverage detected
searching dependent graphs…