forked from r3labs/diff
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff_struct.go
More file actions
Latest commit
105 lines (79 loc) · 1.86 KB
/
Copy pathdiff_struct.go
File metadata and controls
105 lines (79 loc) · 1.86 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package diff
import (
"reflect"
"time"
)
func (d*Differ) diffStruct(path []string, a, b reflect.Value) error {
ifAreType(a, b, reflect.TypeOf(time.Time{})) {
returnd.diffTime(path, a, b)
}
ifa.Kind() ==reflect.Invalid {
ifd.DisableStructValues {
d.cl.Add(CREATE, path, nil, b.Interface())
returnnil
}
returnd.structValues(CREATE, path, b)
}
ifb.Kind() ==reflect.Invalid {
ifd.DisableStructValues {
d.cl.Add(DELETE, path, a.Interface(), nil)
returnnil
}
returnd.structValues(DELETE, path, a)
}
fori:=0; i<a.NumField(); i++ {
field:=a.Type().Field(i)
tname:=tagName(field)
iftname=="-"||hasTagOption(field, "immutable") {
continue
}
iftname=="" {
tname=field.Name
}
af:=a.Field(i)
bf:=b.FieldByName(field.Name)
fpath:=copyAppend(path, tname)
err:=d.diff(fpath, af, bf)
iferr!=nil {
returnerr
}
}
returnnil
}
func (d*Differ) structValues(tstring, path []string, a reflect.Value) error {
varndDiffer
ift!=CREATE&&t!=DELETE {
returnErrInvalidChangeType
}
ifa.Kind() ==reflect.Ptr {
a=reflect.Indirect(a)
}
ifa.Kind() !=reflect.Struct {
returnErrTypeMismatch
}
x:=reflect.New(a.Type()).Elem()
fori:=0; i<a.NumField(); i++ {
field:=a.Type().Field(i)
tname:=tagName(field)
iftname=="-" {
continue
}
iftname=="" {
tname=field.Name
}
af:=a.Field(i)
xf:=x.FieldByName(field.Name)
fpath:=copyAppend(path, tname)
err:=nd.diff(fpath, xf, af)
iferr!=nil {
returnerr
}
}
fori:=0; i<len(nd.cl); i++ {
(d.cl) =append(d.cl, swapChange(t, nd.cl[i]))
}
returnnil
}