From 4d654a02118b712e2ead5fba2a3b068156f6ee45 Mon Sep 17 00:00:00 2001 From: Erik Dubbelboer Date: Thu, 6 Sep 2018 14:32:15 +0800 Subject: [PATCH] Fix named slices When a type is a slice of another type we should use the type name itself and not a slice of the elements of the slice. Fixes #243 --- inception/decoder_tpl.go | 14 +++++++++++--- tests/ff.go | 11 +++++++++++ tests/ff_test.go | 5 +++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/inception/decoder_tpl.go b/inception/decoder_tpl.go index 0985061..eb25c73 100644 --- a/inception/decoder_tpl.go +++ b/inception/decoder_tpl.go @@ -353,10 +353,18 @@ var handleSliceTxt = ` {{.Name}} = []*{{getType $ic .Name .Typ.Elem.Elem}}{} {{end}} {{else}} - {{if eq .IsPtr true}} - {{.Name}} = &[]{{getType $ic .Name .Typ.Elem}}{} + {{if eq .Typ.Name ""}} + {{if eq .IsPtr true}} + {{.Name}} = &[]{{getType $ic .Name .Typ.Elem}}{} + {{else}} + {{.Name}} = []{{getType $ic .Name .Typ.Elem}}{} + {{end}} {{else}} - {{.Name}} = []{{getType $ic .Name .Typ.Elem}}{} + {{if eq .IsPtr true}} + {{.Name}} = &{{getType $ic .Name .Typ}}{} + {{else}} + {{.Name}} = {{getType $ic .Name .Typ}}{} + {{end}} {{end}} {{end}} diff --git a/tests/ff.go b/tests/ff.go index bf6423d..e259015 100644 --- a/tests/ff.go +++ b/tests/ff.go @@ -2305,3 +2305,14 @@ type XDominantField struct { Name *int `json",omitempty"` A *struct{ X int } `json:"Name,omitempty"` } + +type NamedSlice []IntType + +// ffjson: skip +type TContainsNamedSlice struct { + Slice *NamedSlice `json:"ns"` +} + +type XContainsNamedSlice struct { + Slice *NamedSlice `json:"ns"` +} diff --git a/tests/ff_test.go b/tests/ff_test.go index c17f2db..da7bb13 100644 --- a/tests/ff_test.go +++ b/tests/ff_test.go @@ -860,3 +860,8 @@ func TestDominantField(t *testing.T) { i := 43 testType(t, &TDominantField{Y: &i}, &XDominantField{Y: &i}) } + +func TestNamedSlice(t *testing.T) { + ns := NamedSlice{1, 2, 3} + testType(t, &TContainsNamedSlice{Slice: &ns}, &XContainsNamedSlice{Slice: &ns}) +}