Ai
1 Star 1 Fork 1

up-zero/gotool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
bean.go 1.40 KB
一键复制 编辑 原始数据 按行查看 历史
GetcharZp 提交于 2024-12-24 15:05 +08:00 . fix property copy support ptr
package gotool
import (
"errors"
"reflect"
)
// CopyProperties 复制结构体的属性
//
// src: 源对象
// dst: 目标对象
//
// 注意:dst必须是指针类型,且指向的结构体类型与src类型相同
//
// # Examples:
//
// type src struct {
// Name string
// Map map[string]int
// }
// type dst struct {
// Name string
// Map map[string]int
// Age int
// }
// s1 := src{Name: "test", Map: map[string]int{"a": 1}}
// s2 := new(dst)
// gotool.CopyProperties(s1, s2)
func CopyProperties(src, dst any) error {
if src == nil || dst == nil {
return ErrSrcDstCannotBeNil
}
srcValue := reflect.ValueOf(src)
if srcValue.Kind() == reflect.Ptr {
srcValue = srcValue.Elem()
}
dstValue := reflect.ValueOf(dst)
if dstValue.Kind() != reflect.Ptr || dstValue.Elem().Kind() != reflect.Struct {
return errors.New("dst must be a pointer to a struct")
}
// 使用 Elem 解引用指针(获取指针指向的实际结构体值)
dstValue = dstValue.Elem()
srcType := srcValue.Type()
for i := 0; i < srcType.NumField(); i++ {
srcField := srcType.Field(i)
srcFieldValue := srcValue.Field(i)
// 跳过不可导出的字段(即首字母小写)
if !srcFieldValue.CanInterface() {
continue
}
dstField := dstValue.FieldByName(srcField.Name)
if dstField.IsValid() && dstField.CanSet() && dstField.Type() == srcFieldValue.Type() {
dstField.Set(srcFieldValue)
}
}
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/up-zero/gotool.git
git@gitee.com:up-zero/gotool.git
up-zero
gotool
gotool
main

搜索帮助