From 5a7438e1ff0e0dfc8e70bf8dd0ff647e4adcfbc8 Mon Sep 17 00:00:00 2001 From: Victor Starodub Date: Fri, 1 Apr 2016 22:56:46 +0300 Subject: [PATCH] Fixes #8: Fix type-to-string logic for pointers. --- gen/generator.go | 4 ++++ tests/data.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/gen/generator.go b/gen/generator.go index 068cf73..a56d9c9 100644 --- a/gen/generator.go +++ b/gen/generator.go @@ -212,6 +212,10 @@ func (g *Generator) pkgAlias(pkgPath string) string { // getType return the textual type name of given type that can be used in generated code. func (g *Generator) getType(t reflect.Type) string { + if t.Kind() == reflect.Ptr { + return "*" + g.getType(t.Elem()) + } + if t.Name() == "" || t.PkgPath() == "" { return t.String() } else if t.PkgPath() == g.pkgPath { diff --git a/tests/data.go b/tests/data.go index 6bb9da1..6cd40df 100644 --- a/tests/data.go +++ b/tests/data.go @@ -184,6 +184,12 @@ type Structs struct { Sub2 *SubStruct SubNil *SubStruct + SubSlice []SubStruct + SubSliceNil []SubStruct + + SubPtrSlice []*SubStruct + SubPtrSliceNil []*SubStruct + SubA1 SubStructAlias SubA2 *SubStructAlias @@ -194,6 +200,10 @@ type Structs struct { Anonymous1 *struct { V string } + + AnonymousSlice []struct{ V int } + AnonymousPtrSlice []*struct{ V int } + unexported bool } @@ -204,6 +214,16 @@ var structsValue = Structs{ Sub1: SubStruct{Value: "test1", Value2: "v"}, Sub2: &SubStruct{Value: "test2", Value2: "v2"}, + SubSlice: []SubStruct{ + {Value: "s1"}, + {Value: "s2"}, + }, + + SubPtrSlice: []*SubStruct{ + {Value: "p1"}, + {Value: "p2"}, + }, + SubA1: SubStructAlias{Value: "test3", Value2: "v3"}, SubA2: &SubStructAlias{Value: "test4", Value2: "v4"}, @@ -211,9 +231,13 @@ var structsValue = Structs{ V string I int }{V: "bla", I: 5}, + Anonymous1: &struct { V string }{V: "bla1"}, + + AnonymousSlice: []struct{ V int }{{1}, {2}}, + AnonymousPtrSlice: []*struct{ V int }{{3}, {4}}, } var structsString = "{" + @@ -223,12 +247,21 @@ var structsString = "{" + `"Sub2":{"Value":"test2","Value2":"v2"},` + `"SubNil":null,` + + `"SubSlice":[{"Value":"s1","Value2":""},{"Value":"s2","Value2":""}],` + + `"SubSliceNil":[],` + + + `"SubPtrSlice":[{"Value":"p1","Value2":""},{"Value":"p2","Value2":""}],` + + `"SubPtrSliceNil":[],` + + `"SubA1":{"Value":"test3","Value2":"v3"},` + `"SubA2":{"Value":"test4","Value2":"v4"},` + `"Anonymous":{"V":"bla","I":5},` + `"Anonymous1":{"V":"bla1"},` + + `"AnonymousSlice":[{"V":1},{"V":2}],` + + `"AnonymousPtrSlice":[{"V":3},{"V":4}],` + + // Embedded fields go last. `"Value":"test"` + "}"