我遇到github.com/r3labs/diff了 Go 語言的庫來比較兩個相同型別的結構。
庫作業得很好,除了以下一個用例:我使用Date結構來表示日期:
type Date struct {
Year int
Month int
Day int
}
現在,還有一些其他更復雜的結構使用Date結構,例如:
type Student struct {
DateOfBirth Date
}
如果我是關于比較兩個學生,比如
diff.Diff(
Student{DateOfBirth: Date{2021, 11, 13}},
Student{DateOfBirth: Date{2021, 10, 9}},
)
我會得到的結果與2項,一個用于更新日志DateOfBirth > Month和其他的DateOfBirth > Day。
我想要的結果是一個包含單個專案 ( DateOfBirth) 和2021-10-09.
圖書館有可能嗎?
uj5u.com熱心網友回復:
注意此解決方案使用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"
}
]
uj5u.com熱心網友回復:
經過一番研究,我找到了解決方案。
我需要為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
}
希望這會對有類似用例的人有所幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/362127.html
上一篇:go背景關系取消功能的最佳實踐
