(typeName: string, schema: JSONSchema7, ctx: GoCodegenCtx, variants?: GoUntaggedUnionVariant[])
| 2709 | } |
| 2710 | |
| 2711 | function 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 | |
| 2765 | function planGoUnion(typeName: string, schema: JSONSchema7, ctx: GoCodegenCtx, includeWrapper: boolean = false): GoUnionPlan | undefined { |
| 2766 | const members = goNonNullUnionMembers(schema); |
no test coverage detected
searching dependent graphs…