Skip to content

Commit

Permalink
fix: slice of struct
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatur authored Aug 29, 2024
1 parent 105b22c commit d116c5c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
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

0 comments on commit d116c5c

Please sign in to comment.