diff --git a/gen/encoder.go b/gen/encoder.go index 5c29a8a..ebd01f8 100644 --- a/gen/encoder.go +++ b/gen/encoder.go @@ -290,32 +290,43 @@ func (g *Generator) notEmptyCheck(t reflect.Type, v string) string { } } -func (g *Generator) genStructFieldEncoder(t reflect.Type, f reflect.StructField) error { +func (g *Generator) genStructFieldEncoder(t reflect.Type, f reflect.StructField, first bool) (bool, error) { jsonName := g.fieldNamer.GetJSONFieldName(t, f) tags := parseFieldTags(f) if tags.omit { - return nil + return first, nil } + + toggleFirst := first + noOmitEmpty := (!tags.omitEmpty && !g.omitEmpty) || tags.noOmitEmpty if noOmitEmpty { fmt.Fprintln(g.out, " {") + toggleFirst = false } else { fmt.Fprintln(g.out, " if", g.notEmptyCheck(f.Type, "in."+f.Name), "{") + // can be any in runtime, so toggleFirst stay as is + } + + if first { + 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, " }") + } else { + fmt.Fprintf(g.out, " const prefix string = %q\n", ","+strconv.Quote(jsonName)+":") + fmt.Fprintln(g.out, " out.RawString(prefix)") } - 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 err := g.genTypeEncoder(f.Type, "in."+f.Name, tags, 2, !noOmitEmpty); err != nil { - return err + return toggleFirst, err } fmt.Fprintln(g.out, " }") - return nil + return toggleFirst, nil } func (g *Generator) genEncoder(t reflect.Type) error { @@ -363,8 +374,12 @@ func (g *Generator) genStructEncoder(t reflect.Type) error { if err != nil { return fmt.Errorf("cannot generate encoder for %v: %v", t, err) } + + first := true for _, f := range fs { - if err := g.genStructFieldEncoder(t, f); err != nil { + first, err = g.genStructFieldEncoder(t, f, first) + + if err != nil { return err } } diff --git a/gen/generator.go b/gen/generator.go index 13c54c4..4a8f5a8 100644 --- a/gen/generator.go +++ b/gen/generator.go @@ -156,8 +156,9 @@ func (g *Generator) printHeader() { fmt.Println("package ", g.pkgName) fmt.Println() - byAlias := map[string]string{} - var aliases []string + byAlias := make(map[string]string, len(g.imports)) + aliases := make([]string, 0, len(g.imports)) + for path, alias := range g.imports { aliases = append(aliases, alias) byAlias[alias] = path @@ -388,9 +389,9 @@ func (DefaultFieldNamer) GetJSONFieldName(t reflect.Type, f reflect.StructField) jsonName := strings.Split(f.Tag.Get("json"), ",")[0] if jsonName != "" { return jsonName - } else { - return f.Name } + + return f.Name } // LowerCamelCaseFieldNamer @@ -454,9 +455,9 @@ func (LowerCamelCaseFieldNamer) GetJSONFieldName(t reflect.Type, f reflect.Struc jsonName := strings.Split(f.Tag.Get("json"), ",")[0] if jsonName != "" { return jsonName - } else { - return lowerFirst(f.Name) } + + return lowerFirst(f.Name) } // SnakeCaseFieldNamer implements CamelCase to snake_case conversion for fields names.