-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiface.go
73 lines (63 loc) · 1.64 KB
/
iface.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package godiff
import (
"reflect"
)
type (
ifaceDiffer struct {
config *Config
tag *Tag
}
)
func (d *ifaceDiffer) diff(changeLog *ChangeLog, path *Path, from, to interface{}, changeType ChangeType, options *Options) error {
if from == nil && to == nil {
return nil
}
var fromValue, toValue reflect.Value
var fromStruct, toStruct reflect.Type
if from != nil {
fromValue = reflect.ValueOf(from)
fromStruct = structType(fromValue.Type())
}
if to != nil {
toValue = reflect.ValueOf(to)
toStruct = structType(toValue.Type())
}
if fromStruct != nil && toStruct != nil {
differ, err := d.config.registry.Get(fromStruct, toStruct, d.tag)
if err != nil {
return err
}
if fromValue.Kind() == reflect.Ptr {
from = fromValue.Elem().Interface()
}
if toValue.Kind() == reflect.Ptr {
to = toValue.Elem().Interface()
}
return differ.diff(changeLog, path, from, to, changeType, options)
}
if from == nil && toStruct != nil {
differ, err := d.config.registry.Get(toStruct, toStruct, d.tag)
if err != nil {
return err
}
if toValue.Kind() == reflect.Ptr {
to = toValue.Elem().Interface()
}
return differ.diff(changeLog, path, from, to, ChangeTypeCreate, options)
}
if to == nil && fromStruct != nil {
differ, err := d.config.registry.Get(fromStruct, fromStruct, d.tag)
if err != nil {
return err
}
if fromValue.Kind() == reflect.Ptr {
from = fromValue.Elem().Interface()
}
return differ.diff(changeLog, path, from, to, ChangeTypeDelete, options)
}
return nil
}
func newIfaceDiffer(config *Config, tag *Tag) (*ifaceDiffer, error) {
ret := &ifaceDiffer{config: config, tag: tag}
return ret, nil
}