diff --git a/Makefile b/Makefile index 440c0ea..420f306 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,6 @@ generate: root build .root/src/$(PKG)/tests/omitempty.go .root/bin/easyjson -all .root/src/$(PKG)/tests/data.go - .root/bin/easyjson -all .root/src/$(PKG)/tests/required.go .root/bin/easyjson -snake_case .root/src/$(PKG)/tests/snake.go .root/bin/easyjson -omit_empty .root/src/$(PKG)/tests/omitempty.go .root/bin/easyjson -build_tags=use_easyjson .root/src/$(PKG)/benchmark/data.go diff --git a/gen/decoder.go b/gen/decoder.go index f178d72..a1291fc 100644 --- a/gen/decoder.go +++ b/gen/decoder.go @@ -175,40 +175,35 @@ func (g *Generator) genStructFieldDecoder(t reflect.Type, f reflect.StructField) } if tags.required { - fmt.Fprintf(g.out, "%sSet = true\n", jsonName) + fmt.Fprintf(g.out, "%sSet = true\n", f.Name) } return nil } -func (g *Generator) getFieldSetBlock(t reflect.Type, f reflect.StructField) error { - jsonName := g.namer.GetJSONFieldName(t, f) +func (g *Generator) genRequiredFieldSet(t reflect.Type, f reflect.StructField) { tags := parseFieldTags(f) if !tags.required { - return nil + return } - fmt.Fprintf(g.out, "var %sSet bool\n", jsonName) - - return nil + fmt.Fprintf(g.out, "var %sSet bool\n", f.Name) } -func (g *Generator) getFieldCheckBlock(t reflect.Type, f reflect.StructField) error { +func (g *Generator) genRequiredFieldCheck(t reflect.Type, f reflect.StructField) { jsonName := g.namer.GetJSONFieldName(t, f) tags := parseFieldTags(f) if !tags.required { - return nil + return } g.imports["fmt"] = "fmt" - fmt.Fprintf(g.out, "if !%sSet {\n", jsonName) + fmt.Fprintf(g.out, "if !%sSet {\n", f.Name) fmt.Fprintf(g.out, " in.AddError(fmt.Errorf(\"key '%s' is required\"))\n", jsonName) fmt.Fprintf(g.out, "}\n") - - return nil } func mergeStructFields(fields1, fields2 []reflect.StructField) (fields []reflect.StructField) { @@ -294,9 +289,7 @@ func (g *Generator) genStructDecoder(t reflect.Type) error { } for _, f := range fs { - if err := g.getFieldSetBlock(t, f); err != nil { - return err - } + g.genRequiredFieldSet(t, f) } fmt.Fprintln(g.out, " in.Delim('{')") @@ -324,9 +317,7 @@ func (g *Generator) genStructDecoder(t reflect.Type) error { fmt.Fprintln(g.out, " in.Delim('}')") for _, f := range fs { - if err := g.getFieldCheckBlock(t, f); err != nil { - return err - } + g.genRequiredFieldCheck(t, f) } fmt.Fprintln(g.out, "}") diff --git a/tests/data.go b/tests/data.go index 73ced30..aa8963f 100644 --- a/tests/data.go +++ b/tests/data.go @@ -435,3 +435,8 @@ var mapsString = `{` + `"NilMap":null,` + `"CustomMap":{"c":"d"}` + `}` + +type RequiredOptionalStruct struct { + FirstName string `json:"first_name,required"` + Lastname string `json:"last_name"` +} diff --git a/tests/required.go b/tests/required.go deleted file mode 100644 index a83b907..0000000 --- a/tests/required.go +++ /dev/null @@ -1,7 +0,0 @@ -package tests - -//easyjson:json -type RequiredOptionalStruct struct { - FirstName string `json:"first_name,required"` - Lastname string `json:"last_name"` -}