wrapper/tool/diff.go

69 lines
1.7 KiB
Go

// Package tool ...
//
// Description : 对比两个数据是否相同 + 构建不同数据的前后值
//
// Author : go_developer@163.com<白茶清欢>
//
// 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 {
res[itemField] = d.CompareSingle(itemField, input, storage, option)
}
return res
}
// 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)
}
}
// 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
}