Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: json tag support for go union #2048

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading