From fe03749f5c53b0cf324459857a3103739907dae7 Mon Sep 17 00:00:00 2001 From: Adam Lehechka <42357034+alehechka@users.noreply.github.com> Date: Mon, 4 Jul 2022 14:55:16 -0500 Subject: [PATCH 1/3] jenshared omitempty tag generation --- jenshared/addStructs.go | 5 ++--- jenshared/addStructsFromJSON.go | 10 +++++----- jenshared/types.go | 21 +++++++++++++++++++-- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/jenshared/addStructs.go b/jenshared/addStructs.go index 53075c0..e400301 100644 --- a/jenshared/addStructs.go +++ b/jenshared/addStructs.go @@ -48,8 +48,7 @@ func createStructItem(item TypeItem) jen.Code { s.Id(item.Type) } - if item.Name != "" { - s.Tag(map[string]string{"json": item.Name}) - } + s.Tag(item.Tags()) + return s } diff --git a/jenshared/addStructsFromJSON.go b/jenshared/addStructsFromJSON.go index 142338b..3b6dbc4 100644 --- a/jenshared/addStructsFromJSON.go +++ b/jenshared/addStructsFromJSON.go @@ -23,7 +23,7 @@ func createTypeItemsMapFromJSON(data interface{}, config *Config) TypeItemsMap { func parseInterface(items TypeItemsMap, data interface{}, config *Config) TypeItemsMap { switch concreteVal := data.(type) { case bool, float64, string: - items[config.RootName] = TypeItems{{Name: config.RootName, Type: inferDataType(concreteVal, config)}} + items[config.RootName] = TypeItems{{Name: config.RootName, Type: inferDataType(concreteVal, config), OmitEmpty: config.OmitEmpty}} case map[string]interface{}: parseMap(items, concreteVal, config.RootName, config) case []interface{}: @@ -37,10 +37,10 @@ func diveTopLevelArray(items TypeItemsMap, data []interface{}, config *Config, a if len(data) > 0 { switch firstVal := data[0].(type) { case bool, float64, string: - items[config.RootName] = TypeItems{{Name: config.RootName, Type: fmt.Sprintf("%s%s", acc, inferDataType(firstVal, config))}} + items[config.RootName] = TypeItems{{Name: config.RootName, Type: fmt.Sprintf("%s%s", acc, inferDataType(firstVal, config)), OmitEmpty: config.OmitEmpty}} case map[string]interface{}: arrTitle := fmt.Sprintf("%sArray", config.RootName) - items[arrTitle] = TypeItems{{Name: arrTitle, Type: fmt.Sprintf("%s%s", acc, config.RootName)}} + items[arrTitle] = TypeItems{{Name: arrTitle, Type: fmt.Sprintf("%s%s", acc, config.RootName), OmitEmpty: config.OmitEmpty}} parseMap(items, firstVal, config.RootName, config) case []interface{}: @@ -60,10 +60,10 @@ func parseMap(items TypeItemsMap, data map[string]interface{}, parent string, co items[parent] = append(items[parent], TypeItem{Name: key, Type: title}) parseMap(items, concreteVal, title, config) case []interface{}: - items[parent] = append(items[parent], TypeItem{Name: key, Type: fmt.Sprintf("[]%s", title)}) + items[parent] = append(items[parent], TypeItem{Name: key, Type: fmt.Sprintf("[]%s", title), OmitEmpty: config.OmitEmpty}) parseFirstIndexArray(items, concreteVal, title, config) default: - items[parent] = append(items[parent], TypeItem{Name: key, Type: inferDataType(concreteVal, config)}) + items[parent] = append(items[parent], TypeItem{Name: key, Type: inferDataType(concreteVal, config), OmitEmpty: config.OmitEmpty}) } } return items diff --git a/jenshared/types.go b/jenshared/types.go index 6f61a5d..e1843f7 100644 --- a/jenshared/types.go +++ b/jenshared/types.go @@ -14,13 +14,15 @@ type Config struct { OutputFileName string OutputDirectory string TimeFormat string + OmitEmpty bool Debugger *log.Logger } // TypeItem represents a parsed JSON variable type TypeItem struct { - Name string - Type string + Name string + Type string + OmitEmpty bool } // Title converts the JSON name to TitleCase @@ -35,6 +37,21 @@ func (t TypeItem) Title() string { return strings.Title(str) } +// TagJSON prepares the tag for provided item +func (t TypeItem) TagJSON() string { + tags := []string{t.Name} + if t.OmitEmpty { + tags = append(tags, "omitempty") + } + + return strings.Join(tags, ",") +} + +// Tags creates tha Tags map +func (t TypeItem) Tags() map[string]string { + return map[string]string{"json": t.TagJSON()} +} + // TypeItems is an array of TypeItem objects type TypeItems []TypeItem From abc5542b9fc550363282bc04a235c794968b7e5f Mon Sep 17 00:00:00 2001 From: Adam Lehechka <42357034+alehechka@users.noreply.github.com> Date: Mon, 4 Jul 2022 14:55:32 -0500 Subject: [PATCH 2/3] omitempty CLI argument --- cmd/generate.go | 6 ++++++ gen/config.go | 2 ++ 2 files changed, 8 insertions(+) diff --git a/cmd/generate.go b/cmd/generate.go index 4648c2d..5f0d2a4 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -19,6 +19,7 @@ const ( quietFlag = "quiet" stdoutFlag = "out" timeFormatFlag = "time" + omitEmptyFlag = "omitempty" ) var generateFlags = []cli.Flag{ @@ -57,6 +58,10 @@ var generateFlags = []cli.Flag{ Usage: "Time format to use while parsing strings for potential time.Time variables. View time.Time constants for possible defaults: https://pkg.go.dev/time#pkg-constants", Value: time.RFC3339, }, + &cli.BoolFlag{ + Name: omitEmptyFlag, + Usage: "Appends the omitempty to all object variable tags.", + }, &cli.BoolFlag{ Name: debugFlag, Usage: "Log debug messages.", @@ -87,6 +92,7 @@ func generateTypes(ctx *cli.Context) (err error) { PackageName: ctx.String(packageFlag), OutputFileName: ctx.String(outputFileFlag), TimeFormat: ctx.String(timeFormatFlag), + OmitEmpty: ctx.Bool(omitEmptyFlag), } if ctx.Bool(stdoutFlag) { diff --git a/gen/config.go b/gen/config.go index dad05f5..ebb8f3b 100644 --- a/gen/config.go +++ b/gen/config.go @@ -19,6 +19,7 @@ type Config struct { PackageName string OutputFileName string TimeFormat string + OmitEmpty bool } func (c *Config) toJensharedConfig() *jenshared.Config { @@ -37,6 +38,7 @@ func (c *Config) toJensharedConfig() *jenshared.Config { OutputFileName: c.OutputFileName, OutputDirectory: dir, TimeFormat: c.getTimeFormat(), + OmitEmpty: c.OmitEmpty, Debugger: c.Debugger, } } From 640414bb090fc77f0f2a958594a1824ac7e6569c Mon Sep 17 00:00:00 2001 From: Adam Lehechka <42357034+alehechka@users.noreply.github.com> Date: Mon, 4 Jul 2022 14:56:47 -0500 Subject: [PATCH 3/3] add omitempty cli arg to README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f93af92..20f4a99 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ json2go generate --url="https://gorest.co.in/public/v2/users" | Package Name | `--package=api` | `string` | Name of package to generate types into. A nested package path is valid | `main` | | Output File Name | `--output` | `string` | The name of the file that is generated. If a file is provided as input, will use matching name unless explicitly provided. The ".go" extension is not required and will be automatically appended. | `types.go` | | Time Format | `--time=2006-01-02` | `string` | Time format to use while parsing strings for potential time.Time variables. View time.Time constants for possible defaults: https://pkg.go.dev/time#pkg-constants | `RFC3339` | +| Omit Empty | `--omitempty` | `bool` | Appends the omitempty to all object variable tags. | `false` | | Debug logging | `--debug` | `bool` | Will output debugging console logs. | `false` | | Quiet | `--quiet` | `bool` | Will quiet fatal errors. | `false` | | STDOUT | `--out` | `bool` | Instead of generating a Go file, will instead print the contents to STDOUT | `false` |