Fixes #20: Support non-builtin types for maps.

This commit is contained in:
Victor Starodub
2016-04-15 15:56:29 +03:00
parent acb943cc36
commit 5a0d3f3b2a
3 changed files with 11 additions and 4 deletions
+2 -2
View File
@@ -113,13 +113,13 @@ func (g *Generator) genTypeDecoder(t reflect.Type, out string, indent int) error
fmt.Fprintln(g.out, ws+"} else {")
fmt.Fprintln(g.out, ws+" in.Delim('{')")
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(map["+g.getType(t.Key())+"]"+g.getType(t.Elem())+")")
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+" key := in.String()")
fmt.Fprintln(g.out, ws+" key := "+g.getType(t.Key())+"(in.String())")
fmt.Fprintln(g.out, ws+" in.WantColon()")
fmt.Fprintln(g.out, ws+" var "+tmpVar+" "+g.getType(elem))
+1 -1
View File
@@ -99,7 +99,7 @@ func (g *Generator) genTypeEncoder(t reflect.Type, in string, indent int) error
fmt.Fprintln(g.out, ws+" for "+tmpVar+"_name, "+tmpVar+"_value := range "+in+" {")
fmt.Fprintln(g.out, ws+" if !"+tmpVar+"_first { out.RawByte(',') }")
fmt.Fprintln(g.out, ws+" "+tmpVar+"_first = false")
fmt.Fprintln(g.out, ws+" out.String("+tmpVar+"_name)")
fmt.Fprintln(g.out, ws+" out.String(string("+tmpVar+"_name))")
fmt.Fprintln(g.out, ws+" out.RawByte(':')")
g.genTypeEncoder(t.Elem(), tmpVar+"_value", indent+2)
+8 -1
View File
@@ -374,19 +374,26 @@ var excludedFieldValue = ExcludedField{
}
var excludedFieldString = `{"process":true}`
type Str string
type Maps struct {
Map map[string]string
InterfaceMap map[string]interface{}
NilMap map[string]string
CustomMap map[Str]Str
}
var mapsValue = Maps{
Map: map[string]string{"A": "b"}, // only one item since map iteration is randomized
InterfaceMap: map[string]interface{}{"G": float64(1)},
CustomMap: map[Str]Str{"c": "d"},
}
var mapsString = `{` +
`"Map":{"A":"b"},` +
`"InterfaceMap":{"G":1},` +
`"NilMap":null` +
`"NilMap":null,` +
`"CustomMap":{"c":"d"}` +
`}`