-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtag.go
37 lines (33 loc) · 999 Bytes
/
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
package dotconfig
import "strings"
// tagOptions is the string following a comma in a struct field's "env"
// tag, or an empty string.
// Borrows HEAVILY from:
// https://cs.opensource.google/go/go/+/master:src/encoding/json/tags.go;bpv=0;bpt=1
// Main difference is I'm trimming the options so this is valid:
//
// type myStruct struct {
// MaxBytesPerRequest int `env:"MAX_BYTES_PER_REQUEST, optionHasASpaceAfter"`
// }
type tagOptions string
// parseTag splits a struct field's json tag into its name and
// comma-separated options.
func parseTag(tag string) (string, tagOptions) {
tag, opt, _ := strings.Cut(tag, ",")
return tag, tagOptions(opt)
}
// Contains reports whether our comma-separated options contains a given option.
func (o tagOptions) Contains(optionName string) bool {
if len(o) == 0 {
return false
}
s := string(o)
for s != "" {
var name string
name, s, _ = strings.Cut(s, ",")
if strings.TrimSpace(name) == optionName {
return true
}
}
return false
}