Open s3rj1k opened 2 years ago
To accomplish this, I implemented a custom ValueDiffer
which looks like the following:
Instantiation
type NetIPDiffer struct {
DiffFunc (func(path []string, a, b reflect.Value, p interface{}) error)
}
func (differ NetIPDiffer) Match(a, b reflect.Value) bool {
return diff.AreType(a, b, reflect.TypeOf(net.IP{}))
}
func (differ NetIPDiffer) Diff(dt diff.DiffType, df diff.DiffFunc, cl *diff.Changelog, path []string, a, b reflect.Value, p interface{}) 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
}
aString := a.Interface().(net.IP).String()
bString := b.Interface().(net.IP).String()
if aString != bString {
cl.Add(diff.UPDATE, path, aString, bString)
}
return nil
}
func (differ NetIPDiffer) InsertParentDiffer(dfunc func(path []string, a, b reflect.Value, p interface{}) error) {
differ.DiffFunc = dfunc
}
Use
d, err := diff.NewDiffer(
diff.DisableStructValues(),
diff.SliceOrdering(true),
diff.CustomValueDiffers(&NetIPDiffer{}),
)
For reference, I looked at the internal diff_test.go
file to put this stuff together.
@tyliec nice, I think this should be in docs at least
How do I force differ function so it would compare net.IP as a string and not as a slice?
https://go.dev/play/p/0VMlM5RQ9WO