Skip to content
Open
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
4 changes: 4 additions & 0 deletions bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Generator struct {
OmitEmpty bool
DisallowUnknownFields bool
SkipMemberNameUnescaping bool
AllowOmitEmptyStruct bool

OutName string
BuildTags string
Expand Down Expand Up @@ -137,6 +138,9 @@ func (g *Generator) writeMain() (path string, err error) {
if g.SkipMemberNameUnescaping {
fmt.Fprintln(f, " g.SkipMemberNameUnescaping()")
}
if g.AllowOmitEmptyStruct {
fmt.Fprintln(f, " g.AllowOmitEmptyStruct()")
}

sort.Strings(g.Types)
for _, v := range g.Types {
Expand Down
2 changes: 2 additions & 0 deletions easyjson/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var processPkg = flag.Bool("pkg", false, "process the whole package instead of j
var disallowUnknownFields = flag.Bool("disallow_unknown_fields", false, "return error if any unknown field in json appeared")
var skipMemberNameUnescaping = flag.Bool("disable_members_unescape", false, "don't perform unescaping of member names to improve performance")
var showVersion = flag.Bool("version", false, "print version and exit")
var allowOmitEmptyStruct = flag.Bool("allow_omitempty_struct", false, "skip empty struct fields if omitempty tag is present")

func generate(fname string) (err error) {
fInfo, err := os.Stat(fname)
Expand Down Expand Up @@ -85,6 +86,7 @@ func generate(fname string) (err error) {
NoStdMarshalers: *noStdMarshalers,
DisallowUnknownFields: *disallowUnknownFields,
SkipMemberNameUnescaping: *skipMemberNameUnescaping,
AllowOmitEmptyStruct: *allowOmitEmptyStruct,
OmitEmpty: *omitEmpty,
LeaveTemps: *leaveTemps,
OutName: outName,
Expand Down
15 changes: 14 additions & 1 deletion gen/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,15 @@ func (g *Generator) notEmptyCheck(t reflect.Type, v string) string {
switch t.Kind() {
case reflect.Slice, reflect.Map:
return "len(" + v + ") != 0"
case reflect.Struct:
if g.allowOmitEmptyStruct {
val, s := reflect.New(t).Elem(), "false"
for i := 0; i < val.NumField(); i++ {
s += " || " + g.notEmptyCheck(val.Field(i).Type(), v+"."+t.Field(i).Name)
}
return s
}
return "true"
case reflect.Interface, reflect.Ptr:
return v + " != nil"
case reflect.Bool:
Expand Down Expand Up @@ -339,7 +348,11 @@ func (g *Generator) genStructFieldEncoder(t reflect.Type, f reflect.StructField,
fmt.Fprintln(g.out, " {")
toggleFirstCondition = false
} else {
fmt.Fprintln(g.out, " if", g.notEmptyCheck(f.Type, "in."+f.Name), "{")
fmt.Fprintln(
g.out, " if",
strings.ReplaceAll(g.notEmptyCheck(f.Type, "in."+f.Name), "|| false", ""),
"{",
)
// can be any in runtime, so toggleFirstCondition stay as is
}

Expand Down
6 changes: 6 additions & 0 deletions gen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Generator struct {
fieldNamer FieldNamer
simpleBytes bool
skipMemberNameUnescaping bool
allowOmitEmptyStruct bool

// package path to local alias map for tracking imports
imports map[string]string
Expand Down Expand Up @@ -123,6 +124,11 @@ func (g *Generator) SkipMemberNameUnescaping() {
g.skipMemberNameUnescaping = true
}

// AllowOmitEmptyStruct instructs to allow struct field emptiness check if omitempty tag is provided.
func (g *Generator) AllowOmitEmptyStruct() {
g.allowOmitEmptyStruct = true
}

// OmitEmpty triggers `json=",omitempty"` behaviour by default.
func (g *Generator) OmitEmpty() {
g.omitEmpty = true
Expand Down