forked from r3labs/diff
/
patch_struct.go
43 lines (36 loc) · 957 Bytes
/
patch_struct.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
package diff
import "reflect"
/**
Types are being split out to more closely follow the library structure already
in place. Keeps the file simpler as well.
*/
//patchStruct - handles the rendering of a struct field
func (c *ChangeValue) patchStruct() {
field := c.change.Path[c.pos]
for i := 0; i < c.target.NumField(); i++ {
f := c.target.Type().Field(i)
tname := tagName("diff", f)
if tname == "-" {
continue
}
if tname == field || f.Name == field {
x := c.target.Field(i)
if hasTagOption("diff", f, "nocreate") {
c.SetFlag(OptionNoCreate)
}
if hasTagOption("diff", f, "omitunequal") {
c.SetFlag(OptionOmitUnequal)
}
if hasTagOption("diff", f, "immutable") {
c.SetFlag(OptionImmutable)
}
c.swap(&x)
break
}
}
}
//track and zero out struct members
func (c *ChangeValue) deleteStructEntry() {
//deleting a struct value set's it to the 'basic' type
c.Set(reflect.Zero(c.target.Type()))
}