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: tidy up omitempty handling #20

Merged
merged 1 commit into from
Mar 6, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ To learn about more about the available options, run:
* Generates deterministic output based only on the determined structure of the
input, making it suitable for incorporation into build pipelines or detecting
schema changes.
* Generates `omitempty` when possible.
* Generates `,omitempty` tags.
* Generates `,string` tags.
* Uses the standard library's `time.Time` when possible.
* Gracefully handles properties with spaces that [cannot be unmarshalled by
Expand Down
12 changes: 6 additions & 6 deletions cmd/gojsonstruct/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var (
abbreviations = pflag.String("abbreviations", "", "comma-separated list of extra abbreviations")
format = pflag.String("format", "json", "format (json or yaml)")
decompress = pflag.Bool("z", false, "decompress input with gzip")
omitempty = pflag.String("omitempty", "auto", "generate omitempty (never, always, or auto)")
omitEmptyTags = pflag.String("omitempty-tags", "auto", "generate ,omitempty tags (never, always, or auto)")
packageComment = pflag.String("package-comment", "", "package comment")
packageName = pflag.String("package-name", "main", "package name")
skipUnparsableProperties = pflag.Bool("skip-unparsable-properties", true, "skip unparsable properties")
Expand All @@ -29,18 +29,18 @@ var (
goFormat = pflag.Bool("go-format", true, "format generated Go code")
output = pflag.String("o", "", "output filename")

omitEmptyOption = map[string]jsonstruct.OmitEmptyOption{
"never": jsonstruct.OmitEmptyNever,
"always": jsonstruct.OmitEmptyAlways,
"auto": jsonstruct.OmitEmptyAuto,
omitEmptyTagsType = map[string]jsonstruct.OmitEmptyTagsType{
"never": jsonstruct.OmitEmptyTagsNever,
"always": jsonstruct.OmitEmptyTagsAlways,
"auto": jsonstruct.OmitEmptyTagsAuto,
}
)

func run() error {
pflag.Parse()

options := []jsonstruct.GeneratorOption{
jsonstruct.WithOmitEmpty(omitEmptyOption[*omitempty]),
jsonstruct.WithOmitEmptyTags(omitEmptyTagsType[*omitEmptyTags]),
jsonstruct.WithSkipUnparsableProperties(*skipUnparsableProperties),
jsonstruct.WithStringTags(*stringTags),
jsonstruct.WithUseJSONNumber(*useJSONNumber),
Expand Down
24 changes: 12 additions & 12 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import (
// An ExportNameFunc returns the exported name for a property.
type ExportNameFunc func(string) string

// An OmitEmptyOption is an option for handling omitempty.
type OmitEmptyOption int
// An OmitEmptyTagsType sets how to handle ,omitempty tags.
type OmitEmptyTagsType int

// omitempty options.
// OmitEmptyTags values.
const (
OmitEmptyNever OmitEmptyOption = iota
OmitEmptyAlways
OmitEmptyAuto
OmitEmptyTagsNever OmitEmptyTagsType = iota
OmitEmptyTagsAlways
OmitEmptyTagsAuto
)

// A Generator generates Go types from observed values.
Expand All @@ -35,7 +35,7 @@ type Generator struct {
goFormat bool
imports map[string]struct{}
intType string
omitEmptyOption OmitEmptyOption
omitEmptyTags OmitEmptyTagsType
packageComment string
packageName string
skipUnparsableProperties bool
Expand Down Expand Up @@ -90,10 +90,10 @@ func WithIntType(intType string) GeneratorOption {
}
}

// WithOmitEmpty sets whether each field is tagged with omitempty.
func WithOmitEmpty(omitEmptyOption OmitEmptyOption) GeneratorOption {
// WithOmitEmptyTags sets whether ",omitempty" tags should be used.
func WithOmitEmptyTags(omitEmptyTags OmitEmptyTagsType) GeneratorOption {
return func(g *Generator) {
g.omitEmptyOption = omitEmptyOption
g.omitEmptyTags = omitEmptyTags
}
}

Expand Down Expand Up @@ -195,7 +195,7 @@ func NewGenerator(options ...GeneratorOption) *Generator {
goFormat: true,
imports: make(map[string]struct{}),
intType: "int",
omitEmptyOption: OmitEmptyAuto,
omitEmptyTags: OmitEmptyTagsAuto,
packageName: "main",
skipUnparsableProperties: true,
structTagNames: []string{"json"},
Expand Down Expand Up @@ -228,7 +228,7 @@ func (g *Generator) Generate() ([]byte, error) {
exportNameFunc: g.exportNameFunc,
imports: imports,
intType: g.intType,
omitEmptyOption: g.omitEmptyOption,
omitEmptyTags: g.omitEmptyTags,
skipUnparsableProperties: g.skipUnparsableProperties,
stringTags: g.stringTags,
structTagNames: g.structTagNames,
Expand Down
12 changes: 6 additions & 6 deletions generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ func TestGoType(t *testing.T) {
},
},
generatorOptions: []GeneratorOption{
WithOmitEmpty(OmitEmptyAlways),
WithOmitEmptyTags(OmitEmptyTagsAlways),
WithStringTags(true),
},
expectedGoTypeStr: "struct {\nKey string `json:\"key,omitempty\"`\n}",
Expand Down Expand Up @@ -463,7 +463,7 @@ func TestGoType(t *testing.T) {
},
},
generatorOptions: []GeneratorOption{
WithOmitEmpty(OmitEmptyAlways),
WithOmitEmptyTags(OmitEmptyTagsAlways),
WithStringTags(true),
},
expectedGoTypeStr: "struct {\nKey bool `json:\"key,omitempty,string\"`\n}",
Expand Down Expand Up @@ -832,7 +832,7 @@ func TestGoType(t *testing.T) {
},
},
generatorOptions: []GeneratorOption{
WithOmitEmpty(OmitEmptyAlways),
WithOmitEmptyTags(OmitEmptyTagsAlways),
},
expectedGoTypeStr: "struct {\nKey int `json:\"key,omitempty\"`\n}",
},
Expand Down Expand Up @@ -862,7 +862,7 @@ func TestGoType(t *testing.T) {
},
},
generatorOptions: []GeneratorOption{
WithOmitEmpty(OmitEmptyNever),
WithOmitEmptyTags(OmitEmptyTagsNever),
},
expectedGoTypeStr: "struct {\nKey int `json:\"key\"`\n}",
},
Expand Down Expand Up @@ -899,7 +899,7 @@ func TestGoType(t *testing.T) {
},
},
generatorOptions: []GeneratorOption{
WithOmitEmpty(OmitEmptyAuto),
WithOmitEmptyTags(OmitEmptyTagsAuto),
},
expectedGoTypeStr: "struct {\nKey1 int `json:\"key1\"`\nKey2 int `json:\"key2\"`\n}",
},
Expand All @@ -914,7 +914,7 @@ func TestGoType(t *testing.T) {
exportNameFunc: generator.exportNameFunc,
imports: make(map[string]struct{}),
intType: generator.intType,
omitEmptyOption: generator.omitEmptyOption,
omitEmptyTags: generator.omitEmptyTags,
skipUnparsableProperties: generator.skipUnparsableProperties,
stringTags: generator.stringTags,
structTagNames: generator.structTagNames,
Expand Down
8 changes: 4 additions & 4 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type generateOptions struct {
exportNameFunc ExportNameFunc
imports map[string]struct{}
intType string
omitEmptyOption OmitEmptyOption
omitEmptyTags OmitEmptyTagsType
skipUnparsableProperties bool
stringTags bool
structTagNames []string
Expand Down Expand Up @@ -258,11 +258,11 @@ func (v *value) goType(observations int, options *generateOptions) goType {
goType := v.objectProperties[property].goType(v.objects, options)
var omitEmpty bool
switch {
case options.omitEmptyOption == OmitEmptyNever:
case options.omitEmptyTags == OmitEmptyTagsNever:
omitEmpty = false
case options.omitEmptyOption == OmitEmptyAlways:
case options.omitEmptyTags == OmitEmptyTagsAlways:
omitEmpty = true
case options.omitEmptyOption == OmitEmptyAuto:
case options.omitEmptyTags == OmitEmptyTagsAuto:
omitEmpty = goType.omitEmpty
}

Expand Down