From 35d79224b1c5044c4ff8424e3152ea9fe21139bb Mon Sep 17 00:00:00 2001 From: SenseyeDeveloper Date: Thu, 20 Jun 2019 13:41:30 +0300 Subject: [PATCH] remove condition "if first" when is really first from "Code generated by easyjson" --- gen/encoder.go | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/gen/encoder.go b/gen/encoder.go index ebd01f8..e86d531 100644 --- a/gen/encoder.go +++ b/gen/encoder.go @@ -126,7 +126,9 @@ func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldT if enc := primitiveStringEncoders[t.Kind()]; enc != "" && tags.asString { fmt.Fprintf(g.out, ws+enc+"\n", in) return nil - } else if enc := primitiveEncoders[t.Kind()]; enc != "" { + } + + if enc := primitiveEncoders[t.Kind()]; enc != "" { fmt.Fprintf(g.out, ws+enc+"\n", in) return nil } @@ -290,43 +292,50 @@ func (g *Generator) notEmptyCheck(t reflect.Type, v string) string { } } -func (g *Generator) genStructFieldEncoder(t reflect.Type, f reflect.StructField, first bool) (bool, error) { +func (g *Generator) genStructFieldEncoder(t reflect.Type, f reflect.StructField, first, firstCondition bool) (bool, error) { jsonName := g.fieldNamer.GetJSONFieldName(t, f) tags := parseFieldTags(f) if tags.omit { - return first, nil + return firstCondition, nil } - toggleFirst := first + toggleFirstCondition := firstCondition noOmitEmpty := (!tags.omitEmpty && !g.omitEmpty) || tags.noOmitEmpty if noOmitEmpty { fmt.Fprintln(g.out, " {") - toggleFirst = false + toggleFirstCondition = false } else { fmt.Fprintln(g.out, " if", g.notEmptyCheck(f.Type, "in."+f.Name), "{") - // can be any in runtime, so toggleFirst stay as is + // can be any in runtime, so toggleFirstCondition stay as is } - if first { + if firstCondition { fmt.Fprintf(g.out, " const prefix string = %q\n", ","+strconv.Quote(jsonName)+":") - fmt.Fprintln(g.out, " if first {") - fmt.Fprintln(g.out, " first = false") - fmt.Fprintln(g.out, " out.RawString(prefix[1:])") - fmt.Fprintln(g.out, " } else {") - fmt.Fprintln(g.out, " out.RawString(prefix)") - fmt.Fprintln(g.out, " }") + if first { + if !noOmitEmpty { + fmt.Fprintln(g.out, " first = false") + } + fmt.Fprintln(g.out, " out.RawString(prefix[1:])") + } else { + fmt.Fprintln(g.out, " if first {") + fmt.Fprintln(g.out, " first = false") + fmt.Fprintln(g.out, " out.RawString(prefix[1:])") + fmt.Fprintln(g.out, " } else {") + fmt.Fprintln(g.out, " out.RawString(prefix)") + fmt.Fprintln(g.out, " }") + } } else { fmt.Fprintf(g.out, " const prefix string = %q\n", ","+strconv.Quote(jsonName)+":") fmt.Fprintln(g.out, " out.RawString(prefix)") } if err := g.genTypeEncoder(f.Type, "in."+f.Name, tags, 2, !noOmitEmpty); err != nil { - return toggleFirst, err + return toggleFirstCondition, err } fmt.Fprintln(g.out, " }") - return toggleFirst, nil + return toggleFirstCondition, nil } func (g *Generator) genEncoder(t reflect.Type) error { @@ -375,9 +384,9 @@ func (g *Generator) genStructEncoder(t reflect.Type) error { return fmt.Errorf("cannot generate encoder for %v: %v", t, err) } - first := true - for _, f := range fs { - first, err = g.genStructFieldEncoder(t, f, first) + firstCondition := true + for i, f := range fs { + firstCondition, err = g.genStructFieldEncoder(t, f, i == 0, firstCondition) if err != nil { return err