diff --git a/gen/decoder.go b/gen/decoder.go index 2fece5d..600bb32 100644 --- a/gen/decoder.go +++ b/gen/decoder.go @@ -127,7 +127,9 @@ func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags field fmt.Fprintln(g.out, ws+" for !in.IsDelim(']') {") fmt.Fprintln(g.out, ws+" var "+tmpVar+" "+g.getType(elem)) - g.genTypeDecoder(elem, tmpVar, tags, indent+2) + 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()") @@ -159,7 +161,9 @@ func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags field fmt.Fprintln(g.out, ws+" for !in.IsDelim(']') {") fmt.Fprintln(g.out, ws+" if "+iterVar+" < "+fmt.Sprint(length)+" {") - g.genTypeDecoder(elem, out+"["+iterVar+"]", tags, indent+3) + 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 {") @@ -186,7 +190,9 @@ func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags field fmt.Fprintln(g.out, ws+" "+out+" = new("+g.getType(t.Elem())+")") fmt.Fprintln(g.out, ws+" }") - g.genTypeDecoder(t.Elem(), "*"+out, tags, indent+1) + if err := g.genTypeDecoder(t.Elem(), "*"+out, tags, indent+1); err != nil { + return err + } fmt.Fprintln(g.out, ws+"}") @@ -213,7 +219,9 @@ func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags field fmt.Fprintln(g.out, ws+" in.WantColon()") fmt.Fprintln(g.out, ws+" var "+tmpVar+" "+g.getType(elem)) - g.genTypeDecoder(elem, tmpVar, tags, indent+2) + 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()") diff --git a/gen/encoder.go b/gen/encoder.go index a54f6e2..e3f7c40 100644 --- a/gen/encoder.go +++ b/gen/encoder.go @@ -137,7 +137,9 @@ func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldT fmt.Fprintln(g.out, ws+" out.RawByte(',')") fmt.Fprintln(g.out, ws+" }") - g.genTypeEncoder(elem, vVar, tags, indent+2) + if err := g.genTypeEncoder(elem, vVar, tags, indent+2); err != nil { + return err + } fmt.Fprintln(g.out, ws+" }") fmt.Fprintln(g.out, ws+" out.RawByte(']')") @@ -157,7 +159,9 @@ func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldT fmt.Fprintln(g.out, ws+" out.RawByte(',')") fmt.Fprintln(g.out, ws+" }") - g.genTypeEncoder(elem, in+"["+iVar+"]", tags, indent+1) + if err := g.genTypeEncoder(elem, in+"["+iVar+"]", tags, indent+1); err != nil { + return err + } fmt.Fprintln(g.out, ws+"}") fmt.Fprintln(g.out, ws+"out.RawByte(']')") @@ -174,7 +178,9 @@ func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldT fmt.Fprintln(g.out, ws+` out.RawString("null")`) fmt.Fprintln(g.out, ws+"} else {") - g.genTypeEncoder(t.Elem(), "*"+in, tags, indent+1) + if err := g.genTypeEncoder(t.Elem(), "*"+in, tags, indent+1); err != nil { + return err + } fmt.Fprintln(g.out, ws+"}") @@ -196,7 +202,9 @@ func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldT fmt.Fprintln(g.out, ws+" out.String(string("+tmpVar+"Name))") fmt.Fprintln(g.out, ws+" out.RawByte(':')") - g.genTypeEncoder(t.Elem(), tmpVar+"Value", tags, indent+2) + if err := g.genTypeEncoder(t.Elem(), tmpVar+"Value", tags, indent+2); err != nil { + return err + } fmt.Fprintln(g.out, ws+" }") fmt.Fprintln(g.out, ws+" out.RawByte('}')") diff --git a/tests/non_string_keyed_map_test.go b/tests/non_string_keyed_map_test.go new file mode 100644 index 0000000..392b8f3 --- /dev/null +++ b/tests/non_string_keyed_map_test.go @@ -0,0 +1,44 @@ +package tests + +import ( + "os" + "testing" + + "github.com/mailru/easyjson/gen" +) + +type IntMap map[int]string +type IntMapSlice []IntMap +type IntMapArray [2]IntMap +type IntMapPtr *IntMap +type IntMapMap map[string]IntMap + +func TestNonStringKeyedtMapEncoder(t *testing.T) { + f := "non_string_keyed_map_easyjson.go" + for _, test := range []struct { + Data interface{} + }{ + { + Data: IntMap{}, + }, + { + Data: IntMapSlice{}, + }, + { + Data: IntMapArray{}, + }, + { + Data: IntMapPtr(nil), + }, + { + Data: IntMapMap{}, + }, + } { + g := gen.NewGenerator(f) + g.Add(test.Data) + e := g.Run(os.Stdout) + if e == nil { + t.Errorf("generation for %#v should have errored", test.Data) + } + } +}