Parcourir la source

experimenting with recursive update

reid il y a 2 ans
Parent
commit
1f78350444
1 fichiers modifiés avec 9 ajouts et 3 suppressions
  1. 9 3
      broadcast/broadcast.go

+ 9 - 3
broadcast/broadcast.go

@@ -255,9 +255,15 @@ func UpdateBroadcastState(values map[string]interface{}) error {
 
 // this allows us to insert stuff into nested structs/keys and not overwrite the existing contents
 func recursiveUpdate(dst, src reflect.Value) error {
-	if !dst.CanSet() {
-		return fmt.Errorf("field (type: %s, kind: %s) is not settable", dst.Type(), dst.Kind())
-	}
+    // Check if neither dst nor src are maps or structs (e.g., both are primitive types)
+    if dst.Kind() != reflect.Map && dst.Kind() != reflect.Struct &&
+       src.Kind() != reflect.Map && src.Kind() != reflect.Struct {
+        if dst.Type() != src.Type() {
+            return fmt.Errorf("type mismatch: expected %s, got %s", dst.Type(), src.Type())
+        }
+        dst.Set(src)
+        return nil
+    }
 	// If dst is a struct and src is a map, handle them field by field
 	if dst.Kind() == reflect.Struct && src.Kind() == reflect.Map {
 		for _, key := range src.MapKeys() {