Merge pull request #22 from missish/develop
refactor(tool): 增强复制功能并修复零值处理问题
This commit is contained in:
commit
56f6bd2542
@ -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())
|
||||
}
|
||||
config := parsePaymentPlatformConfig(l.ctx, payment.ParsePlatform(req.Platform), req.Config)
|
||||
tool.DeepCopy(method, req)
|
||||
tool.DeepCopy(method, req, tool.CopyWithIgnoreEmpty(false))
|
||||
method.Config = config
|
||||
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()))
|
||||
|
||||
@ -37,7 +37,7 @@ func (l *UpdateNodeLogic) UpdateNode(req *types.UpdateNodeRequest) error {
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "find server error: %v", err)
|
||||
}
|
||||
tool.DeepCopy(nodeInfo, req)
|
||||
tool.DeepCopy(nodeInfo, req, tool.CopyWithIgnoreEmpty(false))
|
||||
config, err := json.Marshal(req.Config)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -50,8 +50,14 @@ func (l *UpdateNodeLogic) UpdateNode(req *types.UpdateNodeRequest) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(req.Tags) > 0 {
|
||||
// 处理Tags字段
|
||||
switch {
|
||||
case len(req.Tags) > 0:
|
||||
// 有Tags,进行连接
|
||||
nodeInfo.Tags = strings.Join(req.Tags, ",")
|
||||
default:
|
||||
// 空数组,清空Tags
|
||||
nodeInfo.Tags = ""
|
||||
}
|
||||
|
||||
nodeInfo.City = req.City
|
||||
|
||||
@ -13,17 +13,28 @@ import (
|
||||
"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 src = srcStruct
|
||||
_ = copier.CopyWithOption(dst, src, copier.Option{
|
||||
|
||||
option := copier.Option{
|
||||
DeepCopy: true,
|
||||
IgnoreEmpty: true,
|
||||
Converters: []copier.TypeConverter{
|
||||
{
|
||||
SrcType: time.Time{},
|
||||
DstType: constant.Int64,
|
||||
Fn: func(src interface{}) (interface{}, error) {
|
||||
Fn: func(src any) (any, error) {
|
||||
s, ok := src.(time.Time)
|
||||
if !ok {
|
||||
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
|
||||
}
|
||||
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 src = srcStruct
|
||||
_ = copier.CopyWithOption(dst, src, copier.Option{
|
||||
|
||||
option := copier.Option{
|
||||
IgnoreEmpty: true,
|
||||
Converters: []copier.TypeConverter{
|
||||
{
|
||||
@ -46,7 +65,6 @@ func ShallowCopy[T, K interface{}](destStruct T, srcStruct K) T {
|
||||
DstType: constant.Int64,
|
||||
Fn: func(src interface{}) (interface{}, error) {
|
||||
s, ok := src.(time.Time)
|
||||
|
||||
if !ok {
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user