Skip to content

Commit

Permalink
feat: json tag support for go union (#2048)
Browse files Browse the repository at this point in the history
  • Loading branch information
Souvikns authored Jun 20, 2024
1 parent abd8cbc commit d6a0bdf
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
5 changes: 2 additions & 3 deletions examples/go-union-type/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ type AdditionalProperty struct {
AdditionalPropertyOneOf_1
string
float64
ModelinaArrType []string
ModelinaArrType []Union
ModelinaArrType
AdditionalPropertyOneOf_6
}",
]
Expand All @@ -21,7 +20,7 @@ Array [
type Union struct {
string
float64
ModelinaAnyType interface{}
ModelinaAnyType
}",
]
`;
Expand Down
5 changes: 5 additions & 0 deletions src/generators/go/presets/CommonPreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,10 @@ export const GO_COMMON_PRESET: GoPreset<GoCommonPresetOptions> = {

return `${content}\n ${blocks.join('\n')}`;
}
},
union: {
field: ({ content }) => {
return `${content} \`json:"-,omitempty\``;
}
}
};
6 changes: 3 additions & 3 deletions src/generators/go/renderers/UnionRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ export const GO_DEFAULT_UNION_PRESET: UnionPresetType<GoOptions> = {
const fieldType = field.type;

if (fieldType === 'interface{}') {
return `${options.unionAnyModelName} ${fieldType}`;
return `${options.unionAnyModelName}`;
}
if (fieldType.includes('map')) {
return `${options.unionDictModelName} ${fieldType}`;
return `${options.unionDictModelName}`;
}
if (fieldType.includes('[]')) {
return `${options.unionArrModelName} ${fieldType}`;
return `${options.unionArrModelName}`;
}
return `${fieldType}`;
},
Expand Down
12 changes: 6 additions & 6 deletions test/generators/go/__snapshots__/GoGenerator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package some_package
type Union struct {
string
float64
ModelinaAnyType interface{}
ModelinaAnyType
}"
`;

Expand All @@ -30,7 +30,7 @@ package some_package
// AdditionalProperties represents a AdditionalProperties model.
type AdditionalProperties struct {
ModelinaAnyType interface{}
ModelinaAnyType
string
}"
`;
Expand Down Expand Up @@ -88,7 +88,7 @@ package some_package
type Union struct {
string
float64
ModelinaAnyType interface{}
ModelinaAnyType
}"
`;

Expand All @@ -98,7 +98,7 @@ package some_package
// AdditionalProperties represents a AdditionalProperties model.
type AdditionalProperties struct {
ModelinaAnyType interface{}
ModelinaAnyType
string
}"
`;
Expand Down Expand Up @@ -296,7 +296,7 @@ exports[`GoGenerator should render \`struct\` type 2`] = `
type Union struct {
string
float64
ModelinaAnyType interface{}
ModelinaAnyType
}"
`;

Expand Down Expand Up @@ -333,7 +333,7 @@ type Members struct {
type Union struct {
string
float64
ModelinaAnyType interface{}
ModelinaAnyType
}
// LocationAdditionalProperty represents a LocationAdditionalProperty model.
type LocationAdditionalProperty struct {
Expand Down

0 comments on commit d6a0bdf

Please sign in to comment.