Skip to content

Commit

Permalink
Refactor: Extract method JsonAnnotation
Browse files Browse the repository at this point in the history
In some of the cases, we cannot omitempty. This refactor will allow us to have
greater control over such cases.
  • Loading branch information
isimluk committed Nov 27, 2020
1 parent 21116f0 commit c10fbbe
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 4 deletions.
4 changes: 4 additions & 0 deletions metaschema/parser/assembly.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ func (a *Assembly) XmlAnnotation() string {
return a.XmlGroupping() + a.XmlName() + ",omitempty"
}

func (a *Assembly) JsonAnnotation() string {
return a.JsonName() + ",omitempty"
}

func (a *Assembly) compile(metaschema *Metaschema) error {
if a.Ref != "" {
var err error
Expand Down
4 changes: 4 additions & 0 deletions metaschema/parser/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ func (f *Field) GoName() string {
return strcase.ToCamel(f.JsonName())
}

func (f *Field) JsonAnnotation() string {
return f.JsonName() + ",omitempty"
}

func (f *Field) JsonName() string {
if f.GroupAs != nil {
return f.GroupAs.Name
Expand Down
5 changes: 5 additions & 0 deletions metaschema/parser/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,18 @@ func (f *Flag) GoName() string {
func (f *Flag) JsonName() string {
return f.XmlName()
}

func (f *Flag) XmlName() string {
if f.Name != "" {
return f.Name
}
return f.Def.Name
}

func (f *Flag) JsonAnnotation() string {
return f.JsonName() + ",omitempty"
}

func (f *Flag) compile(metaschema *Metaschema) error {
var err error
if f.Ref != "" {
Expand Down
6 changes: 3 additions & 3 deletions metaschema/templates/generated_models.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ type {{.GoTypeName}} struct {
{{- end}}
{{- range .Flags}}
// {{ .GoComment }}
{{.GoName}} {{.GoDatatype}} `xml:"{{.XmlName}},attr,omitempty" json:"{{.JsonName}},omitempty"`
{{.GoName}} {{.GoDatatype}} `xml:"{{.XmlName}},attr,omitempty" json:"{{.JsonAnnotation}}"`
{{- end}}
{{if .Model}}
{{- range .Model.GoStructItems}}
// {{ .GoComment }}
{{.GoName}} {{.GoMemLayout}}{{.GoTypeNameMultiplexed}} `xml:"{{.XmlAnnotation}}" json:"{{.JsonName}},omitempty"`
{{.GoName}} {{.GoMemLayout}}{{.GoTypeNameMultiplexed}} `xml:"{{.XmlAnnotation}}" json:"{{.JsonAnnotation}}"`
{{- end}}
{{end}}

Expand All @@ -32,7 +32,7 @@ type {{.GoTypeName}} struct {
type {{.GoTypeName}} struct {
{{- range .Flags}}
// {{ .GoComment }}
{{.GoName}} {{.GoDatatype}} `xml:"{{.XmlName}},attr,omitempty" json:"{{.JsonName}},omitempty"`
{{.GoName}} {{.GoDatatype}} `xml:"{{.XmlName}},attr,omitempty" json:"{{.JsonAnnotation}}"`
{{end -}}

{{- if not .Empty -}}
Expand Down
2 changes: 1 addition & 1 deletion metaschema/templates/pkged.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c10fbbe

Please sign in to comment.