refactor(tool): 为DeepCopy和ShallowCopy添加配置选项支持
为copy工具函数添加了CopyOption配置选项,允许调用方自定义复制行为。主要修改包括: 1. 新增CopyOption函数类型和CopyWithIgnoreEmpty选项 2. 修改DeepCopy和ShallowCopy函数签名以支持可变选项参数 3. 重构内部实现以应用传入的选项配置 这使得调用方可以灵活控制是否忽略空值等复制行为,增强了工具函数的可配置性。
This commit is contained in:
parent
207ffc854e
commit
581e3ee48a
@ -39,7 +39,7 @@ func (l *UpdatePaymentMethodLogic) UpdatePaymentMethod(req *types.UpdatePaymentM
|
|||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "find payment method error: %s", err.Error())
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "find payment method error: %s", err.Error())
|
||||||
}
|
}
|
||||||
config := parsePaymentPlatformConfig(l.ctx, payment.ParsePlatform(req.Platform), req.Config)
|
config := parsePaymentPlatformConfig(l.ctx, payment.ParsePlatform(req.Platform), req.Config)
|
||||||
tool.DeepCopy(method, req)
|
tool.DeepCopy(method, req, tool.CopyWithIgnoreEmpty(false))
|
||||||
method.Config = config
|
method.Config = config
|
||||||
if err := l.svcCtx.PaymentModel.Update(l.ctx, method); err != nil {
|
if err := l.svcCtx.PaymentModel.Update(l.ctx, method); err != nil {
|
||||||
l.Errorw("update payment method error", logger.Field("id", req.Id), logger.Field("error", err.Error()))
|
l.Errorw("update payment method error", logger.Field("id", req.Id), logger.Field("error", err.Error()))
|
||||||
|
|||||||
@ -13,17 +13,28 @@ import (
|
|||||||
"github.com/perfect-panel/server/pkg/constant"
|
"github.com/perfect-panel/server/pkg/constant"
|
||||||
)
|
)
|
||||||
|
|
||||||
func DeepCopy[T, K interface{}](destStruct T, srcStruct K) T {
|
// CopyOption 定义复制选项的函数类型
|
||||||
|
type CopyOption func(*copier.Option)
|
||||||
|
|
||||||
|
// CopyWithIgnoreEmpty 设置是否忽略空值
|
||||||
|
func CopyWithIgnoreEmpty(ignoreEmpty bool) CopyOption {
|
||||||
|
return func(o *copier.Option) {
|
||||||
|
o.IgnoreEmpty = ignoreEmpty
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeepCopy[T, K any](destStruct T, srcStruct K, opts ...CopyOption) T {
|
||||||
var dst = destStruct
|
var dst = destStruct
|
||||||
var src = srcStruct
|
var src = srcStruct
|
||||||
_ = copier.CopyWithOption(dst, src, copier.Option{
|
|
||||||
|
option := copier.Option{
|
||||||
DeepCopy: true,
|
DeepCopy: true,
|
||||||
IgnoreEmpty: true,
|
IgnoreEmpty: true,
|
||||||
Converters: []copier.TypeConverter{
|
Converters: []copier.TypeConverter{
|
||||||
{
|
{
|
||||||
SrcType: time.Time{},
|
SrcType: time.Time{},
|
||||||
DstType: constant.Int64,
|
DstType: constant.Int64,
|
||||||
Fn: func(src interface{}) (interface{}, error) {
|
Fn: func(src any) (any, error) {
|
||||||
s, ok := src.(time.Time)
|
s, ok := src.(time.Time)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errors.New("src type not matching")
|
return nil, errors.New("src type not matching")
|
||||||
@ -32,13 +43,21 @@ func DeepCopy[T, K interface{}](destStruct T, srcStruct K) T {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
}
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(&option)
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = copier.CopyWithOption(dst, src, option)
|
||||||
return dst
|
return dst
|
||||||
}
|
}
|
||||||
func ShallowCopy[T, K interface{}](destStruct T, srcStruct K) T {
|
|
||||||
|
func ShallowCopy[T, K interface{}](destStruct T, srcStruct K, opts ...CopyOption) T {
|
||||||
var dst = destStruct
|
var dst = destStruct
|
||||||
var src = srcStruct
|
var src = srcStruct
|
||||||
_ = copier.CopyWithOption(dst, src, copier.Option{
|
|
||||||
|
option := copier.Option{
|
||||||
IgnoreEmpty: true,
|
IgnoreEmpty: true,
|
||||||
Converters: []copier.TypeConverter{
|
Converters: []copier.TypeConverter{
|
||||||
{
|
{
|
||||||
@ -46,7 +65,6 @@ func ShallowCopy[T, K interface{}](destStruct T, srcStruct K) T {
|
|||||||
DstType: constant.Int64,
|
DstType: constant.Int64,
|
||||||
Fn: func(src interface{}) (interface{}, error) {
|
Fn: func(src interface{}) (interface{}, error) {
|
||||||
s, ok := src.(time.Time)
|
s, ok := src.(time.Time)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errors.New("src type not matching")
|
return nil, errors.New("src type not matching")
|
||||||
}
|
}
|
||||||
@ -54,7 +72,13 @@ func ShallowCopy[T, K interface{}](destStruct T, srcStruct K) T {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
}
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(&option)
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = copier.CopyWithOption(dst, src, option)
|
||||||
return dst
|
return dst
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user