-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtag.go
45 lines (36 loc) · 894 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
38
39
40
41
42
43
44
45
package extra
import (
"reflect"
"strings"
)
type tagOptions string
// Credit: The Go Authors @ "encoding/json"
// parseTag splits a struct field's quirk tag into its name and
// comma-separated options.
func parseTag(tag string) (string, tagOptions) {
if idx := strings.Index(tag, ","); idx != -1 {
return tag[:idx], tagOptions(tag[idx+1:])
}
return tag, tagOptions("")
}
func gatherTags(d interface{}) []reflectSettings {
elem := reflect.ValueOf(d).Elem()
numFields := elem.NumField()
var (
tag string // stores the name of the field in Dgraph.
tags []reflectSettings
)
// loop through elements of struct.
for i := 0; i < numFields; i++ {
tag, _ = parseTag(reflect.TypeOf(d).Elem().Field(i).Tag.Get("json"))
if tag == "" {
continue
}
tags = append(tags, reflectSettings{i, tag})
}
return tags
}
type reflectSettings struct {
index int
tag string
}