1
0
Fork 0

feat: raw map parser.

This commit is contained in:
Fernandez Ludovic 2020-07-12 12:56:22 +02:00 committed by Traefiker Bot
parent 0186c31d59
commit c42f1b7a50
13 changed files with 901 additions and 18 deletions

View file

@ -271,6 +271,16 @@ func (f filler) setMap(field reflect.Value, node *Node) error {
field.Set(reflect.MakeMap(field.Type()))
}
if field.Type().Elem().Kind() == reflect.Interface {
fillRawValue(field, node, false)
for _, child := range node.Children {
fillRawValue(field, child, true)
}
return nil
}
for _, child := range node.Children {
ptrValue := reflect.New(reflect.PtrTo(field.Type().Elem()))
@ -284,6 +294,7 @@ func (f filler) setMap(field reflect.Value, node *Node) error {
key := reflect.ValueOf(child.Name)
field.SetMapIndex(key, value)
}
return nil
}
@ -339,3 +350,23 @@ func setFloat(field reflect.Value, value string, bitSize int) error {
field.Set(reflect.ValueOf(val).Convert(field.Type()))
return nil
}
func fillRawValue(field reflect.Value, node *Node, subMap bool) {
m, ok := node.RawValue.(map[string]interface{})
if !ok {
return
}
if _, self := m[node.Name]; self || !subMap {
for k, v := range m {
field.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v))
}
return
}
p := map[string]interface{}{node.Name: m}
node.RawValue = p
field.SetMapIndex(reflect.ValueOf(node.Name), reflect.ValueOf(p[node.Name]))
}