2 回答

TA貢獻1780條經驗 獲得超5個贊
注意此解決方案正在使用github.com/r3labs/diff/v2
.
沒有這樣的選擇。diff 只是遞歸地處理結構字段并為每個不同的字段生成更改日志。
要實現您想要的輸出,您可以實現自己的ValueDiffer
. 這樣,您可以“原子地”區分結構并以您想要的格式附加到變更日志。
一個人為的例子,部分從包內部復制:
type DateDiffer struct {
}
// Whether this differ should be used to match a specific type
func (d *DateDiffer) Match(a, b reflect.Value) bool {
return diff.AreType(a, b, reflect.TypeOf(Date{}))
}
// The actual diff function, where you also append to the changelog
// using your custom format
func (d *DateDiffer) Diff(cl *diff.Changelog, path []string, a, b reflect.Value) error {
if a.Kind() == reflect.Invalid {
cl.Add(diff.CREATE, path, nil, b.Interface())
return nil
}
if b.Kind() == reflect.Invalid {
cl.Add(diff.DELETE, path, a.Interface(), nil)
return nil
}
var d1, d2 Date
d1, _ = a.Interface().(Date)
d2, _ = b.Interface().(Date)
if d1.Day != d2.Day || d1.Month != d2.Month || d1.Year != d2.Year {
cl.Add(diff.UPDATE, path, fmt.Sprintf("%d-%d-%d", d1.Year, d1.Month, d1.Day), fmt.Sprintf("%d-%d-%d", d2.Year, d2.Month, d2.Day))
}
return nil
}
// unsure what this is actually for, but you must implement it either way
func (d *DateDiffer) InsertParentDiffer(dfunc func(path []string, a, b reflect.Value, p interface{}) error) {
return
}
然后你這樣使用它:
d2, _ := diff.NewDiffer(diff.CustomValueDiffers(&DateDiffer{}))
s1 := Student{DateOfBirth: Date{2021, 11, 13}}
s2 := Student{DateOfBirth: Date{2021, 10, 9}}
ch2, _ := d2.Diff(s1, s2)
輸出(編組和縮進的 json):
[
{
"type": "update",
"path": [
"DateOfBirth"
],
"from": "2021-11-13",
"to": "2021-10-9"
}
]

TA貢獻1735條經驗 獲得超5個贊
經過一番研究,我找到了解決方案。
我需要為 the 創建一個自定義差異,Date并使用DisableStructValues包中的選項。
此選項很有用,因為它禁止為結構中的每個項目填充單獨的更改,并在將其與nil值進行比較時返回整個對象。
diff.Diff(
Student{DateOfBirth: Date{2021, 11, 13}},
Student{DateOfBirth: Date{2021, 10, 9}},
diff.CustomValueDiffers(differ.DateDiffer{}),
diff.DisableStructValues()
)
要實現自定義差異,需要一個實現以下接口的新結構:
type ValueDiffer interface {
Match(a, b reflect.Value) bool
Diff(cl *Changelog, path []string, a, b reflect.Value) error
InsertParentDiffer(dfunc func(path []string, a, b reflect.Value, p interface{}) error)
}
這是我的自定義不同的實現。
type DateDiffer struct {
DiffFunc (func(path []string, a, b reflect.Value, p interface{}) error)
}
func (differ DateDiffer) Match(a, b reflect.Value) bool {
return diff.AreType(a, b, reflect.TypeOf(Date{}))
}
func (differ DateDiffer) Diff(cl *diff.Changelog, path []string, a, b reflect.Value) error {
if a.Kind() == reflect.Invalid {
cl.Add(diff.CREATE, path, nil, b.Interface())
return nil
}
if b.Kind() == reflect.Invalid {
cl.Add(diff.DELETE, path, a.Interface(), nil)
return nil
}
var source, target Date
source, _ = a.Interface().(Date)
target, _ = b.Interface().(Date)
if !source.Equal(target) {
cl.Add(diff.UPDATE, path, a.Interface(), b.Interface())
}
return nil
}
func (differ DateDiffer) InsertParentDiffer(dfunc func(path []string, a, b reflect.Value, p interface{}) error) {
differ.DiffFunc = dfunc
}
希望這會幫助有類似用例的人。
- 2 回答
- 0 關注
- 165 瀏覽
添加回答
舉報