Skip to content

Commit

Permalink
Handles any with RawValue
Browse files Browse the repository at this point in the history
  • Loading branch information
juliens committed Jan 17, 2024
1 parent 368ce2e commit 4b2ed0b
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 1 deletion.
12 changes: 12 additions & 0 deletions parser/element_fill.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ func (f filler) fill(field reflect.Value, node *Node) error {
return f.setMap(field, node)
case reflect.Slice:
return f.setSlice(field, node)
case reflect.Interface:
return f.setInterface(field, node)
default:
return nil
}
Expand Down Expand Up @@ -576,3 +578,13 @@ func (f filler) cleanRawValue(value reflect.Value) (reflect.Value, error) {

return value, nil
}

func (f filler) setInterface(field reflect.Value, node *Node) error {
if node.RawValue != nil {
field.Set(reflect.ValueOf(node.RawValue))
return nil
}

field.Set(reflect.ValueOf(node.Value))
return nil
}
50 changes: 50 additions & 0 deletions parser/element_fill_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,56 @@ func TestFill(t *testing.T) {
},
}},
},
{
desc: "any",
node: &Node{
Name: "traefik",
Kind: reflect.Struct,
Children: []*Node{
{
Name: "bar",
FieldName: "Bar",
RawValue: map[string]interface{}{
"baz": map[string]interface{}{
"boz": map[string]interface{}{
"foo": "42",
},
"foo": "bar",
},
"foo": "bar",
},
},
{
Name: "foo",
FieldName: "Foo",
Value: "bar",
RawValue: map[string]interface{}{
"foo": "bar",
},
},

{
Name: "fii",
FieldName: "Fii",
Value: "bar",
},
},
},
element: &struct {
Bar any
Foo any
Fii any
}{},
expected: expected{element: &struct {
Bar any
Foo any
Fii any
}{
Bar: map[string]any{"foo": "bar", "baz": map[string]any{"foo": "bar", "boz": map[string]any{"foo": "42"}}},
Foo: map[string]any{"foo": "bar"},
Fii: "bar",
}},
},
}

for _, test := range testCases {
Expand Down
5 changes: 5 additions & 0 deletions parser/nodes_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ func (m metadata) add(rootType reflect.Type, node *Node) error {
return nil
}

if fType.Kind() == reflect.Interface {
addRawValue(node)
return nil
}

return fmt.Errorf("invalid node %s: %v", node.Name, fType.Kind())
}

Expand Down
37 changes: 36 additions & 1 deletion parser/nodes_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,42 @@ func TestAddMetadata(t *testing.T) {
},
},
structure: struct{ Foo interf }{},
expected: expected{error: true},
expected: expected{
node: &Node{
Name: "traefik",
Kind: reflect.Struct,
Children: []*Node{
{Name: "Foo", FieldName: "Foo", RawValue: map[string]any{
"Fii": "hii",
}, Kind: reflect.Interface},
},
},
},
},
{
desc: "level 1, interface multiple level",
tree: &Node{
Name: "traefik",
Children: []*Node{
{Name: "Foo", Value: "", Children: []*Node{
{Name: "Fii", Children: []*Node{
{Name: "Fuu", Value: "huu"},
}},
}},
},
},
structure: struct{ Foo interf }{},
expected: expected{
node: &Node{
Name: "traefik",
Kind: reflect.Struct,
Children: []*Node{
{Name: "Foo", FieldName: "Foo", RawValue: map[string]any{
"Fii": map[string]any{"Fuu": "huu"},
}, Kind: reflect.Interface},
},
},
},
},
{
desc: "level 1, map string",
Expand Down

0 comments on commit 4b2ed0b

Please sign in to comment.