Skip to content

Commit

Permalink
fix: slice of struct
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatur committed Aug 27, 2024
1 parent 368ce2e commit abfd130
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
10 changes: 10 additions & 0 deletions parser/element_fill.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,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 abfd130

Please sign in to comment.