wrapper/tool/diff.go

69 lines
1.7 KiB
Go
Raw Permalink Normal View History

2024-03-08 11:33:54 +08:00
// Package tool ...
//
// Description : 对比两个数据是否相同 + 构建不同数据的前后值
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-03-08 11:03
package tool
2024-03-08 15:56:53 +08:00
import (
"git.zhangdeman.cn/zhangdeman/wrapper"
"git.zhangdeman.cn/zhangdeman/wrapper/tool/define"
)
2024-03-08 11:33:54 +08:00
var (
Diff = &diff{}
)
type diff struct {
}
2024-03-08 15:56:53 +08:00
// 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 {
2024-03-08 15:58:58 +08:00
res[itemField] = d.CompareSingle(itemField, input, storage, option)
2024-03-08 15:56:53 +08:00
}
return res
}
2024-03-08 15:58:58 +08:00
// CompareSingle 比较一个字段
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:57 2024/3/8
func (d *diff) CompareSingle(field string, input wrapper.Map, storage wrapper.Map, option *define.DiffOption) *define.DiffResult {
if nil == option {
option = define.NewDiffOption()
}
if compareFunc, exist := option.CustomDiffFuncTable[field]; exist && nil != compareFunc {
return compareFunc(field, input, storage, option)
} else {
return define.DefaultDiffFunc(field, input, storage, option)
}
}
2024-03-11 11:18:25 +08:00
// IsSame 判断连个数据是否一致
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:16 2024/3/11
func (d *diff) IsSame(fieldList []string, input wrapper.Map, storage wrapper.Map, option *define.DiffOption) bool {
res := d.Compare(fieldList, input, storage, option)
for _, item := range res {
if item.IsSame {
continue
}
return false
}
return true
}