-
Notifications
You must be signed in to change notification settings - Fork 180
/
diff.go
47 lines (37 loc) · 1.29 KB
/
diff.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package registry
import (
"github.com/pydio/cells/v4/common/config"
"github.com/r3labs/diff/v3"
"reflect"
)
func init() {
config.RegisterCustomValueDiffer(&customValueDiffer{})
}
type customValueDiffer struct {
DiffFunc func(path []string, a, b reflect.Value, p interface{}) error
}
func (c *customValueDiffer) Match(a, b reflect.Value) bool {
if a.IsValid() && a.CanInterface() && b.IsValid() && b.CanInterface() {
_, okA := a.Interface().(Item)
_, okB := b.Interface().(Item)
return okA && okB
}
return false
}
func (c *customValueDiffer) Diff(dt diff.DiffType, df diff.DiffFunc, cl *diff.Changelog, path []string, a, b reflect.Value, parent interface{}) error {
sa := a.Interface().(Item)
sb := b.Interface().(Item)
if err := c.DiffFunc(append(path, "id"), reflect.ValueOf(sa.ID()), reflect.ValueOf(sb.ID()), parent); err != nil {
return err
}
if err := c.DiffFunc(append(path, "name"), reflect.ValueOf(sa.Name()), reflect.ValueOf(sb.Name()), parent); err != nil {
return err
}
if err := c.DiffFunc(append(path, "metadata"), reflect.ValueOf(sa.Metadata()), reflect.ValueOf(sb.Metadata()), parent); err != nil {
return err
}
return nil
}
func (c *customValueDiffer) InsertParentDiffer(dfunc func(path []string, a, b reflect.Value, p interface{}) error) {
c.DiffFunc = dfunc
}