增加比较两个map某一个字段值是否相等的能力 #4

Merged
zhangdeman merged 5 commits from feature/diff into master 2024-03-08 16:00:00 +08:00
2 changed files with 27 additions and 0 deletions
Showing only changes of commit 4d25a2a650 - Show all commits

View File

@ -69,6 +69,7 @@ type CustomDiffFunc func(field string, inputVal wrapper.Map, storageVal wrapper.
// //
// Date : 11:10 2024/3/8 // Date : 11:10 2024/3/8
type DiffResult struct { type DiffResult struct {
Field string `json:"field"` // 字段名
OldVal interface{} `json:"old_val"` // 当前field在storageVal中的值 OldVal interface{} `json:"old_val"` // 当前field在storageVal中的值
NewVal interface{} `json:"new_val"` // 当前field在inputVal中的值 NewVal interface{} `json:"new_val"` // 当前field在inputVal中的值
IsSame bool `json:"is_same"` // 两个值是否相同 IsSame bool `json:"is_same"` // 两个值是否相同
@ -126,6 +127,7 @@ func DefaultDiffFunc(field string, inputVal wrapper.Map, storageVal wrapper.Map,
option = NewDiffOption() option = NewDiffOption()
} }
result := &DiffResult{ result := &DiffResult{
Field: field,
OldVal: nil, OldVal: nil,
NewVal: nil, NewVal: nil,
IsSame: true, IsSame: true,

View File

@ -7,9 +7,34 @@
// Date : 2024-03-08 11:03 // Date : 2024-03-08 11:03
package tool package tool
import (
"git.zhangdeman.cn/zhangdeman/wrapper"
"git.zhangdeman.cn/zhangdeman/wrapper/tool/define"
)
var ( var (
Diff = &diff{} Diff = &diff{}
) )
type diff struct { 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
}