Merge pull request #144 from ChuntaoLu/lu.mk

[Bug Fix] Handle errors when generating encoder/decoder for nested structures
This commit is contained in:
Vasily Romanov
2017-10-17 11:39:07 +03:00
committed by GitHub
3 changed files with 68 additions and 8 deletions
+12 -4
View File
@@ -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()")
+12 -4
View File
@@ -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('}')")
+44
View File
@@ -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)
}
}
}