diff --git a/.github/workflows/easyjson.yml b/.github/workflows/easyjson.yml index 5f0b6e10..a4b397e5 100644 --- a/.github/workflows/easyjson.yml +++ b/.github/workflows/easyjson.yml @@ -2,9 +2,9 @@ name: easyjson on: push: - branches: [ master ] + branches: [ main ] pull_request: - branches: [ master ] + branches: [ main ] jobs: test: @@ -32,32 +32,3 @@ jobs: - name: Build and Run tests run: make - - test-non-amd64: - runs-on: ubuntu-latest - name: Test on ${{ matrix.distro }} ${{ matrix.arch }} - strategy: - matrix: - include: - - arch: ppc64le - distro: ubuntu20.04 - steps: - - uses: actions/checkout@v2 - - uses: uraimo/run-on-arch-action@master - with: - arch: ${{ matrix.arch }} - distro: ${{ matrix.distro }} - install: | - apt-get update - apt install -y curl wget make gcc - latestGo=$(curl "https://golang.org/VERSION?m=text") - wget --quiet "https://dl.google.com/go/${latestGo}.linux-${{ matrix.arch }}.tar.gz" - rm -f $(which go) - rm -rf /usr/local/go - tar -C /usr/local -xzf "${latestGo}.linux-${{ matrix.arch }}.tar.gz" - run: | - export PATH=/usr/local/go/bin:$PATH - export PATH=~/go/bin:$PATH - printf "Go Version: $(go version)\n" - go install golang.org/x/lint/golint@latest - make \ No newline at end of file diff --git a/bootstrap/bootstrap.go b/bootstrap/bootstrap.go index 9b870e9a..d97704ec 100644 --- a/bootstrap/bootstrap.go +++ b/bootstrap/bootstrap.go @@ -50,37 +50,37 @@ func (g *Generator) writeStub() error { if err != nil { return err } - defer f.Close() + defer func() { _ = f.Close() }() if g.BuildTags != "" { - fmt.Fprintln(f, "// +build ", g.BuildTags) - fmt.Fprintln(f) + _, _ = fmt.Fprintln(f, "// +build ", g.BuildTags) + _, _ = fmt.Fprintln(f) } - fmt.Fprintln(f, "// TEMPORARY AUTOGENERATED FILE: easyjson stub code to make the package") - fmt.Fprintln(f, "// compilable during generation.") - fmt.Fprintln(f) - fmt.Fprintln(f, "package ", g.PkgName) + _, _ = fmt.Fprintln(f, "// TEMPORARY AUTOGENERATED FILE: easyjson stub code to make the package") + _, _ = fmt.Fprintln(f, "// compilable during generation.") + _, _ = fmt.Fprintln(f) + _, _ = fmt.Fprintln(f, "package ", g.PkgName) if len(g.Types) > 0 { - fmt.Fprintln(f) - fmt.Fprintln(f, "import (") - fmt.Fprintln(f, ` "`+pkgWriter+`"`) - fmt.Fprintln(f, ` "`+pkgLexer+`"`) - fmt.Fprintln(f, ")") + _, _ = fmt.Fprintln(f) + _, _ = fmt.Fprintln(f, "import (") + _, _ = fmt.Fprintln(f, ` "`+pkgWriter+`"`) + _, _ = fmt.Fprintln(f, ` "`+pkgLexer+`"`) + _, _ = fmt.Fprintln(f, ")") } sort.Strings(g.Types) for _, t := range g.Types { - fmt.Fprintln(f) + _, _ = fmt.Fprintln(f) if !g.NoStdMarshalers { - fmt.Fprintln(f, "func (", t, ") MarshalJSON() ([]byte, error) { return nil, nil }") - fmt.Fprintln(f, "func (*", t, ") UnmarshalJSON([]byte) error { return nil }") + _, _ = fmt.Fprintln(f, "func (", t, ") MarshalJSON() ([]byte, error) { return nil, nil }") + _, _ = fmt.Fprintln(f, "func (*", t, ") UnmarshalJSON([]byte) error { return nil }") } - fmt.Fprintln(f, "func (", t, ") MarshalEasyJSON(w *jwriter.Writer) {}") - fmt.Fprintln(f, "func (*", t, ") UnmarshalEasyJSON(l *jlexer.Lexer) {}") - fmt.Fprintln(f) - fmt.Fprintln(f, "type EasyJSON_exporter_"+t+" *"+t) + _, _ = fmt.Fprintln(f, "func (", t, ") MarshalEasyJSON(w *jwriter.Writer) {}") + _, _ = fmt.Fprintln(f, "func (*", t, ") UnmarshalEasyJSON(l *jlexer.Lexer) {}") + _, _ = fmt.Fprintln(f) + _, _ = fmt.Fprintln(f, "type EasyJSON_exporter_"+t+" *"+t) } return nil } @@ -92,62 +92,62 @@ func (g *Generator) writeMain() (path string, err error) { return "", err } - fmt.Fprintln(f, "// +build ignore") - fmt.Fprintln(f) - fmt.Fprintln(f, "// TEMPORARY AUTOGENERATED FILE: easyjson bootstapping code to launch") - fmt.Fprintln(f, "// the actual generator.") - fmt.Fprintln(f) - fmt.Fprintln(f, "package main") - fmt.Fprintln(f) - fmt.Fprintln(f, "import (") - fmt.Fprintln(f, ` "fmt"`) - fmt.Fprintln(f, ` "os"`) - fmt.Fprintln(f) - fmt.Fprintf(f, " %q\n", genPackage) + _, _ = fmt.Fprintln(f, "// +build ignore") + _, _ = fmt.Fprintln(f) + _, _ = fmt.Fprintln(f, "// TEMPORARY AUTOGENERATED FILE: easyjson bootstapping code to launch") + _, _ = fmt.Fprintln(f, "// the actual generator.") + _, _ = fmt.Fprintln(f) + _, _ = fmt.Fprintln(f, "package main") + _, _ = fmt.Fprintln(f) + _, _ = fmt.Fprintln(f, "import (") + _, _ = fmt.Fprintln(f, ` "fmt"`) + _, _ = fmt.Fprintln(f, ` "os"`) + _, _ = fmt.Fprintln(f) + _, _ = fmt.Fprintf(f, " %q\n", genPackage) if len(g.Types) > 0 { - fmt.Fprintln(f) - fmt.Fprintf(f, " pkg %q\n", g.PkgPath) - } - fmt.Fprintln(f, ")") - fmt.Fprintln(f) - fmt.Fprintln(f, "func main() {") - fmt.Fprintf(f, " g := gen.NewGenerator(%q)\n", filepath.Base(g.OutName)) - fmt.Fprintf(f, " g.SetPkg(%q, %q)\n", g.PkgName, g.PkgPath) + _, _ = fmt.Fprintln(f) + _, _ = fmt.Fprintf(f, " pkg %q\n", g.PkgPath) + } + _, _ = fmt.Fprintln(f, ")") + _, _ = fmt.Fprintln(f) + _, _ = fmt.Fprintln(f, "func main() {") + _, _ = fmt.Fprintf(f, " g := gen.NewGenerator(%q)\n", filepath.Base(g.OutName)) + _, _ = fmt.Fprintf(f, " g.SetPkg(%q, %q)\n", g.PkgName, g.PkgPath) if g.BuildTags != "" { - fmt.Fprintf(f, " g.SetBuildTags(%q)\n", g.BuildTags) + _, _ = fmt.Fprintf(f, " g.SetBuildTags(%q)\n", g.BuildTags) } if g.SnakeCase { - fmt.Fprintln(f, " g.UseSnakeCase()") + _, _ = fmt.Fprintln(f, " g.UseSnakeCase()") } if g.LowerCamelCase { - fmt.Fprintln(f, " g.UseLowerCamelCase()") + _, _ = fmt.Fprintln(f, " g.UseLowerCamelCase()") } if g.OmitEmpty { - fmt.Fprintln(f, " g.OmitEmpty()") + _, _ = fmt.Fprintln(f, " g.OmitEmpty()") } if g.NoStdMarshalers { - fmt.Fprintln(f, " g.NoStdMarshalers()") + _, _ = fmt.Fprintln(f, " g.NoStdMarshalers()") } if g.DisallowUnknownFields { - fmt.Fprintln(f, " g.DisallowUnknownFields()") + _, _ = fmt.Fprintln(f, " g.DisallowUnknownFields()") } if g.SimpleBytes { - fmt.Fprintln(f, " g.SimpleBytes()") + _, _ = fmt.Fprintln(f, " g.SimpleBytes()") } if g.SkipMemberNameUnescaping { - fmt.Fprintln(f, " g.SkipMemberNameUnescaping()") + _, _ = fmt.Fprintln(f, " g.SkipMemberNameUnescaping()") } sort.Strings(g.Types) for _, v := range g.Types { - fmt.Fprintln(f, " g.Add(pkg.EasyJSON_exporter_"+v+"(nil))") + _, _ = fmt.Fprintln(f, " g.Add(pkg.EasyJSON_exporter_"+v+"(nil))") } - fmt.Fprintln(f, " if err := g.Run(os.Stdout); err != nil {") - fmt.Fprintln(f, " fmt.Fprintln(os.Stderr, err)") - fmt.Fprintln(f, " os.Exit(1)") - fmt.Fprintln(f, " }") - fmt.Fprintln(f, "}") + _, _ = fmt.Fprintln(f, " if err := g.Run(os.Stdout); err != nil {") + _, _ = fmt.Fprintln(f, " fmt.Fprintln(os.Stderr, err)") + _, _ = fmt.Fprintln(f, " os.Exit(1)") + _, _ = fmt.Fprintln(f, " }") + _, _ = fmt.Fprintln(f, "}") src := f.Name() if err := f.Close(); err != nil { @@ -171,7 +171,7 @@ func (g *Generator) Run() error { return err } if !g.LeaveTemps { - defer os.Remove(path) + defer func() { _ = os.Remove(path) }() } f, err := os.Create(g.OutName + ".tmp") @@ -179,7 +179,7 @@ func (g *Generator) Run() error { return err } if !g.LeaveTemps { - defer os.Remove(f.Name()) // will not remove after rename + defer func() { _ = os.Remove(f.Name()) }() // will not remove after rename } execArgs := []string{"run"} @@ -196,7 +196,7 @@ func (g *Generator) Run() error { if err = cmd.Run(); err != nil { return err } - f.Close() + _ = f.Close() // move unformatted file to out path if g.NoFormat { @@ -212,5 +212,5 @@ func (g *Generator) Run() error { if err != nil { return err } - return ioutil.WriteFile(g.OutName, out, 0644) + return ioutil.WriteFile(g.OutName, out, 0644) // #nosec G306 -- g.OutName is autogenerated code file, 0644 is fine. } diff --git a/buffer/pool.go b/buffer/pool.go index 18a911ee..bdd3010b 100644 --- a/buffer/pool.go +++ b/buffer/pool.go @@ -52,11 +52,11 @@ func putBuf(buf []byte) { // avoid an extra allocation. // We don't care about the length and we know the capability so // we don't need to save anything else. - c.Put(unsafe.Pointer(&buf[:1][0])) + c.Put(unsafe.Pointer(&buf[:1][0])) // #nosec G103 } } -const maxArraySize = uint((uint64(1) << 50 - 1) & uint64(^uint(0) >> 1)) +const maxArraySize = uint((uint64(1)<<50 - 1) & uint64(^uint(0)>>1)) // getBuf gets a chunk from reuse pool or creates a new one if reuse failed. func getBuf(size int) []byte { diff --git a/buffer/pool_test.go b/buffer/pool_test.go index 1f321d30..9d7b8477 100644 --- a/buffer/pool_test.go +++ b/buffer/pool_test.go @@ -94,7 +94,7 @@ func TestReadCloser(t *testing.T) { if err != nil { t.Errorf("ReadCloser() error: %v", err) } - rc.Close() // Will always return nil + _ = rc.Close() // Will always return nil got := out.Bytes() if !bytes.Equal(got, want) { diff --git a/easyjson/main.go b/easyjson/main.go index ef4a63bb..605de22b 100644 --- a/easyjson/main.go +++ b/easyjson/main.go @@ -112,7 +112,7 @@ func main() { for _, fname := range files { if err := generate(fname); err != nil { - fmt.Fprintln(os.Stderr, err) + _, _ = fmt.Fprintln(os.Stderr, err) os.Exit(1) } } diff --git a/gen/decoder.go b/gen/decoder.go index ad109ae2..0436ce53 100644 --- a/gen/decoder.go +++ b/gen/decoder.go @@ -63,23 +63,23 @@ func (g *Generator) genTypeDecoder(t reflect.Type, out string, tags fieldTags, i unmarshalerIface := reflect.TypeOf((*easyjson.Unmarshaler)(nil)).Elem() if reflect.PtrTo(t).Implements(unmarshalerIface) { - fmt.Fprintln(g.out, ws+"("+out+").UnmarshalEasyJSON(in)") + _, _ = fmt.Fprintln(g.out, ws+"("+out+").UnmarshalEasyJSON(in)") return nil } unmarshalerIface = reflect.TypeOf((*json.Unmarshaler)(nil)).Elem() if reflect.PtrTo(t).Implements(unmarshalerIface) { - fmt.Fprintln(g.out, ws+"if data := in.Raw(); in.Ok() {") - fmt.Fprintln(g.out, ws+" in.AddError( ("+out+").UnmarshalJSON(data) )") - fmt.Fprintln(g.out, ws+"}") + _, _ = fmt.Fprintln(g.out, ws+"if data := in.Raw(); in.Ok() {") + _, _ = fmt.Fprintln(g.out, ws+" in.AddError( ("+out+").UnmarshalJSON(data) )") + _, _ = fmt.Fprintln(g.out, ws+"}") return nil } unmarshalerIface = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem() if reflect.PtrTo(t).Implements(unmarshalerIface) { - fmt.Fprintln(g.out, ws+"if data := in.UnsafeBytes(); in.Ok() {") - fmt.Fprintln(g.out, ws+" in.AddError( ("+out+").UnmarshalText(data) )") - fmt.Fprintln(g.out, ws+"}") + _, _ = fmt.Fprintln(g.out, ws+"if data := in.UnsafeBytes(); in.Ok() {") + _, _ = fmt.Fprintln(g.out, ws+" in.AddError( ("+out+").UnmarshalText(data) )") + _, _ = fmt.Fprintln(g.out, ws+"}") return nil } @@ -110,13 +110,13 @@ func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags field ws := strings.Repeat(" ", indent) // Check whether type is primitive, needs to be done after interface check. if dec := customDecoders[t.String()]; dec != "" { - fmt.Fprintln(g.out, ws+out+" = "+dec) + _, _ = fmt.Fprintln(g.out, ws+out+" = "+dec) return nil } else if dec := primitiveStringDecoders[t.Kind()]; dec != "" && tags.asString { if tags.intern && t.Kind() == reflect.String { dec = "in.StringIntern()" } - fmt.Fprintln(g.out, ws+out+" = "+g.getType(t)+"("+dec+")") + _, _ = fmt.Fprintln(g.out, ws+out+" = "+g.getType(t)+"("+dec+")") return nil } else if dec := primitiveDecoders[t.Kind()]; dec != "" { if tags.intern && t.Kind() == reflect.String { @@ -125,7 +125,7 @@ func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags field if tags.noCopy && t.Kind() == reflect.String { dec = "in.UnsafeString()" } - fmt.Fprintln(g.out, ws+out+" = "+g.getType(t)+"("+dec+")") + _, _ = fmt.Fprintln(g.out, ws+out+" = "+g.getType(t)+"("+dec+")") return nil } @@ -135,17 +135,17 @@ func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags field elem := t.Elem() if elem.Kind() == reflect.Uint8 && elem.Name() == "uint8" { - fmt.Fprintln(g.out, ws+"if in.IsNull() {") - fmt.Fprintln(g.out, ws+" in.Skip()") - fmt.Fprintln(g.out, ws+" "+out+" = nil") - fmt.Fprintln(g.out, ws+"} else {") + _, _ = fmt.Fprintln(g.out, ws+"if in.IsNull() {") + _, _ = fmt.Fprintln(g.out, ws+" in.Skip()") + _, _ = fmt.Fprintln(g.out, ws+" "+out+" = nil") + _, _ = fmt.Fprintln(g.out, ws+"} else {") if g.simpleBytes { - fmt.Fprintln(g.out, ws+" "+out+" = []byte(in.String())") + _, _ = fmt.Fprintln(g.out, ws+" "+out+" = []byte(in.String())") } else { - fmt.Fprintln(g.out, ws+" "+out+" = in.Bytes()") + _, _ = fmt.Fprintln(g.out, ws+" "+out+" = in.Bytes()") } - fmt.Fprintln(g.out, ws+"}") + _, _ = fmt.Fprintln(g.out, ws+"}") } else { @@ -154,32 +154,32 @@ func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags field capacity = minSliceBytes / int(elem.Size()) } - fmt.Fprintln(g.out, ws+"if in.IsNull() {") - fmt.Fprintln(g.out, ws+" in.Skip()") - fmt.Fprintln(g.out, ws+" "+out+" = nil") - fmt.Fprintln(g.out, ws+"} else {") - fmt.Fprintln(g.out, ws+" in.Delim('[')") - fmt.Fprintln(g.out, ws+" if "+out+" == nil {") - fmt.Fprintln(g.out, ws+" if !in.IsDelim(']') {") - fmt.Fprintln(g.out, ws+" "+out+" = make("+g.getType(t)+", 0, "+fmt.Sprint(capacity)+")") - fmt.Fprintln(g.out, ws+" } else {") - fmt.Fprintln(g.out, ws+" "+out+" = "+g.getType(t)+"{}") - fmt.Fprintln(g.out, ws+" }") - fmt.Fprintln(g.out, ws+" } else { ") - fmt.Fprintln(g.out, ws+" "+out+" = ("+out+")[:0]") - fmt.Fprintln(g.out, ws+" }") - fmt.Fprintln(g.out, ws+" for !in.IsDelim(']') {") - fmt.Fprintln(g.out, ws+" var "+tmpVar+" "+g.getType(elem)) + _, _ = fmt.Fprintln(g.out, ws+"if in.IsNull() {") + _, _ = fmt.Fprintln(g.out, ws+" in.Skip()") + _, _ = fmt.Fprintln(g.out, ws+" "+out+" = nil") + _, _ = fmt.Fprintln(g.out, ws+"} else {") + _, _ = fmt.Fprintln(g.out, ws+" in.Delim('[')") + _, _ = fmt.Fprintln(g.out, ws+" if "+out+" == nil {") + _, _ = fmt.Fprintln(g.out, ws+" if !in.IsDelim(']') {") + _, _ = fmt.Fprintln(g.out, ws+" "+out+" = make("+g.getType(t)+", 0, "+fmt.Sprint(capacity)+")") + _, _ = fmt.Fprintln(g.out, ws+" } else {") + _, _ = fmt.Fprintln(g.out, ws+" "+out+" = "+g.getType(t)+"{}") + _, _ = fmt.Fprintln(g.out, ws+" }") + _, _ = fmt.Fprintln(g.out, ws+" } else { ") + _, _ = fmt.Fprintln(g.out, ws+" "+out+" = ("+out+")[:0]") + _, _ = fmt.Fprintln(g.out, ws+" }") + _, _ = fmt.Fprintln(g.out, ws+" for !in.IsDelim(']') {") + _, _ = fmt.Fprintln(g.out, ws+" var "+tmpVar+" "+g.getType(elem)) if err := g.genTypeDecoder(elem, tmpVar, tags, indent+2); err != nil { return err } - fmt.Fprintln(g.out, ws+" "+out+" = append("+out+", "+tmpVar+")") - fmt.Fprintln(g.out, ws+" in.WantComma()") - fmt.Fprintln(g.out, ws+" }") - fmt.Fprintln(g.out, ws+" in.Delim(']')") - fmt.Fprintln(g.out, ws+"}") + _, _ = fmt.Fprintln(g.out, ws+" "+out+" = append("+out+", "+tmpVar+")") + _, _ = fmt.Fprintln(g.out, ws+" in.WantComma()") + _, _ = fmt.Fprintln(g.out, ws+" }") + _, _ = fmt.Fprintln(g.out, ws+" in.Delim(']')") + _, _ = fmt.Fprintln(g.out, ws+"}") } case reflect.Array: @@ -187,36 +187,36 @@ func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags field elem := t.Elem() if elem.Kind() == reflect.Uint8 && elem.Name() == "uint8" { - fmt.Fprintln(g.out, ws+"if in.IsNull() {") - fmt.Fprintln(g.out, ws+" in.Skip()") - fmt.Fprintln(g.out, ws+"} else {") - fmt.Fprintln(g.out, ws+" copy("+out+"[:], in.Bytes())") - fmt.Fprintln(g.out, ws+"}") + _, _ = fmt.Fprintln(g.out, ws+"if in.IsNull() {") + _, _ = fmt.Fprintln(g.out, ws+" in.Skip()") + _, _ = fmt.Fprintln(g.out, ws+"} else {") + _, _ = fmt.Fprintln(g.out, ws+" copy("+out+"[:], in.Bytes())") + _, _ = fmt.Fprintln(g.out, ws+"}") } else { length := t.Len() - fmt.Fprintln(g.out, ws+"if in.IsNull() {") - fmt.Fprintln(g.out, ws+" in.Skip()") - fmt.Fprintln(g.out, ws+"} else {") - fmt.Fprintln(g.out, ws+" in.Delim('[')") - fmt.Fprintln(g.out, ws+" "+iterVar+" := 0") - fmt.Fprintln(g.out, ws+" for !in.IsDelim(']') {") - fmt.Fprintln(g.out, ws+" if "+iterVar+" < "+fmt.Sprint(length)+" {") + _, _ = fmt.Fprintln(g.out, ws+"if in.IsNull() {") + _, _ = fmt.Fprintln(g.out, ws+" in.Skip()") + _, _ = fmt.Fprintln(g.out, ws+"} else {") + _, _ = fmt.Fprintln(g.out, ws+" in.Delim('[')") + _, _ = fmt.Fprintln(g.out, ws+" "+iterVar+" := 0") + _, _ = fmt.Fprintln(g.out, ws+" for !in.IsDelim(']') {") + _, _ = fmt.Fprintln(g.out, ws+" if "+iterVar+" < "+fmt.Sprint(length)+" {") if err := g.genTypeDecoder(elem, "("+out+")["+iterVar+"]", tags, indent+3); err != nil { return err } - fmt.Fprintln(g.out, ws+" "+iterVar+"++") - fmt.Fprintln(g.out, ws+" } else {") - fmt.Fprintln(g.out, ws+" in.SkipRecursive()") - fmt.Fprintln(g.out, ws+" }") - fmt.Fprintln(g.out, ws+" in.WantComma()") - fmt.Fprintln(g.out, ws+" }") - fmt.Fprintln(g.out, ws+" in.Delim(']')") - fmt.Fprintln(g.out, ws+"}") + _, _ = fmt.Fprintln(g.out, ws+" "+iterVar+"++") + _, _ = fmt.Fprintln(g.out, ws+" } else {") + _, _ = fmt.Fprintln(g.out, ws+" in.SkipRecursive()") + _, _ = fmt.Fprintln(g.out, ws+" }") + _, _ = fmt.Fprintln(g.out, ws+" in.WantComma()") + _, _ = fmt.Fprintln(g.out, ws+" }") + _, _ = fmt.Fprintln(g.out, ws+" in.Delim(']')") + _, _ = fmt.Fprintln(g.out, ws+"}") } case reflect.Struct: @@ -225,25 +225,25 @@ func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags field if len(out) > 0 && out[0] == '*' { // NOTE: In order to remove an extra reference to a pointer - fmt.Fprintln(g.out, ws+dec+"(in, "+out[1:]+")") + _, _ = fmt.Fprintln(g.out, ws+dec+"(in, "+out[1:]+")") } else { - fmt.Fprintln(g.out, ws+dec+"(in, &"+out+")") + _, _ = fmt.Fprintln(g.out, ws+dec+"(in, &"+out+")") } case reflect.Ptr: - fmt.Fprintln(g.out, ws+"if in.IsNull() {") - fmt.Fprintln(g.out, ws+" in.Skip()") - fmt.Fprintln(g.out, ws+" "+out+" = nil") - fmt.Fprintln(g.out, ws+"} else {") - fmt.Fprintln(g.out, ws+" if "+out+" == nil {") - fmt.Fprintln(g.out, ws+" "+out+" = new("+g.getType(t.Elem())+")") - fmt.Fprintln(g.out, ws+" }") + _, _ = fmt.Fprintln(g.out, ws+"if in.IsNull() {") + _, _ = fmt.Fprintln(g.out, ws+" in.Skip()") + _, _ = fmt.Fprintln(g.out, ws+" "+out+" = nil") + _, _ = fmt.Fprintln(g.out, ws+"} else {") + _, _ = fmt.Fprintln(g.out, ws+" if "+out+" == nil {") + _, _ = fmt.Fprintln(g.out, ws+" "+out+" = new("+g.getType(t.Elem())+")") + _, _ = fmt.Fprintln(g.out, ws+" }") if err := g.genTypeDecoder(t.Elem(), "*"+out, tags, indent+1); err != nil { return err } - fmt.Fprintln(g.out, ws+"}") + _, _ = fmt.Fprintln(g.out, ws+"}") case reflect.Map: key := t.Key() @@ -255,66 +255,66 @@ func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags field tmpVar := g.uniqueVarName() keepEmpty := tags.required || tags.noOmitEmpty || (!g.omitEmpty && !tags.omitEmpty) - fmt.Fprintln(g.out, ws+"if in.IsNull() {") - fmt.Fprintln(g.out, ws+" in.Skip()") - fmt.Fprintln(g.out, ws+"} else {") - fmt.Fprintln(g.out, ws+" in.Delim('{')") + _, _ = fmt.Fprintln(g.out, ws+"if in.IsNull() {") + _, _ = fmt.Fprintln(g.out, ws+" in.Skip()") + _, _ = fmt.Fprintln(g.out, ws+"} else {") + _, _ = fmt.Fprintln(g.out, ws+" in.Delim('{')") if !keepEmpty { - fmt.Fprintln(g.out, ws+" if !in.IsDelim('}') {") + _, _ = fmt.Fprintln(g.out, ws+" if !in.IsDelim('}') {") } - fmt.Fprintln(g.out, ws+" "+out+" = make("+g.getType(t)+")") + _, _ = fmt.Fprintln(g.out, ws+" "+out+" = make("+g.getType(t)+")") if !keepEmpty { - fmt.Fprintln(g.out, ws+" } else {") - fmt.Fprintln(g.out, ws+" "+out+" = nil") - fmt.Fprintln(g.out, ws+" }") + _, _ = fmt.Fprintln(g.out, ws+" } else {") + _, _ = fmt.Fprintln(g.out, ws+" "+out+" = nil") + _, _ = fmt.Fprintln(g.out, ws+" }") } - fmt.Fprintln(g.out, ws+" for !in.IsDelim('}') {") + _, _ = fmt.Fprintln(g.out, ws+" for !in.IsDelim('}') {") // NOTE: extra check for TextUnmarshaler. It overrides default methods. if reflect.PtrTo(key).Implements(reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()) { - fmt.Fprintln(g.out, ws+" var key "+g.getType(key)) - fmt.Fprintln(g.out, ws+"if data := in.UnsafeBytes(); in.Ok() {") - fmt.Fprintln(g.out, ws+" in.AddError(key.UnmarshalText(data) )") - fmt.Fprintln(g.out, ws+"}") + _, _ = fmt.Fprintln(g.out, ws+" var key "+g.getType(key)) + _, _ = fmt.Fprintln(g.out, ws+"if data := in.UnsafeBytes(); in.Ok() {") + _, _ = fmt.Fprintln(g.out, ws+" in.AddError(key.UnmarshalText(data) )") + _, _ = fmt.Fprintln(g.out, ws+"}") } else if keyDec != "" { - fmt.Fprintln(g.out, ws+" key := "+g.getType(key)+"("+keyDec+")") + _, _ = fmt.Fprintln(g.out, ws+" key := "+g.getType(key)+"("+keyDec+")") } else { - fmt.Fprintln(g.out, ws+" var key "+g.getType(key)) + _, _ = fmt.Fprintln(g.out, ws+" var key "+g.getType(key)) if err := g.genTypeDecoder(key, "key", tags, indent+2); err != nil { return err } } - fmt.Fprintln(g.out, ws+" in.WantColon()") - fmt.Fprintln(g.out, ws+" var "+tmpVar+" "+g.getType(elem)) + _, _ = fmt.Fprintln(g.out, ws+" in.WantColon()") + _, _ = fmt.Fprintln(g.out, ws+" var "+tmpVar+" "+g.getType(elem)) if err := g.genTypeDecoder(elem, tmpVar, tags, indent+2); err != nil { return err } - fmt.Fprintln(g.out, ws+" ("+out+")[key] = "+tmpVar) - fmt.Fprintln(g.out, ws+" in.WantComma()") - fmt.Fprintln(g.out, ws+" }") - fmt.Fprintln(g.out, ws+" in.Delim('}')") - fmt.Fprintln(g.out, ws+"}") + _, _ = fmt.Fprintln(g.out, ws+" ("+out+")[key] = "+tmpVar) + _, _ = fmt.Fprintln(g.out, ws+" in.WantComma()") + _, _ = fmt.Fprintln(g.out, ws+" }") + _, _ = fmt.Fprintln(g.out, ws+" in.Delim('}')") + _, _ = fmt.Fprintln(g.out, ws+"}") case reflect.Interface: if t.NumMethod() != 0 { if g.interfaceIsEasyjsonUnmarshaller(t) { - fmt.Fprintln(g.out, ws+out+".UnmarshalEasyJSON(in)") + _, _ = fmt.Fprintln(g.out, ws+out+".UnmarshalEasyJSON(in)") } else if g.interfaceIsJsonUnmarshaller(t) { - fmt.Fprintln(g.out, ws+out+".UnmarshalJSON(in.Raw())") + _, _ = fmt.Fprintln(g.out, ws+out+".UnmarshalJSON(in.Raw())") } else { return fmt.Errorf("interface type %v not supported: only interface{} and easyjson/json Unmarshaler are allowed", t) } } else { - fmt.Fprintln(g.out, ws+"if m, ok := "+out+".(easyjson.Unmarshaler); ok {") - fmt.Fprintln(g.out, ws+"m.UnmarshalEasyJSON(in)") - fmt.Fprintln(g.out, ws+"} else if m, ok := "+out+".(json.Unmarshaler); ok {") - fmt.Fprintln(g.out, ws+"_ = m.UnmarshalJSON(in.Raw())") - fmt.Fprintln(g.out, ws+"} else {") - fmt.Fprintln(g.out, ws+" "+out+" = in.Interface()") - fmt.Fprintln(g.out, ws+"}") + _, _ = fmt.Fprintln(g.out, ws+"if m, ok := "+out+".(easyjson.Unmarshaler); ok {") + _, _ = fmt.Fprintln(g.out, ws+"m.UnmarshalEasyJSON(in)") + _, _ = fmt.Fprintln(g.out, ws+"} else if m, ok := "+out+".(json.Unmarshaler); ok {") + _, _ = fmt.Fprintln(g.out, ws+"_ = m.UnmarshalJSON(in.Raw())") + _, _ = fmt.Fprintln(g.out, ws+"} else {") + _, _ = fmt.Fprintln(g.out, ws+" "+out+" = in.Interface()") + _, _ = fmt.Fprintln(g.out, ws+"}") } default: return fmt.Errorf("don't know how to decode %v", t) @@ -342,13 +342,13 @@ func (g *Generator) genStructFieldDecoder(t reflect.Type, f reflect.StructField) return errors.New("Mutually exclusive tags are specified: 'intern' and 'nocopy'") } - fmt.Fprintf(g.out, " case %q:\n", jsonName) + _, _ = fmt.Fprintf(g.out, " case %q:\n", jsonName) if err := g.genTypeDecoder(f.Type, "out."+f.Name, tags, 3); err != nil { return err } if tags.required { - fmt.Fprintf(g.out, "%sSet = true\n", f.Name) + _, _ = fmt.Fprintf(g.out, "%sSet = true\n", f.Name) } return nil @@ -361,7 +361,7 @@ func (g *Generator) genRequiredFieldSet(t reflect.Type, f reflect.StructField) { return } - fmt.Fprintf(g.out, "var %sSet bool\n", f.Name) + _, _ = fmt.Fprintf(g.out, "var %sSet bool\n", f.Name) } func (g *Generator) genRequiredFieldCheck(t reflect.Type, f reflect.StructField) { @@ -374,9 +374,9 @@ func (g *Generator) genRequiredFieldCheck(t reflect.Type, f reflect.StructField) g.imports["fmt"] = "fmt" - 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") + _, _ = 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") } func mergeStructFields(fields1, fields2 []reflect.StructField) (fields []reflect.StructField) { @@ -460,16 +460,16 @@ func (g *Generator) genSliceArrayDecoder(t reflect.Type) error { fname := g.getDecoderName(t) typ := g.getType(t) - fmt.Fprintln(g.out, "func "+fname+"(in *jlexer.Lexer, out *"+typ+") {") - fmt.Fprintln(g.out, " isTopLevel := in.IsStart()") + _, _ = fmt.Fprintln(g.out, "func "+fname+"(in *jlexer.Lexer, out *"+typ+") {") + _, _ = fmt.Fprintln(g.out, " isTopLevel := in.IsStart()") err := g.genTypeDecoderNoCheck(t, "*out", fieldTags{}, 1) if err != nil { return err } - fmt.Fprintln(g.out, " if isTopLevel {") - fmt.Fprintln(g.out, " in.Consumed()") - fmt.Fprintln(g.out, " }") - fmt.Fprintln(g.out, "}") + _, _ = fmt.Fprintln(g.out, " if isTopLevel {") + _, _ = fmt.Fprintln(g.out, " in.Consumed()") + _, _ = fmt.Fprintln(g.out, " }") + _, _ = fmt.Fprintln(g.out, "}") return nil } @@ -482,15 +482,15 @@ func (g *Generator) genStructDecoder(t reflect.Type) error { fname := g.getDecoderName(t) typ := g.getType(t) - fmt.Fprintln(g.out, "func "+fname+"(in *jlexer.Lexer, out *"+typ+") {") - fmt.Fprintln(g.out, " isTopLevel := in.IsStart()") - fmt.Fprintln(g.out, " if in.IsNull() {") - fmt.Fprintln(g.out, " if isTopLevel {") - fmt.Fprintln(g.out, " in.Consumed()") - fmt.Fprintln(g.out, " }") - fmt.Fprintln(g.out, " in.Skip()") - fmt.Fprintln(g.out, " return") - fmt.Fprintln(g.out, " }") + _, _ = fmt.Fprintln(g.out, "func "+fname+"(in *jlexer.Lexer, out *"+typ+") {") + _, _ = fmt.Fprintln(g.out, " isTopLevel := in.IsStart()") + _, _ = fmt.Fprintln(g.out, " if in.IsNull() {") + _, _ = fmt.Fprintln(g.out, " if isTopLevel {") + _, _ = fmt.Fprintln(g.out, " in.Consumed()") + _, _ = fmt.Fprintln(g.out, " }") + _, _ = fmt.Fprintln(g.out, " in.Skip()") + _, _ = fmt.Fprintln(g.out, " return") + _, _ = fmt.Fprintln(g.out, " }") // Init embedded pointer fields. for i := 0; i < t.NumField(); i++ { @@ -498,7 +498,7 @@ func (g *Generator) genStructDecoder(t reflect.Type) error { if !f.Anonymous || f.Type.Kind() != reflect.Ptr { continue } - fmt.Fprintln(g.out, " out."+f.Name+" = new("+g.getType(f.Type.Elem())+")") + _, _ = fmt.Fprintln(g.out, " out."+f.Name+" = new("+g.getType(f.Type.Elem())+")") } fs, err := getStructFields(t) @@ -510,48 +510,48 @@ func (g *Generator) genStructDecoder(t reflect.Type) error { g.genRequiredFieldSet(t, f) } - fmt.Fprintln(g.out, " in.Delim('{')") - fmt.Fprintln(g.out, " for !in.IsDelim('}') {") - fmt.Fprintf(g.out, " key := in.UnsafeFieldName(%v)\n", g.skipMemberNameUnescaping) - fmt.Fprintln(g.out, " in.WantColon()") - fmt.Fprintln(g.out, " if in.IsNull() {") - fmt.Fprintln(g.out, " in.Skip()") - fmt.Fprintln(g.out, " in.WantComma()") - fmt.Fprintln(g.out, " continue") - fmt.Fprintln(g.out, " }") - - fmt.Fprintln(g.out, " switch key {") + _, _ = fmt.Fprintln(g.out, " in.Delim('{')") + _, _ = fmt.Fprintln(g.out, " for !in.IsDelim('}') {") + _, _ = fmt.Fprintf(g.out, " key := in.UnsafeFieldName(%v)\n", g.skipMemberNameUnescaping) + _, _ = fmt.Fprintln(g.out, " in.WantColon()") + _, _ = fmt.Fprintln(g.out, " if in.IsNull() {") + _, _ = fmt.Fprintln(g.out, " in.Skip()") + _, _ = fmt.Fprintln(g.out, " in.WantComma()") + _, _ = fmt.Fprintln(g.out, " continue") + _, _ = fmt.Fprintln(g.out, " }") + + _, _ = fmt.Fprintln(g.out, " switch key {") for _, f := range fs { if err := g.genStructFieldDecoder(t, f); err != nil { return err } } - fmt.Fprintln(g.out, " default:") + _, _ = fmt.Fprintln(g.out, " default:") if g.disallowUnknownFields { - fmt.Fprintln(g.out, ` in.AddError(&jlexer.LexerError{ + _, _ = fmt.Fprintln(g.out, ` in.AddError(&jlexer.LexerError{ Offset: in.GetPos(), Reason: "unknown field", Data: key, })`) } else if hasUnknownsUnmarshaler(t) { - fmt.Fprintln(g.out, " out.UnmarshalUnknown(in, key)") + _, _ = fmt.Fprintln(g.out, " out.UnmarshalUnknown(in, key)") } else { - fmt.Fprintln(g.out, " in.SkipRecursive()") + _, _ = fmt.Fprintln(g.out, " in.SkipRecursive()") } - fmt.Fprintln(g.out, " }") - fmt.Fprintln(g.out, " in.WantComma()") - fmt.Fprintln(g.out, " }") - fmt.Fprintln(g.out, " in.Delim('}')") - fmt.Fprintln(g.out, " if isTopLevel {") - fmt.Fprintln(g.out, " in.Consumed()") - fmt.Fprintln(g.out, " }") + _, _ = fmt.Fprintln(g.out, " }") + _, _ = fmt.Fprintln(g.out, " in.WantComma()") + _, _ = fmt.Fprintln(g.out, " }") + _, _ = fmt.Fprintln(g.out, " in.Delim('}')") + _, _ = fmt.Fprintln(g.out, " if isTopLevel {") + _, _ = fmt.Fprintln(g.out, " in.Consumed()") + _, _ = fmt.Fprintln(g.out, " }") for _, f := range fs { g.genRequiredFieldCheck(t, f) } - fmt.Fprintln(g.out, "}") + _, _ = fmt.Fprintln(g.out, "}") return nil } @@ -567,18 +567,18 @@ func (g *Generator) genStructUnmarshaler(t reflect.Type) error { typ := g.getType(t) if !g.noStdMarshalers { - fmt.Fprintln(g.out, "// UnmarshalJSON supports json.Unmarshaler interface") - fmt.Fprintln(g.out, "func (v *"+typ+") UnmarshalJSON(data []byte) error {") - fmt.Fprintln(g.out, " r := jlexer.Lexer{Data: data}") - fmt.Fprintln(g.out, " "+fname+"(&r, v)") - fmt.Fprintln(g.out, " return r.Error()") - fmt.Fprintln(g.out, "}") + _, _ = fmt.Fprintln(g.out, "// UnmarshalJSON supports json.Unmarshaler interface") + _, _ = fmt.Fprintln(g.out, "func (v *"+typ+") UnmarshalJSON(data []byte) error {") + _, _ = fmt.Fprintln(g.out, " r := jlexer.Lexer{Data: data}") + _, _ = fmt.Fprintln(g.out, " "+fname+"(&r, v)") + _, _ = fmt.Fprintln(g.out, " return r.Error()") + _, _ = fmt.Fprintln(g.out, "}") } - fmt.Fprintln(g.out, "// UnmarshalEasyJSON supports easyjson.Unmarshaler interface") - fmt.Fprintln(g.out, "func (v *"+typ+") UnmarshalEasyJSON(l *jlexer.Lexer) {") - fmt.Fprintln(g.out, " "+fname+"(l, v)") - fmt.Fprintln(g.out, "}") + _, _ = fmt.Fprintln(g.out, "// UnmarshalEasyJSON supports easyjson.Unmarshaler interface") + _, _ = fmt.Fprintln(g.out, "func (v *"+typ+") UnmarshalEasyJSON(l *jlexer.Lexer) {") + _, _ = fmt.Fprintln(g.out, " "+fname+"(l, v)") + _, _ = fmt.Fprintln(g.out, "}") return nil } diff --git a/gen/encoder.go b/gen/encoder.go index a68d849f..d0812efb 100644 --- a/gen/encoder.go +++ b/gen/encoder.go @@ -96,19 +96,19 @@ func (g *Generator) genTypeEncoder(t reflect.Type, in string, tags fieldTags, in marshalerIface := reflect.TypeOf((*easyjson.Marshaler)(nil)).Elem() if reflect.PtrTo(t).Implements(marshalerIface) { - fmt.Fprintln(g.out, ws+"("+in+").MarshalEasyJSON(out)") + _, _ = fmt.Fprintln(g.out, ws+"("+in+").MarshalEasyJSON(out)") return nil } marshalerIface = reflect.TypeOf((*json.Marshaler)(nil)).Elem() if reflect.PtrTo(t).Implements(marshalerIface) { - fmt.Fprintln(g.out, ws+"out.Raw( ("+in+").MarshalJSON() )") + _, _ = fmt.Fprintln(g.out, ws+"out.Raw( ("+in+").MarshalJSON() )") return nil } marshalerIface = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem() if reflect.PtrTo(t).Implements(marshalerIface) { - fmt.Fprintln(g.out, ws+"out.RawText( ("+in+").MarshalText() )") + _, _ = fmt.Fprintln(g.out, ws+"out.RawText( ("+in+").MarshalText() )") return nil } @@ -130,12 +130,12 @@ func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldT // Check whether type is primitive, needs to be done after interface check. if enc := primitiveStringEncoders[t.Kind()]; enc != "" && tags.asString { - fmt.Fprintf(g.out, ws+enc+"\n", in) + _, _ = fmt.Fprintf(g.out, ws+enc+"\n", in) return nil } if enc := primitiveEncoders[t.Kind()]; enc != "" { - fmt.Fprintf(g.out, ws+enc+"\n", in) + _, _ = fmt.Fprintf(g.out, ws+enc+"\n", in) return nil } @@ -147,31 +147,31 @@ func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldT if t.Elem().Kind() == reflect.Uint8 && elem.Name() == "uint8" { if g.simpleBytes { - fmt.Fprintln(g.out, ws+"out.String(string("+in+"))") + _, _ = fmt.Fprintln(g.out, ws+"out.String(string("+in+"))") } else { - fmt.Fprintln(g.out, ws+"out.Base64Bytes("+in+")") + _, _ = fmt.Fprintln(g.out, ws+"out.Base64Bytes("+in+")") } } else { if !assumeNonEmpty { - fmt.Fprintln(g.out, ws+"if "+in+" == nil && (out.Flags & jwriter.NilSliceAsEmpty) == 0 {") - fmt.Fprintln(g.out, ws+` out.RawString("null")`) - fmt.Fprintln(g.out, ws+"} else {") + _, _ = fmt.Fprintln(g.out, ws+"if "+in+" == nil && (out.Flags & jwriter.NilSliceAsEmpty) == 0 {") + _, _ = fmt.Fprintln(g.out, ws+` out.RawString("null")`) + _, _ = fmt.Fprintln(g.out, ws+"} else {") } else { - fmt.Fprintln(g.out, ws+"{") + _, _ = fmt.Fprintln(g.out, ws+"{") } - fmt.Fprintln(g.out, ws+" out.RawByte('[')") - fmt.Fprintln(g.out, ws+" for "+iVar+", "+vVar+" := range "+in+" {") - fmt.Fprintln(g.out, ws+" if "+iVar+" > 0 {") - fmt.Fprintln(g.out, ws+" out.RawByte(',')") - fmt.Fprintln(g.out, ws+" }") + _, _ = fmt.Fprintln(g.out, ws+" out.RawByte('[')") + _, _ = fmt.Fprintln(g.out, ws+" for "+iVar+", "+vVar+" := range "+in+" {") + _, _ = fmt.Fprintln(g.out, ws+" if "+iVar+" > 0 {") + _, _ = fmt.Fprintln(g.out, ws+" out.RawByte(',')") + _, _ = fmt.Fprintln(g.out, ws+" }") if err := g.genTypeEncoder(elem, vVar, tags, indent+2, false); err != nil { return err } - fmt.Fprintln(g.out, ws+" }") - fmt.Fprintln(g.out, ws+" out.RawByte(']')") - fmt.Fprintln(g.out, ws+"}") + _, _ = fmt.Fprintln(g.out, ws+" }") + _, _ = fmt.Fprintln(g.out, ws+" out.RawByte(']')") + _, _ = fmt.Fprintln(g.out, ws+"}") } case reflect.Array: @@ -180,36 +180,36 @@ func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldT if t.Elem().Kind() == reflect.Uint8 && elem.Name() == "uint8" { if g.simpleBytes { - fmt.Fprintln(g.out, ws+"out.String(string("+in+"[:]))") + _, _ = fmt.Fprintln(g.out, ws+"out.String(string("+in+"[:]))") } else { - fmt.Fprintln(g.out, ws+"out.Base64Bytes("+in+"[:])") + _, _ = fmt.Fprintln(g.out, ws+"out.Base64Bytes("+in+"[:])") } } else { - fmt.Fprintln(g.out, ws+"out.RawByte('[')") - fmt.Fprintln(g.out, ws+"for "+iVar+" := range "+in+" {") - fmt.Fprintln(g.out, ws+" if "+iVar+" > 0 {") - fmt.Fprintln(g.out, ws+" out.RawByte(',')") - fmt.Fprintln(g.out, ws+" }") + _, _ = fmt.Fprintln(g.out, ws+"out.RawByte('[')") + _, _ = fmt.Fprintln(g.out, ws+"for "+iVar+" := range "+in+" {") + _, _ = fmt.Fprintln(g.out, ws+" if "+iVar+" > 0 {") + _, _ = fmt.Fprintln(g.out, ws+" out.RawByte(',')") + _, _ = fmt.Fprintln(g.out, ws+" }") if err := g.genTypeEncoder(elem, "("+in+")["+iVar+"]", tags, indent+1, false); err != nil { return err } - fmt.Fprintln(g.out, ws+"}") - fmt.Fprintln(g.out, ws+"out.RawByte(']')") + _, _ = fmt.Fprintln(g.out, ws+"}") + _, _ = fmt.Fprintln(g.out, ws+"out.RawByte(']')") } case reflect.Struct: enc := g.getEncoderName(t) g.addType(t) - fmt.Fprintln(g.out, ws+enc+"(out, "+in+")") + _, _ = fmt.Fprintln(g.out, ws+enc+"(out, "+in+")") case reflect.Ptr: if !assumeNonEmpty { - fmt.Fprintln(g.out, ws+"if "+in+" == nil {") - fmt.Fprintln(g.out, ws+` out.RawString("null")`) - fmt.Fprintln(g.out, ws+"} else {") + _, _ = fmt.Fprintln(g.out, ws+"if "+in+" == nil {") + _, _ = fmt.Fprintln(g.out, ws+` out.RawString("null")`) + _, _ = fmt.Fprintln(g.out, ws+"} else {") } if err := g.genTypeEncoder(t.Elem(), "*"+in, tags, indent+1, false); err != nil { @@ -217,7 +217,7 @@ func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldT } if !assumeNonEmpty { - fmt.Fprintln(g.out, ws+"}") + _, _ = fmt.Fprintln(g.out, ws+"}") } case reflect.Map: @@ -229,59 +229,59 @@ func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldT tmpVar := g.uniqueVarName() if !assumeNonEmpty { - fmt.Fprintln(g.out, ws+"if "+in+" == nil && (out.Flags & jwriter.NilMapAsEmpty) == 0 {") - fmt.Fprintln(g.out, ws+" out.RawString(`null`)") - fmt.Fprintln(g.out, ws+"} else {") + _, _ = fmt.Fprintln(g.out, ws+"if "+in+" == nil && (out.Flags & jwriter.NilMapAsEmpty) == 0 {") + _, _ = fmt.Fprintln(g.out, ws+" out.RawString(`null`)") + _, _ = fmt.Fprintln(g.out, ws+"} else {") } else { - fmt.Fprintln(g.out, ws+"{") + _, _ = fmt.Fprintln(g.out, ws+"{") } - fmt.Fprintln(g.out, ws+" out.RawByte('{')") - fmt.Fprintln(g.out, ws+" "+tmpVar+"First := true") - fmt.Fprintln(g.out, ws+" for "+tmpVar+"Name, "+tmpVar+"Value := range "+in+" {") - fmt.Fprintln(g.out, ws+" if "+tmpVar+"First { "+tmpVar+"First = false } else { out.RawByte(',') }") + _, _ = fmt.Fprintln(g.out, ws+" out.RawByte('{')") + _, _ = fmt.Fprintln(g.out, ws+" "+tmpVar+"First := true") + _, _ = fmt.Fprintln(g.out, ws+" for "+tmpVar+"Name, "+tmpVar+"Value := range "+in+" {") + _, _ = fmt.Fprintln(g.out, ws+" if "+tmpVar+"First { "+tmpVar+"First = false } else { out.RawByte(',') }") // NOTE: extra check for TextMarshaler. It overrides default methods. if reflect.PtrTo(key).Implements(reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()) { - fmt.Fprintln(g.out, ws+" "+fmt.Sprintf("out.RawText(("+tmpVar+"Name).MarshalText()"+")")) + _, _ = fmt.Fprintln(g.out, ws+" "+fmt.Sprintf("out.RawText(("+tmpVar+"Name).MarshalText()"+")")) } else if keyEnc != "" { - fmt.Fprintln(g.out, ws+" "+fmt.Sprintf(keyEnc, tmpVar+"Name")) + _, _ = fmt.Fprintln(g.out, ws+" "+fmt.Sprintf(keyEnc, tmpVar+"Name")) } else { if err := g.genTypeEncoder(key, tmpVar+"Name", tags, indent+2, false); err != nil { return err } } - fmt.Fprintln(g.out, ws+" out.RawByte(':')") + _, _ = fmt.Fprintln(g.out, ws+" out.RawByte(':')") if err := g.genTypeEncoder(t.Elem(), tmpVar+"Value", tags, indent+2, false); err != nil { return err } - fmt.Fprintln(g.out, ws+" }") - fmt.Fprintln(g.out, ws+" out.RawByte('}')") - fmt.Fprintln(g.out, ws+"}") + _, _ = fmt.Fprintln(g.out, ws+" }") + _, _ = fmt.Fprintln(g.out, ws+" out.RawByte('}')") + _, _ = fmt.Fprintln(g.out, ws+"}") case reflect.Interface: if t.NumMethod() != 0 { if g.interfaceIsEasyjsonMarshaller(t) { - fmt.Fprintln(g.out, ws+in+".MarshalEasyJSON(out)") + _, _ = fmt.Fprintln(g.out, ws+in+".MarshalEasyJSON(out)") } else if g.interfaceIsJSONMarshaller(t) { - fmt.Fprintln(g.out, ws+"if m, ok := "+in+".(easyjson.Marshaler); ok {") - fmt.Fprintln(g.out, ws+" m.MarshalEasyJSON(out)") - fmt.Fprintln(g.out, ws+"} else {") - fmt.Fprintln(g.out, ws+in+".MarshalJSON()") - fmt.Fprintln(g.out, ws+"}") + _, _ = fmt.Fprintln(g.out, ws+"if m, ok := "+in+".(easyjson.Marshaler); ok {") + _, _ = fmt.Fprintln(g.out, ws+" m.MarshalEasyJSON(out)") + _, _ = fmt.Fprintln(g.out, ws+"} else {") + _, _ = fmt.Fprintln(g.out, ws+in+".MarshalJSON()") + _, _ = fmt.Fprintln(g.out, ws+"}") } else { return fmt.Errorf("interface type %v not supported: only interface{} and interfaces that implement json or easyjson Marshaling are allowed", t) } } else { - fmt.Fprintln(g.out, ws+"if m, ok := "+in+".(easyjson.Marshaler); ok {") - fmt.Fprintln(g.out, ws+" m.MarshalEasyJSON(out)") - fmt.Fprintln(g.out, ws+"} else if m, ok := "+in+".(json.Marshaler); ok {") - fmt.Fprintln(g.out, ws+" out.Raw(m.MarshalJSON())") - fmt.Fprintln(g.out, ws+"} else {") - fmt.Fprintln(g.out, ws+" out.Raw(json.Marshal("+in+"))") - fmt.Fprintln(g.out, ws+"}") + _, _ = fmt.Fprintln(g.out, ws+"if m, ok := "+in+".(easyjson.Marshaler); ok {") + _, _ = fmt.Fprintln(g.out, ws+" m.MarshalEasyJSON(out)") + _, _ = fmt.Fprintln(g.out, ws+"} else if m, ok := "+in+".(json.Marshaler); ok {") + _, _ = fmt.Fprintln(g.out, ws+" out.Raw(m.MarshalJSON())") + _, _ = fmt.Fprintln(g.out, ws+"} else {") + _, _ = fmt.Fprintln(g.out, ws+" out.Raw(json.Marshal("+in+"))") + _, _ = fmt.Fprintln(g.out, ws+"}") } default: return fmt.Errorf("don't know how to encode %v", t) @@ -336,37 +336,37 @@ func (g *Generator) genStructFieldEncoder(t reflect.Type, f reflect.StructField, noOmitEmpty := (!tags.omitEmpty && !g.omitEmpty) || tags.noOmitEmpty if noOmitEmpty { - fmt.Fprintln(g.out, " {") + _, _ = fmt.Fprintln(g.out, " {") toggleFirstCondition = false } else { - fmt.Fprintln(g.out, " if", g.notEmptyCheck(f.Type, "in."+f.Name), "{") + _, _ = fmt.Fprintln(g.out, " if", g.notEmptyCheck(f.Type, "in."+f.Name), "{") // can be any in runtime, so toggleFirstCondition stay as is } if firstCondition { - fmt.Fprintf(g.out, " const prefix string = %q\n", ","+strconv.Quote(jsonName)+":") + _, _ = fmt.Fprintf(g.out, " const prefix string = %q\n", ","+strconv.Quote(jsonName)+":") if first { if !noOmitEmpty { - fmt.Fprintln(g.out, " first = false") + _, _ = fmt.Fprintln(g.out, " first = false") } - fmt.Fprintln(g.out, " out.RawString(prefix[1:])") + _, _ = 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, " }") + _, _ = 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, " out.RawString(prefix)") } if err := g.genTypeEncoder(f.Type, "in."+f.Name, tags, 2, !noOmitEmpty); err != nil { return toggleFirstCondition, err } - fmt.Fprintln(g.out, " }") + _, _ = fmt.Fprintln(g.out, " }") return toggleFirstCondition, nil } @@ -389,12 +389,12 @@ func (g *Generator) genSliceArrayMapEncoder(t reflect.Type) error { fname := g.getEncoderName(t) typ := g.getType(t) - fmt.Fprintln(g.out, "func "+fname+"(out *jwriter.Writer, in "+typ+") {") + _, _ = fmt.Fprintln(g.out, "func "+fname+"(out *jwriter.Writer, in "+typ+") {") err := g.genTypeEncoderNoCheck(t, "in", fieldTags{}, 1, false) if err != nil { return err } - fmt.Fprintln(g.out, "}") + _, _ = fmt.Fprintln(g.out, "}") return nil } @@ -406,10 +406,10 @@ func (g *Generator) genStructEncoder(t reflect.Type) error { fname := g.getEncoderName(t) typ := g.getType(t) - fmt.Fprintln(g.out, "func "+fname+"(out *jwriter.Writer, in "+typ+") {") - fmt.Fprintln(g.out, " out.RawByte('{')") - fmt.Fprintln(g.out, " first := true") - fmt.Fprintln(g.out, " _ = first") + _, _ = fmt.Fprintln(g.out, "func "+fname+"(out *jwriter.Writer, in "+typ+") {") + _, _ = fmt.Fprintln(g.out, " out.RawByte('{')") + _, _ = fmt.Fprintln(g.out, " first := true") + _, _ = fmt.Fprintln(g.out, " _ = first") fs, err := getStructFields(t) if err != nil { @@ -427,14 +427,14 @@ func (g *Generator) genStructEncoder(t reflect.Type) error { if hasUnknownsMarshaler(t) { if !firstCondition { - fmt.Fprintln(g.out, " in.MarshalUnknowns(out, false)") + _, _ = fmt.Fprintln(g.out, " in.MarshalUnknowns(out, false)") } else { - fmt.Fprintln(g.out, " in.MarshalUnknowns(out, first)") + _, _ = fmt.Fprintln(g.out, " in.MarshalUnknowns(out, first)") } } - fmt.Fprintln(g.out, " out.RawByte('}')") - fmt.Fprintln(g.out, "}") + _, _ = fmt.Fprintln(g.out, " out.RawByte('}')") + _, _ = fmt.Fprintln(g.out, "}") return nil } @@ -450,18 +450,18 @@ func (g *Generator) genStructMarshaler(t reflect.Type) error { typ := g.getType(t) if !g.noStdMarshalers { - fmt.Fprintln(g.out, "// MarshalJSON supports json.Marshaler interface") - fmt.Fprintln(g.out, "func (v "+typ+") MarshalJSON() ([]byte, error) {") - fmt.Fprintln(g.out, " w := jwriter.Writer{}") - fmt.Fprintln(g.out, " "+fname+"(&w, v)") - fmt.Fprintln(g.out, " return w.Buffer.BuildBytes(), w.Error") - fmt.Fprintln(g.out, "}") + _, _ = fmt.Fprintln(g.out, "// MarshalJSON supports json.Marshaler interface") + _, _ = fmt.Fprintln(g.out, "func (v "+typ+") MarshalJSON() ([]byte, error) {") + _, _ = fmt.Fprintln(g.out, " w := jwriter.Writer{}") + _, _ = fmt.Fprintln(g.out, " "+fname+"(&w, v)") + _, _ = fmt.Fprintln(g.out, " return w.Buffer.BuildBytes(), w.Error") + _, _ = fmt.Fprintln(g.out, "}") } - fmt.Fprintln(g.out, "// MarshalEasyJSON supports easyjson.Marshaler interface") - fmt.Fprintln(g.out, "func (v "+typ+") MarshalEasyJSON(w *jwriter.Writer) {") - fmt.Fprintln(g.out, " "+fname+"(w, v)") - fmt.Fprintln(g.out, "}") + _, _ = fmt.Fprintln(g.out, "// MarshalEasyJSON supports easyjson.Marshaler interface") + _, _ = fmt.Fprintln(g.out, "func (v "+typ+") MarshalEasyJSON(w *jwriter.Writer) {") + _, _ = fmt.Fprintln(g.out, " "+fname+"(w, v)") + _, _ = fmt.Fprintln(g.out, "}") return nil } diff --git a/gen/generator.go b/gen/generator.go index 9155d015..babc7aa4 100644 --- a/gen/generator.go +++ b/gen/generator.go @@ -75,7 +75,7 @@ func NewGenerator(filename string) *Generator { // Use a file-unique prefix on all auxiliary funcs to avoid // name clashes. hash := fnv.New32() - hash.Write([]byte(filename)) + _, _ = hash.Write([]byte(filename)) ret.hashString = fmt.Sprintf("%x", hash.Sum32()) return ret diff --git a/helpers.go b/helpers.go index 5e0e60ef..e11e4f70 100644 --- a/helpers.go +++ b/helpers.go @@ -44,7 +44,7 @@ type UnknownsMarshaler interface { } func isNilInterface(i interface{}) bool { - return (*[2]uintptr)(unsafe.Pointer(&i))[1] == 0 + return (*[2]uintptr)(unsafe.Pointer(&i))[1] == 0 // #nosec G103 } // Marshal returns data as a single byte slice. Method is suboptimal as the data is likely to be copied diff --git a/jlexer/bytestostr.go b/jlexer/bytestostr.go index e68108f8..2dde61d5 100644 --- a/jlexer/bytestostr.go +++ b/jlexer/bytestostr.go @@ -17,5 +17,5 @@ import ( // chunk may be either blocked from being freed by GC because of a single string or the buffer.Data // may be garbage-collected even when the string exists. func bytesToStr(data []byte) string { - return *(*string)(unsafe.Pointer(&data)) + return *(*string)(unsafe.Pointer(&data)) // #nosec G103 } diff --git a/parser/pkgpath.go b/parser/pkgpath.go index c045c33f..9ff33ea1 100644 --- a/parser/pkgpath.go +++ b/parser/pkgpath.go @@ -111,7 +111,7 @@ func getModulePath(goModPath string) string { pkgPathFromGoModCache.Unlock() }() - data, err := ioutil.ReadFile(goModPath) + data, err := ioutil.ReadFile(goModPath) // #nosec G304 -- Not a user input, used in auto code generation. if err != nil { return "" } diff --git a/tests/basic_test.go b/tests/basic_test.go index c7cee83b..131a5513 100644 --- a/tests/basic_test.go +++ b/tests/basic_test.go @@ -222,7 +222,7 @@ func TestNestedEasyJsonMarshal(t *testing.T) { Slice: []interface{}{n["Slice1"], n["Slice2"]}, Map: map[string]interface{}{"1": n["Map1"], "2": n["Map2"]}, } - easyjson.Marshal(ni) + _, _ = easyjson.Marshal(ni) for k, v := range n { if !v.EasilyMarshaled { @@ -244,7 +244,7 @@ func TestNestedMarshaler(t *testing.T) { t.Errorf("Can't marshal NestedMarshaler: %s", err) } - s2 := NestedMarshaler { + s2 := NestedMarshaler{ Value: &StructWithMarshaler{}, }