From 581e3ee48a9b0acf18242df127a1db5a63f255db Mon Sep 17 00:00:00 2001 From: missish <2445921920@qq.com> Date: Sat, 31 May 2025 10:40:36 +0800 Subject: [PATCH 1/4] =?UTF-8?q?refactor(tool):=20=E4=B8=BADeepCopy?= =?UTF-8?q?=E5=92=8CShallowCopy=E6=B7=BB=E5=8A=A0=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E9=80=89=E9=A1=B9=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 为copy工具函数添加了CopyOption配置选项,允许调用方自定义复制行为。主要修改包括: 1. 新增CopyOption函数类型和CopyWithIgnoreEmpty选项 2. 修改DeepCopy和ShallowCopy函数签名以支持可变选项参数 3. 重构内部实现以应用传入的选项配置 这使得调用方可以灵活控制是否忽略空值等复制行为,增强了工具函数的可配置性。 --- .../admin/payment/updatePaymentMethodLogic.go | 2 +- pkg/tool/copy.go | 40 +++++++++++++++---- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/internal/logic/admin/payment/updatePaymentMethodLogic.go b/internal/logic/admin/payment/updatePaymentMethodLogic.go index c3bf26e..87b4fd7 100644 --- a/internal/logic/admin/payment/updatePaymentMethodLogic.go +++ b/internal/logic/admin/payment/updatePaymentMethodLogic.go @@ -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())) diff --git a/pkg/tool/copy.go b/pkg/tool/copy.go index 0dc629c..7d707c1 100644 --- a/pkg/tool/copy.go +++ b/pkg/tool/copy.go @@ -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 } From e0a4bb028bd180bf72e16805bf44ee6bd09ec0cf Mon Sep 17 00:00:00 2001 From: missish <2445921920@qq.com> Date: Sat, 31 May 2025 11:22:02 +0800 Subject: [PATCH 2/4] =?UTF-8?q?fix(admin/server):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E6=9B=B4=E6=96=B0=E6=97=B6Tags=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改DeepCopy调用以忽略空值,并完善Tags字段的处理逻辑。当Tags为空数组时清空数据库字段,避免保留旧值。 --- internal/logic/admin/server/updateNodeLogic.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/internal/logic/admin/server/updateNodeLogic.go b/internal/logic/admin/server/updateNodeLogic.go index e7ced74..582c206 100644 --- a/internal/logic/admin/server/updateNodeLogic.go +++ b/internal/logic/admin/server/updateNodeLogic.go @@ -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 From a4eca9f9a6965385062eaa7f0927c1d663ed61a0 Mon Sep 17 00:00:00 2001 From: missish <2445921920@qq.com> Date: Sat, 31 May 2025 11:49:47 +0800 Subject: [PATCH 3/4] chore: add ppanel.yaml to .gitignore for security --- .gitignore | 2 +- etc/ppanel.yaml | 0 2 files changed, 1 insertion(+), 1 deletion(-) delete mode 100644 etc/ppanel.yaml diff --git a/.gitignore b/.gitignore index 1af49f6..64c6618 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,4 @@ *.key node_modules package-lock.json -package.json \ No newline at end of file +package.jsonppanel.yaml diff --git a/etc/ppanel.yaml b/etc/ppanel.yaml deleted file mode 100644 index e69de29..0000000 From 3ee43b14c61caf70791afda96b0f4bc49129a30e Mon Sep 17 00:00:00 2001 From: missish <2445921920@qq.com> Date: Sat, 31 May 2025 11:53:40 +0800 Subject: [PATCH 4/4] Revert "chore: add ppanel.yaml to .gitignore for security" This reverts commit a4eca9f9a6965385062eaa7f0927c1d663ed61a0. --- .gitignore | 2 +- etc/ppanel.yaml | 0 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 etc/ppanel.yaml diff --git a/.gitignore b/.gitignore index 64c6618..1af49f6 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,4 @@ *.key node_modules package-lock.json -package.jsonppanel.yaml +package.json \ No newline at end of file diff --git a/etc/ppanel.yaml b/etc/ppanel.yaml new file mode 100644 index 0000000..e69de29