-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtag.go
More file actions
46 lines (35 loc) · 651 Bytes
/
tag.go
File metadata and controls
46 lines (35 loc) · 651 Bytes
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
package gofig
import (
"reflect"
"strings"
)
const omitempty = "omitempty"
// Tag is a gofig struct tag.
type Tag struct {
Name string
OmitEmpty bool
RawTag string
}
func (t Tag) String() string {
return t.RawTag
}
// TagFromStructField returns a Tag from the struct fields tag.
func TagFromStructField(field reflect.StructField, tag string) Tag {
t := Tag{
Name: field.Name,
}
if v, ok := field.Tag.Lookup(DefaultStructTag); ok {
t.RawTag = v
for i, v := range strings.Split(v, ",") {
if i == 0 {
t.Name = v
continue
}
if v == omitempty {
t.OmitEmpty = true
continue
}
}
}
return t
}