Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #178 from Vladius25/master
Browse files Browse the repository at this point in the history
Support nested structures in util.MapToStruct
  • Loading branch information
muka authored Dec 13, 2022
2 parents 0746e3a + b01fffd commit 85dc80e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
19 changes: 17 additions & 2 deletions util/map_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ func mapStructField(obj interface{}, name string, value dbus.Variant) error {

if val.Type().Kind() == reflect.Map {

if structFieldType.Kind() == reflect.Struct ||
(structFieldType.Kind() == reflect.Pointer && structFieldType.Elem().Kind() == reflect.Struct) {

variantMap, ok := value.Value().(map[string]dbus.Variant)
if ok {
if structFieldType.Kind() == reflect.Pointer {
if structFieldValue.IsZero() {
return fmt.Errorf("Pointer field %s: uninitialized", name)
}
return MapToStruct(structFieldValue.Interface(), variantMap)
}
return MapToStruct(structFieldValue.Addr().Interface(), variantMap)
}
}

structVal := structFieldType.Elem()
structKey := structFieldType.Key()

Expand All @@ -99,7 +114,7 @@ func mapStructField(obj interface{}, name string, value dbus.Variant) error {
return fmt.Errorf("Field %s: map key mismatching values object=%s props=%s", name, structKey.Kind(), mapKey.Kind())
}

// Assign value if signture is map[*]interface{}
// Assign value if signature is map[*]interface{}
if structVal.Kind() == reflect.Interface {

val1MapType := reflect.MapOf(structKey, structVal)
Expand All @@ -122,7 +137,7 @@ func mapStructField(obj interface{}, name string, value dbus.Variant) error {
return fmt.Errorf("Mismatching types for field=%s object=%s props=%s", name, structFieldType, val.Type())
}

// MapToStruct converts a map[string]interface{} to a struct
// MapToStruct converts a map[string]dbus.Variant to a struct
func MapToStruct(s interface{}, m map[string]dbus.Variant) error {
for k, v := range m {
err := mapStructField(s, k, v)
Expand Down
39 changes: 39 additions & 0 deletions util/map_struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,42 @@ func TestStructToMap(t *testing.T) {
assert.Equal(t, val2.([]byte), val1[0x00])

}

func TestNestedStructToMap(t *testing.T) {

log.SetLevel(log.DebugLevel)

type fooStruct struct {
Field1 string
Field2 string
}
type barStruct struct {
Field uint32
Nested *fooStruct
}

struct1 := struct {
BarData barStruct
}{BarData: barStruct{Nested: &fooStruct{}}}

nestedVal := map[string]dbus.Variant{
"Field1": dbus.MakeVariant("val3-1"),
"Field2": dbus.MakeVariant("val3-2"),
}
val1 := map[string]dbus.Variant{
"Field": dbus.MakeVariant(uint32(9)),
"Nested": dbus.MakeVariant(nestedVal),
}

map1 := map[string]dbus.Variant{
"BarData": dbus.MakeVariant(val1),
}

err := MapToStruct(&struct1, map1)
if err != nil {
t.Fatal(err)
}

assert.Equal(t, struct1.BarData.Field, val1["Field"].Value().(uint32))
assert.Equal(t, struct1.BarData.Nested.Field2, nestedVal["Field2"].Value().(string))
}

0 comments on commit 85dc80e

Please sign in to comment.