bbgo_origin/pkg/dynamic/merge.go
c9s b15e8d0ce4
all: refactor exit method set and fix dynamic call/merge
Signed-off-by: c9s <yoanlin93@gmail.com>
2022-06-30 15:49:18 +08:00

39 lines
1004 B
Go

package dynamic
import "reflect"
// MergeStructValues merges the field value from the source struct to the dest struct.
// Only fields with the same type and the same name will be updated.
func MergeStructValues(dst, src interface{}) {
if dst == nil {
return
}
rtA := reflect.TypeOf(dst)
srcStructType := reflect.TypeOf(src)
rtA = rtA.Elem()
srcStructType = srcStructType.Elem()
for i := 0; i < rtA.NumField(); i++ {
fieldType := rtA.Field(i)
fieldName := fieldType.Name
if !fieldType.IsExported() {
continue
}
// 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)
}
}
}
}
}