Fix node set

This commit is contained in:
Christian Goll
2022-07-26 10:48:16 +02:00
parent d252b974bf
commit 925a5a2155
2 changed files with 18 additions and 4 deletions

View File

@@ -307,9 +307,9 @@ func (ent *Entry) Defined() bool {
Set the Entry trough an interface by trying to cast the interface
*/
func SetEntry(entryPtr interface{}, val interface{}) {
valKind := reflect.TypeOf(val)
if reflect.TypeOf(entryPtr) == reflect.TypeOf((*Entry)(nil)) {
entry := entryPtr.(*Entry)
valKind := reflect.TypeOf(val)
if valKind.Kind() == reflect.String {
entry.Set(val.(string))
} else if valKind.Kind() == reflect.Slice {
@@ -319,6 +319,18 @@ func SetEntry(entryPtr interface{}, val interface{}) {
panic("Got unknown slice type")
}
}
} else if reflect.TypeOf(entryPtr) == reflect.TypeOf((*[]string)(nil)) {
entry := entryPtr.(*[]string)
if valKind.Kind() == reflect.String {
// most likely we got a comma seperated string slice
*entry = strings.Split(val.(string), ",")
} else if valKind.Kind() == reflect.Slice {
if valKind.Elem().Kind() == reflect.String {
*entry = val.([]string)
} else {
panic("Got unknown slice type")
}
}
} else {
panic(fmt.Sprintf("Can't convert %s to *node.Entry to call Set\n", reflect.TypeOf(entryPtr)))
}
@@ -370,7 +382,7 @@ func DelEntry(entryMapInt interface{}, val interface{}) {
}
/*
Call SetEntry for given field (NodeInfo)
Call SetEntry for given field (NodeInfo).
*/
func (node *NodeInfo) SetField(fieldName string, val interface{}) {
field := reflect.ValueOf(node).Elem().FieldByName(fieldName)
@@ -378,6 +390,8 @@ func (node *NodeInfo) SetField(fieldName string, val interface{}) {
if field.Addr().Type() == reflect.TypeOf((*Entry)(nil)) {
//fmt.Println(reflect.TypeOf(field.Addr().Interface()))
SetEntry(field.Addr().Interface(), val)
} else if field.Addr().Type() == reflect.TypeOf((*[]string)(nil)) {
SetEntry(field.Addr().Interface(), val)
}
/*
else if field.Addr().Kind() == reflect.Map {