forked from fatih/structs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tags.go
40 lines (32 loc) · 798 Bytes
/
tags.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
package structure
import "strings"
// tagOptions contains a slice of tag options
type tagOptions []string
// Has returns true if the given opt is available inside the slice.
func (t tagOptions) Has(opt string) bool {
for _, tagOpt := range t {
if tagOpt == opt {
return true
}
}
return false
}
// parseTag splits a struct field's tag into its name and a list of options
// which comes after a name. A tag is in the form of: "name,option1,option2".
// The name can be neglectected.
func parseTag(tag string) (string, tagOptions) {
res := strings.Split(tag, ",")
// tag = ""
if len(res) == 0 {
return tag, res
}
// tag = "name"
if len(res) == 1 {
return tag, res[1:]
}
// tag is one of followings:
// "name,opt"
// "name,opt,opt2"
// ",opt"
return res[0], res[1:]
}