mirror of
https://github.com/netbirdio/easyjson.git
synced 2026-05-22 18:44:42 -07:00
Merge pull request #235 from SenseyeDeveloper/master
remove condition "if first" when is really first from "Code generated…
This commit is contained in:
+27
-18
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user