Skip to content

Commit

Permalink
Merge pull request #235 from SenseyeDeveloper/master
Browse files Browse the repository at this point in the history
remove condition "if first" when is really first from "Code generated…
  • Loading branch information
rvasily authored Jun 20, 2019
2 parents 17beebf + 35d7922 commit da37f6c
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions gen/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit da37f6c

Please sign in to comment.