Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: slice of struct #19

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ issues:
exclude:
- 'ST1000: at least one file in a package should have a package comment'
- 'package-comments: should have a package comment'
- 'G602: slice index out of range'
- 'G602: slice bounds out of range'
exclude-rules:
- path: (.+)_test.go
linters:
Expand Down
10 changes: 10 additions & 0 deletions parser/element_fill.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,16 @@ func (f filler) fillRawMapWithTypedSlice(elt interface{}) (reflect.Value, error)

eltValue.SetMapIndex(reflect.ValueOf(k), value)
}

case reflect.Slice:
for i, v := range elt.([]interface{}) {
value, err := f.fillRawMapWithTypedSlice(v)
if err != nil {
return eltValue, err
}

eltValue.Index(i).Set(value)
}
}

return eltValue, nil
Expand Down
31 changes: 31 additions & 0 deletions parser/element_fill_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1395,6 +1395,37 @@ func TestFill(t *testing.T) {
},
}},
},
{
desc: "slice struct with slice",
rawSliceSeparator: "║",
node: &Node{
Name: "traefik",
Kind: reflect.Pointer,
Children: []*Node{
{Name: "Foo", FieldName: "Foo", Kind: reflect.Map, Children: []*Node{
{Name: "Field1", FieldName: "Field1", RawValue: map[string]interface{}{
"Field2": []interface{}{map[string]interface{}{"Values": "║24║foo║bar"}},
}},
{Name: "Field3", RawValue: map[string]interface{}{
"Values": "║24║foo║bar",
}},
}},
},
},
element: &struct {
Foo map[string]interface{}
}{},
expected: expected{element: &struct {
Foo map[string]interface{}
}{
Foo: map[string]interface{}{
"Field1": map[string]interface{}{"Field2": []interface{}{map[string]interface{}{"Values": []interface{}{"foo", "bar"}}}},
"Field3": map[string]interface{}{
"Values": []interface{}{"foo", "bar"},
},
},
}},
},
{
desc: "slice pointer struct",
node: &Node{
Expand Down