-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstruct_tag.go
49 lines (42 loc) · 995 Bytes
/
struct_tag.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
package deepcopy
import (
"reflect"
"strings"
)
// fieldDetail stores field copying detail parsed from a struct field
type fieldDetail struct {
field *reflect.StructField
key string
ignored bool
required bool
done bool
index []int
nestedFields []*fieldDetail
}
// markDone sets the `done` flag of a field detail and all of its nested fields recursively
func (detail *fieldDetail) markDone() {
detail.done = true
for _, f := range detail.nestedFields {
f.markDone()
}
}
// parseTag parses struct tag for getting copying detail and configuration
func parseTag(detail *fieldDetail) {
tagValue, ok := detail.field.Tag.Lookup(defaultTagName)
detail.key = detail.field.Name
if !ok {
return
}
tags := strings.Split(tagValue, ",")
switch {
case tags[0] == "-":
detail.ignored = true
case tags[0] != "":
detail.key = tags[0]
}
for _, tagOpt := range tags[1:] {
if tagOpt == "required" && !detail.ignored {
detail.required = true
}
}
}