/
structtag.go
94 lines (77 loc) · 2.11 KB
/
structtag.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package generator
// copied from https://github.com/fatih/structtag/blob/master/tags.go
import (
"errors"
"strconv"
"strings"
)
var (
errTagSyntax = errors.New("bad syntax for struct tag pair")
errTagKeySyntax = errors.New("bad syntax for struct tag key")
errTagValueSyntax = errors.New("bad syntax for struct tag value")
errKeyNotSet = errors.New("tag key does not exist")
errTagNotExist = errors.New("tag does not exist")
errTagKeyMismatch = errors.New("mismatch between key and tag.key")
)
// parseStructTag parses a single struct field tag and returns the set of tags.
func parseStructTag(tag string) (map[string][]string, error) {
tags := map[string][]string{}
hasTag := tag != ""
// NOTE(arslan) following code is from reflect and vet package with some
// modifications to collect all necessary information and extend it with
// usable methods
for tag != "" {
// Skip leading space.
i := 0
for i < len(tag) && tag[i] == ' ' {
i++
}
tag = tag[i:]
if tag == "" {
break
}
// Scan to colon. A space, a quote or a control character is a syntax
// error. Strictly speaking, control chars include the range [0x7f,
// 0x9f], not just [0x00, 0x1f], but in practice, we ignore the
// multi-byte control characters as it is simpler to inspect the tag's
// bytes than the tag's runes.
i = 0
for i < len(tag) && tag[i] > ' ' && tag[i] != ':' && tag[i] != '"' && tag[i] != 0x7f {
i++
}
if i == 0 {
return nil, errTagKeySyntax
}
if i+1 >= len(tag) || tag[i] != ':' {
return nil, errTagSyntax
}
if tag[i+1] != '"' {
return nil, errTagValueSyntax
}
key := string(tag[:i])
tag = tag[i+1:]
// Scan quoted string to find value.
i = 1
for i < len(tag) && tag[i] != '"' {
if tag[i] == '\\' {
i++
}
i++
}
if i >= len(tag) {
return nil, errTagValueSyntax
}
qvalue := string(tag[:i+1])
tag = tag[i+1:]
value, err := strconv.Unquote(qvalue)
if err != nil {
return nil, errTagValueSyntax
}
options := strings.Split(value, ",")
tags[key] = options
}
if hasTag && len(tags) == 0 {
return nil, nil
}
return tags, nil
}