-
Notifications
You must be signed in to change notification settings - Fork 0
/
structtag.go
89 lines (79 loc) · 2.65 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
package schemer
import (
"regexp"
"strings"
)
// StructTagName is the Go struct tag name used by schemer
const StructTagName string = "schemer"
// StructTag represents information that can be parsed from a schemer struct tag
type StructTag struct {
FieldAliases []string // list of names of fields. This comes from the name of the field in the struct, and also struct tags
FieldAliasesSet bool // if true, struct tags are present on this field
Nullable bool // is this field nullable?
NullableSet bool // if true, then the null struct tags is present on this field
WeakDecoding bool // does this field allow weak decoding?
WeakDecodingSet bool // if true, weak decoding was present in the struct tag for this field
}
const tagAlias = `[A-Za-z0-9_]+`
const tagAliases = `\[(` + tagAlias + `(?:,` + tagAlias + `)*)\]`
const tagOpt = `(?:weak|null)`
// ^(\-|([A-Za-z0-9_]+)|\[([A-Za-z0-9_]+(?:,[A-Za-z0-9_]+)*)\])?(,\!?(?:weak|null))*$
// Note: non-capturing groups use regex syntax (?: ... )
// Group 1 = Alias / Aliases
// Group 2 = Single alias
// Group 3 = Multiple aliases (comma-delimited)
// Group 4 = Options (comma-delimited; prefixed with a comma)
var structTagRegex *regexp.Regexp = regexp.MustCompile(
`^(\-|(` + tagAlias + `)|` + tagAliases + `)?(,\!?` + tagOpt + `)*$`,
)
// ParseStructTag parses a struct tag string and returns a decoded StructTag
// the format of the tag must be:
// tag := alias? ("," option)*
// alias := "-" |
// identifier |
// "[" identifier ("," identifier)* "]"
// option := "!" ? ( "weak" | "null" )
// If the
func ParseStructTag(s string) (tag StructTag) {
// See `structTagRegex` documentation
match := structTagRegex.FindStringSubmatch(s)
if match == nil || len(match) != 5 {
return
}
singleAlias, aliasesStr, optsStr := match[2], match[3], match[4]
// Check to see if we are skipping this field
if match[1] == "-" {
tag.FieldAliasesSet = true
return
}
// Update aliases
if singleAlias != "" {
tag.FieldAliases = []string{singleAlias}
tag.FieldAliasesSet = true
} else if aliasesStr != "" {
tag.FieldAliases = strings.Split(aliasesStr, ",")
tag.FieldAliasesSet = true
}
if optsStr != "" {
// Parse `optsStr` by removing leading comma and splitting
opts := strings.Split(optsStr[1:], ",")
// Update options
for _, opt := range opts {
switch opt {
case "null":
tag.Nullable = true
tag.NullableSet = true
case "!null":
tag.Nullable = false
tag.NullableSet = true
case "weak":
tag.WeakDecoding = true
tag.WeakDecodingSet = true
case "!weak":
tag.WeakDecoding = false
tag.WeakDecodingSet = true
}
}
}
return
}