Merge pull request #22 from missish/develop

refactor(tool): 增强复制功能并修复零值处理问题
This commit is contained in:
Leif Draven 2025-05-31 21:47:07 +09:00 committed by GitHub
commit 56f6bd2542
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 41 additions and 11 deletions

View File

@ -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()))

View File

@ -37,7 +37,7 @@ func (l *UpdateNodeLogic) UpdateNode(req *types.UpdateNodeRequest) error {
if err != nil { if err != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "find server error: %v", err) 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) config, err := json.Marshal(req.Config)
if err != nil { if err != nil {
return err return err
@ -50,8 +50,14 @@ func (l *UpdateNodeLogic) UpdateNode(req *types.UpdateNodeRequest) error {
return err return err
} }
if len(req.Tags) > 0 { // 处理Tags字段
switch {
case len(req.Tags) > 0:
// 有Tags进行连接
nodeInfo.Tags = strings.Join(req.Tags, ",") nodeInfo.Tags = strings.Join(req.Tags, ",")
default:
// 空数组清空Tags
nodeInfo.Tags = ""
} }
nodeInfo.City = req.City nodeInfo.City = req.City

View File

@ -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
} }