Skip to content

Commit

Permalink
Add "noomitempty" norman tag
Browse files Browse the repository at this point in the history
Example usage:

  Labels map[string]string `json:"labels" norman:"noomitempty"`

Resulting API schema:
  "labels": {
    "create": true,
    "noOmitEmpty": true,
    "nullable": true,
    "type": "map[string]",
    "update": true
  }

Generated client code:

  Labels                         map[string]string                  `json:"labels" yaml:"labels"`

Without this, the field would normally have `,omitempty` as part of the
json and yaml tags.
  • Loading branch information
cmurphy committed May 18, 2021
1 parent 30f8d18 commit c685c62
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
10 changes: 6 additions & 4 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ var (
)

type fieldInfo struct {
Name string
Type string
Name string
Type string
NoOmitEmpty bool
}

func getGoType(field types.Field, schema *types.Schema, schemas *types.Schemas) string {
Expand Down Expand Up @@ -110,8 +111,9 @@ func getTypeMap(schema *types.Schema, schemas *types.Schemas) map[string]fieldIn
continue
}
result[field.CodeName] = fieldInfo{
Name: name,
Type: getGoType(field, schema, schemas),
Name: name,
Type: getGoType(field, schema, schemas),
NoOmitEmpty: field.NoOmitEmpty,
}
}
return result
Expand Down
2 changes: 1 addition & 1 deletion generator/type_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type {{.schema.CodeName}} struct {
types.Resource
{{- end}}
{{- range $key, $value := .structFields}}
{{$key}} {{$value.Type}} %BACK%json:"{{$value.Name}},omitempty" yaml:"{{$value.Name}},omitempty"%BACK%
{{$key}} {{$value.Type}} %BACK%json:"{{$value.Name}}{{if not $value.NoOmitEmpty }},omitempty{{end}}" yaml:"{{$value.Name}}{{if not $value.NoOmitEmpty}},omitempty{{end}}"%BACK%
{{- end}}
}
Expand Down
2 changes: 2 additions & 0 deletions types/reflection.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ func applyTag(structField *reflect.StructField, field *Field) error {
field.ValidChars = value
case "invalidChars":
field.InvalidChars = value
case "noomitempty":
field.NoOmitEmpty = true
default:
return fmt.Errorf("invalid tag %s on field %s", key, structField.Name)
}
Expand Down
1 change: 1 addition & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ type Field struct {
Description string `json:"description,omitempty"`
CodeName string `json:"-"`
DynamicField bool `json:"dynamicField,omitempty"`
NoOmitEmpty bool `json:"noOmitEmpty,omitempty"`
}

type Action struct {
Expand Down

0 comments on commit c685c62

Please sign in to comment.