dynamic: invert if

Signed-off-by: c9s <yoanlin93@gmail.com>
This commit is contained in:
c9s 2022-06-30 15:48:06 +08:00
parent b15e8d0ce4
commit 903d773025
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -24,14 +24,17 @@ func MergeStructValues(dst, src interface{}) {
}
// if there is a field with the same name
if fieldSrcType, ok := srcStructType.FieldByName(fieldName); ok {
// ensure that the type is the same
if fieldSrcType.Type == fieldType.Type {
srcValue := reflect.ValueOf(src).Elem().FieldByName(fieldName)
dstValue := reflect.ValueOf(dst).Elem().FieldByName(fieldName)
if (fieldType.Type.Kind() == reflect.Ptr && dstValue.IsNil()) || dstValue.IsZero() {
dstValue.Set(srcValue)
}
fieldSrcType, found := srcStructType.FieldByName(fieldName)
if !found {
continue
}
// ensure that the type is the same
if fieldSrcType.Type == fieldType.Type {
srcValue := reflect.ValueOf(src).Elem().FieldByName(fieldName)
dstValue := reflect.ValueOf(dst).Elem().FieldByName(fieldName)
if (fieldType.Type.Kind() == reflect.Ptr && dstValue.IsNil()) || dstValue.IsZero() {
dstValue.Set(srcValue)
}
}
}