使用反射更新结构字段
Updating struct field using reflection
我正在为实体实施部分更新。
实体结构看起来像
type Entity struct {
Id string `readonly:"true"`
Spec EntitySpec
Status EntityState
}
type EntitySpec struct {
Version *string `readonly:"true"`
Users []*User
Field1 *InnerStruct1
Field2 []*InnerStruct2
}
等等。
所以我尝试使用反射递归迭代Entity
结构字段并更新允许用户更新的字段:
func method(existingEntity interface{}, newEntity interface{}) {
entityType := reflect.TypeOf(existingEntity)
logger.Debugf("type: %v", entityType)
for i := 0; i < entityType.NumField(); i++ {
value := entityType.Field(i)
logger.Debugf("Name: %s", value.Name)
tag := value.Tag
logger.Debugf("tag: %s", tag)
if tag.Get("readonly") == "true" {
logger.Debugf("readonly, go to next one")
continue
}
oldField := reflect.Indirect(reflect.ValueOf(existingEntity).Field(i))
newField := reflect.Indirect(reflect.ValueOf(newEntity).FieldByName(value.Name))
logger.Debugf("type: %v", value.Type.Kind())
if value.Type.Kind() == reflect.Struct {
logger.Debugf("value: %v", oldField)
//struct, go into it
method(oldField.Interface(), newField.Interface())
} else {
if oldField != newField && oldField.String() != newField.String() {
logger.Debugf("Type of old field: %v", oldField.Type())
logger.Debugf("Type of new field: %v", newField.Type())
logger.Debugf("Update: %v \nTo %v", oldField, newField)
oldField.Set(newField) //HERE I get the exception
} else {
logger.Debugf("Field values are equal")
}
}
}
}
但是当我尝试用 oldField.Set(newField)
分配一个新值时,出现异常:
reflect: reflect.Value.Set using unaddressable value
我也试过 reflect.ValueOf(existingEntity).Field(i).Set(reflect.ValueOf(newEntity).FieldByName(value.Name))
但遇到了同样的异常。
你能给我解释一下为什么会这样以及如何解决这个问题吗?
传递指向您正在更新的值的指针。这是更新为 existingEntity 获取指针的函数:
func method(existingEntity interface{}, newEntity interface{}) {
entityType := reflect.TypeOf(existingEntity).Elem()
for i := 0; i < entityType.NumField(); i++ {
value := entityType.Field(i)
tag := value.Tag
if tag.Get("readonly") == "true" {
continue
}
oldField := reflect.ValueOf(existingEntity).Elem().Field(i)
newField := reflect.ValueOf(newEntity).FieldByName(value.Name)
if value.Type.Kind() == reflect.Struct {
method(oldField.Addr().Interface(), newField.Interface())
} else {
oldField.Set(newField)
}
}
}
这样使用:
var a Example
b := Example{123, 456, Example2{789}}
method(&a, b)
这处理了问题中的具体情况。如果需要深度克隆,解决方案会更复杂。
我正在为实体实施部分更新。
实体结构看起来像
type Entity struct {
Id string `readonly:"true"`
Spec EntitySpec
Status EntityState
}
type EntitySpec struct {
Version *string `readonly:"true"`
Users []*User
Field1 *InnerStruct1
Field2 []*InnerStruct2
}
等等。
所以我尝试使用反射递归迭代Entity
结构字段并更新允许用户更新的字段:
func method(existingEntity interface{}, newEntity interface{}) {
entityType := reflect.TypeOf(existingEntity)
logger.Debugf("type: %v", entityType)
for i := 0; i < entityType.NumField(); i++ {
value := entityType.Field(i)
logger.Debugf("Name: %s", value.Name)
tag := value.Tag
logger.Debugf("tag: %s", tag)
if tag.Get("readonly") == "true" {
logger.Debugf("readonly, go to next one")
continue
}
oldField := reflect.Indirect(reflect.ValueOf(existingEntity).Field(i))
newField := reflect.Indirect(reflect.ValueOf(newEntity).FieldByName(value.Name))
logger.Debugf("type: %v", value.Type.Kind())
if value.Type.Kind() == reflect.Struct {
logger.Debugf("value: %v", oldField)
//struct, go into it
method(oldField.Interface(), newField.Interface())
} else {
if oldField != newField && oldField.String() != newField.String() {
logger.Debugf("Type of old field: %v", oldField.Type())
logger.Debugf("Type of new field: %v", newField.Type())
logger.Debugf("Update: %v \nTo %v", oldField, newField)
oldField.Set(newField) //HERE I get the exception
} else {
logger.Debugf("Field values are equal")
}
}
}
}
但是当我尝试用 oldField.Set(newField)
分配一个新值时,出现异常:
reflect: reflect.Value.Set using unaddressable value
我也试过 reflect.ValueOf(existingEntity).Field(i).Set(reflect.ValueOf(newEntity).FieldByName(value.Name))
但遇到了同样的异常。
你能给我解释一下为什么会这样以及如何解决这个问题吗?
传递指向您正在更新的值的指针。这是更新为 existingEntity 获取指针的函数:
func method(existingEntity interface{}, newEntity interface{}) {
entityType := reflect.TypeOf(existingEntity).Elem()
for i := 0; i < entityType.NumField(); i++ {
value := entityType.Field(i)
tag := value.Tag
if tag.Get("readonly") == "true" {
continue
}
oldField := reflect.ValueOf(existingEntity).Elem().Field(i)
newField := reflect.ValueOf(newEntity).FieldByName(value.Name)
if value.Type.Kind() == reflect.Struct {
method(oldField.Addr().Interface(), newField.Interface())
} else {
oldField.Set(newField)
}
}
}
这样使用:
var a Example
b := Example{123, 456, Example2{789}}
method(&a, b)
这处理了问题中的具体情况。如果需要深度克隆,解决方案会更复杂。