Skip to content

Commit 4d654a0

Browse files
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
1 parent e517b90 commit 4d654a0

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

Diff for: inception/decoder_tpl.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,18 @@ var handleSliceTxt = `
353353
{{.Name}} = []*{{getType $ic .Name .Typ.Elem.Elem}}{}
354354
{{end}}
355355
{{else}}
356-
{{if eq .IsPtr true}}
357-
{{.Name}} = &[]{{getType $ic .Name .Typ.Elem}}{}
356+
{{if eq .Typ.Name ""}}
357+
{{if eq .IsPtr true}}
358+
{{.Name}} = &[]{{getType $ic .Name .Typ.Elem}}{}
359+
{{else}}
360+
{{.Name}} = []{{getType $ic .Name .Typ.Elem}}{}
361+
{{end}}
358362
{{else}}
359-
{{.Name}} = []{{getType $ic .Name .Typ.Elem}}{}
363+
{{if eq .IsPtr true}}
364+
{{.Name}} = &{{getType $ic .Name .Typ}}{}
365+
{{else}}
366+
{{.Name}} = {{getType $ic .Name .Typ}}{}
367+
{{end}}
360368
{{end}}
361369
{{end}}
362370

Diff for: tests/ff.go

+11
Original file line numberDiff line numberDiff line change
@@ -2305,3 +2305,14 @@ type XDominantField struct {
23052305
Name *int `json",omitempty"`
23062306
A *struct{ X int } `json:"Name,omitempty"`
23072307
}
2308+
2309+
type NamedSlice []IntType
2310+
2311+
// ffjson: skip
2312+
type TContainsNamedSlice struct {
2313+
Slice *NamedSlice `json:"ns"`
2314+
}
2315+
2316+
type XContainsNamedSlice struct {
2317+
Slice *NamedSlice `json:"ns"`
2318+
}

Diff for: tests/ff_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -860,3 +860,8 @@ func TestDominantField(t *testing.T) {
860860
i := 43
861861
testType(t, &TDominantField{Y: &i}, &XDominantField{Y: &i})
862862
}
863+
864+
func TestNamedSlice(t *testing.T) {
865+
ns := NamedSlice{1, 2, 3}
866+
testType(t, &TContainsNamedSlice{Slice: &ns}, &XContainsNamedSlice{Slice: &ns})
867+
}

0 commit comments

Comments
 (0)