Skip to content

Commit

Permalink
Fix named slices
Browse files Browse the repository at this point in the history
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
  • Loading branch information
erikdubbelboer committed Mar 23, 2019
1 parent e517b90 commit 4d654a0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
14 changes: 11 additions & 3 deletions inception/decoder_tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
11 changes: 11 additions & 0 deletions tests/ff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
5 changes: 5 additions & 0 deletions tests/ff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}

0 comments on commit 4d654a0

Please sign in to comment.