Fixes #5: Correct handling of named primitive types.

This commit is contained in:
Victor Starodub
2016-03-29 13:14:08 +03:00
parent 57a7965db5
commit 4fd343250a
4 changed files with 125 additions and 19 deletions
+1 -1
View File
@@ -53,7 +53,7 @@ func (g *Generator) genTypeDecoder(t reflect.Type, out string, indent int) error
// Check whether type is primitive, needs to be done after interface check.
if dec := primitiveDecoders[t.Kind()]; dec != "" {
fmt.Fprintln(g.out, ws+out+" = "+dec)
fmt.Fprintln(g.out, ws+out+" = "+g.getType(t)+"("+dec+")")
return nil
}
+15 -15
View File
@@ -15,20 +15,20 @@ func (g *Generator) getStructEncoderName(t reflect.Type) string {
}
var primitiveEncoders = map[reflect.Kind]string{
reflect.String: "out.String",
reflect.Bool: "out.Bool",
reflect.Int: "out.Int",
reflect.Int8: "out.Int8",
reflect.Int16: "out.Int16",
reflect.Int32: "out.Int32",
reflect.Int64: "out.Int64",
reflect.Uint: "out.Uint",
reflect.Uint8: "out.Uint8",
reflect.Uint16: "out.Uint16",
reflect.Uint32: "out.Uint32",
reflect.Uint64: "out.Uint64",
reflect.Float32: "out.Float32",
reflect.Float64: "out.Float64",
reflect.String: "out.String(string(%v))",
reflect.Bool: "out.Bool(bool(%v))",
reflect.Int: "out.Int(int(%v))",
reflect.Int8: "out.Int8(int8(%v))",
reflect.Int16: "out.Int16(int16(%v))",
reflect.Int32: "out.Int32(int32(%v))",
reflect.Int64: "out.Int64(int64(%v))",
reflect.Uint: "out.Uint(uint(%v))",
reflect.Uint8: "out.Uint8(uint8(%v))",
reflect.Uint16: "out.Uint16(uint16(%v))",
reflect.Uint32: "out.Uint32(uint32(%v))",
reflect.Uint64: "out.Uint64(uint64(%v))",
reflect.Float32: "out.Float32(float32(%v))",
reflect.Float64: "out.Float64(float64(%v))",
}
func (g *Generator) genTypeEncoder(t reflect.Type, in string, indent int) error {
@@ -48,7 +48,7 @@ func (g *Generator) genTypeEncoder(t reflect.Type, in string, indent int) error
// Check whether type is primitive, needs to be done after interface check.
if enc := primitiveEncoders[t.Kind()]; enc != "" {
fmt.Fprintln(g.out, ws+enc+"("+in+")")
fmt.Fprintf(g.out, ws+enc+"\n", in)
return nil
}
+1
View File
@@ -17,6 +17,7 @@ var testCases = []struct {
Encoded string
}{
{&primitiveTypesValue, primitiveTypesString},
{&namedPrimitiveTypesValue, namedPrimitiveTypesString},
{&structsValue, structsString},
{&omitEmptyValue, omitEmptyString},
{&snakeStructValue, snakeStructString},
+108 -3
View File
@@ -78,19 +78,115 @@ var primitiveTypesString = "{" +
"}"
type (
NamedString string
NamedBool bool
NamedInt int
NamedInt8 int8
NamedInt16 int16
NamedInt32 int32
NamedInt64 int64
NamedUint uint
NamedUint8 uint8
NamedUint16 uint16
NamedUint32 uint32
NamedUint64 uint64
NamedFloat32 float32
NamedFloat64 float64
NamedStrPtr *string
)
type NamedPrimitiveTypes struct {
String NamedString
Bool NamedBool
Int NamedInt
Int8 NamedInt8
Int16 NamedInt16
Int32 NamedInt32
Int64 NamedInt64
Uint NamedUint
Uint8 NamedUint8
Uint16 NamedUint16
Uint32 NamedUint32
Uint64 NamedUint64
Float32 NamedFloat32
Float64 NamedFloat64
Ptr NamedStrPtr
PtrNil NamedStrPtr
}
var namedPrimitiveTypesValue = NamedPrimitiveTypes{
String: "test",
Bool: true,
Int: math.MinInt32,
Int8: math.MinInt8,
Int16: math.MinInt16,
Int32: math.MinInt32,
Int64: math.MinInt64,
Uint: math.MaxUint32,
Uint8: math.MaxUint8,
Uint16: math.MaxUint16,
Uint32: math.MaxUint32,
Uint64: math.MaxUint64,
Float32: 1.5,
Float64: math.MaxFloat64,
Ptr: NamedStrPtr(&str),
}
var namedPrimitiveTypesString = "{" +
`"String":"test",` +
`"Bool":true,` +
`"Int":` + fmt.Sprint(math.MinInt32) + `,` +
`"Int8":` + fmt.Sprint(math.MinInt8) + `,` +
`"Int16":` + fmt.Sprint(math.MinInt16) + `,` +
`"Int32":` + fmt.Sprint(math.MinInt32) + `,` +
`"Int64":` + fmt.Sprint(int64(math.MinInt64)) + `,` +
`"Uint":` + fmt.Sprint(math.MaxUint32) + `,` +
`"Uint8":` + fmt.Sprint(math.MaxUint8) + `,` +
`"Uint16":` + fmt.Sprint(math.MaxUint16) + `,` +
`"Uint32":` + fmt.Sprint(math.MaxUint32) + `,` +
`"Uint64":` + fmt.Sprint(uint64(math.MaxUint64)) + `,` +
`"Float32":` + fmt.Sprint(1.5) + `,` +
`"Float64":` + fmt.Sprint(math.MaxFloat64) + `,` +
`"Ptr":"bla",` +
`"PtrNil":null` +
"}"
type SubStruct struct {
Value string
Value2 string
unexpored bool
}
type SubStructAlias SubStruct
type Structs struct {
SubStruct
Value2 int
Sub1 SubStruct `json:"substruct"`
Sub2 *SubStruct
SubNil *SubStruct
Sub1 SubStruct `json:"substruct"`
Sub2 *SubStruct
SubNil *SubStruct
SubA1 SubStructAlias
SubA2 *SubStructAlias
Anonymous struct {
V string
I int
@@ -107,6 +203,10 @@ var structsValue = Structs{
Sub1: SubStruct{Value: "test1", Value2: "v"},
Sub2: &SubStruct{Value: "test2", Value2: "v2"},
SubA1: SubStructAlias{Value: "test3", Value2: "v3"},
SubA2: &SubStructAlias{Value: "test4", Value2: "v4"},
Anonymous: struct {
V string
I int
@@ -118,9 +218,14 @@ var structsValue = Structs{
var structsString = "{" +
`"Value2":5,` +
`"substruct":{"Value":"test1","Value2":"v"},` +
`"Sub2":{"Value":"test2","Value2":"v2"},` +
`"SubNil":null,` +
`"SubA1":{"Value":"test3","Value2":"v3"},` +
`"SubA2":{"Value":"test4","Value2":"v4"},` +
`"Anonymous":{"V":"bla","I":5},` +
`"Anonymous1":{"V":"bla1"},` +