Fix: Connection settings route and address data types

This commit is contained in:
Christian Müller 2022-11-22 14:27:06 +01:00
parent 8fe336a60b
commit 58824d8cc3
2 changed files with 37 additions and 30 deletions

View file

@ -125,30 +125,37 @@ func decodeSettings(input map[string]map[string]dbus.Variant) (settings Connecti
func decode(input interface{}) (value interface{}) { func decode(input interface{}) (value interface{}) {
if variant, isVariant := input.(dbus.Variant); isVariant { if variant, isVariant := input.(dbus.Variant); isVariant {
return decode(variant.Value()) return decode(variant.Value())
} else { } else if inputMap, isMap := input.(map[string]dbus.Variant); isMap {
return decodeMap(inputMap)
if inputMap, isMap := input.(map[string]dbus.Variant); isMap { } else if inputArray, isArray := input.([]dbus.Variant); isArray {
valueMap := map[string]interface{}{} return decodeArray(inputArray)
for key, data := range inputMap {
valueMap[key] = decode(data)
}
return valueMap
} else if inputArray, isArray := input.([]interface{}); isArray {
var valueArray []interface{}
for _, data := range inputArray {
valueArray = append(valueArray, decode(data))
}
return valueArray
} else if inputArray, isArray := input.([]map[string]dbus.Variant); isArray { } else if inputArray, isArray := input.([]map[string]dbus.Variant); isArray {
var valueArray []interface{} return decodeMapArray(inputArray)
for _, data := range inputArray {
valueArray = append(valueArray, decode(data))
}
return valueArray
} else { } else {
return input return input
} }
}
func decodeArray(input []dbus.Variant) (value []interface{}) {
for _, data := range input {
value = append(value, decode(data))
} }
return
}
func decodeMapArray(input []map[string]dbus.Variant) (value []map[string]interface{}) {
for _, data := range input {
value = append(value, decodeMap(data))
}
return
}
func decodeMap(input map[string]dbus.Variant) (value map[string]interface{}) {
value = map[string]interface{}{}
for key, data := range input {
value[key] = decode(data)
}
return
} }
func (c *connection) ClearSecrets() error { func (c *connection) ClearSecrets() error {

View file

@ -63,15 +63,15 @@ func TestDecodeSettings(t *testing.T) {
expected := ConnectionSettings{ expected := ConnectionSettings{
"ipv4": { "ipv4": {
"address-data": []interface{}{ "address-data": []map[string]interface{}{
map[string]interface{}{ {
"address": "192.168.1.156", "address": "192.168.1.156",
"prefix": 24, "prefix": 24,
}, },
}, },
"dns-search": []string{}, "dns-search": []string{},
"method": "manual", "method": "manual",
"route-data": []interface{}(nil), "route-data": []map[string]interface{}(nil),
"routes": [][]uint32{}, "routes": [][]uint32{},
"addresses": [][]uint32{ "addresses": [][]uint32{
{ {
@ -86,11 +86,11 @@ func TestDecodeSettings(t *testing.T) {
}, },
"ipv6": { "ipv6": {
"addr-gen-mode": 3, "addr-gen-mode": 3,
"address-data": []interface{}(nil), "address-data": []map[string]interface{}(nil),
"routes": [][]interface{}{}, "routes": [][]interface{}{},
"dns-search": []string{}, "dns-search": []string{},
"method": "auto", "method": "auto",
"route-data": []interface{}(nil), "route-data": []map[string]interface{}(nil),
"dhcp-timeout": 45, "dhcp-timeout": 45,
"route-metric": 100, "route-metric": 100,
"addresses": [][]interface{}{}, "addresses": [][]interface{}{},
@ -145,15 +145,15 @@ func TestDecode(t *testing.T) {
result := decode(ipSettings) result := decode(ipSettings)
expected := map[string]interface{}{ expected := map[string]interface{}{
"address-data": []interface{}{ "address-data": []map[string]interface{}{
map[string]interface{}{ {
"address": "192.168.1.156", "address": "192.168.1.156",
"prefix": 24, "prefix": 24,
}, },
}, },
"dns-search": []string{}, "dns-search": []string{},
"method": "manual", "method": "manual",
"route-data": []interface{}(nil), "route-data": []map[string]interface{}(nil),
"routes": [][]uint32{}, "routes": [][]uint32{},
"addresses": [][]uint32{ "addresses": [][]uint32{
{ {