From abfd1303c457c52621b87b07dc7d94dc377a8a6a Mon Sep 17 00:00:00 2001 From: mmatur Date: Tue, 27 Aug 2024 16:24:27 +0200 Subject: [PATCH] fix: slice of struct --- parser/element_fill.go | 10 ++++++++++ parser/element_fill_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/parser/element_fill.go b/parser/element_fill.go index 0afe048..bdbd959 100644 --- a/parser/element_fill.go +++ b/parser/element_fill.go @@ -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 diff --git a/parser/element_fill_test.go b/parser/element_fill_test.go index b9d54a6..02e4efa 100644 --- a/parser/element_fill_test.go +++ b/parser/element_fill_test.go @@ -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{