From 4d25a2a6500d4a358492f0600a16382d40ec4997 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 8 Mar 2024 15:56:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E5=A4=96=E6=9A=B4=E9=9C=B2compare?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tool/define/diff.go | 2 ++ tool/diff.go | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/tool/define/diff.go b/tool/define/diff.go index 8b63d9e..e8a969c 100644 --- a/tool/define/diff.go +++ b/tool/define/diff.go @@ -69,6 +69,7 @@ type CustomDiffFunc func(field string, inputVal wrapper.Map, storageVal wrapper. // // Date : 11:10 2024/3/8 type DiffResult struct { + Field string `json:"field"` // 字段名 OldVal interface{} `json:"old_val"` // 当前field在storageVal中的值 NewVal interface{} `json:"new_val"` // 当前field在inputVal中的值 IsSame bool `json:"is_same"` // 两个值是否相同 @@ -126,6 +127,7 @@ func DefaultDiffFunc(field string, inputVal wrapper.Map, storageVal wrapper.Map, option = NewDiffOption() } result := &DiffResult{ + Field: field, OldVal: nil, NewVal: nil, IsSame: true, diff --git a/tool/diff.go b/tool/diff.go index 9e3e575..1b47618 100644 --- a/tool/diff.go +++ b/tool/diff.go @@ -7,9 +7,34 @@ // Date : 2024-03-08 11:03 package tool +import ( + "git.zhangdeman.cn/zhangdeman/wrapper" + "git.zhangdeman.cn/zhangdeman/wrapper/tool/define" +) + var ( Diff = &diff{} ) type diff struct { } + +// Compare 比较两个数据源的指定字段 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 15:53 2024/3/8 +func (d *diff) Compare(fieldList []string, input wrapper.Map, storage wrapper.Map, option *define.DiffOption) map[string]*define.DiffResult { + if nil == option { + option = define.NewDiffOption() + } + res := make(map[string]*define.DiffResult) + for _, itemField := range fieldList { + if compareFunc, exist := option.CustomDiffFuncTable[itemField]; exist && nil != compareFunc { + res[itemField] = compareFunc(itemField, input, storage, option) + } else { + res[itemField] = define.DefaultDiffFunc(itemField, input, storage, option) + } + } + return res +}