forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflection.go
61 lines (49 loc) · 1.16 KB
/
reflection.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
package quickfix
import (
"reflect"
"strconv"
"strings"
)
func parseStructTag(goTag string) (tag Tag, omitEmpty bool, defaultVal *string) {
tagParts := strings.Split(goTag, ",")
if fixTag, err := strconv.Atoi(tagParts[0]); err != nil {
panic(err)
} else {
tag = Tag(fixTag)
}
if len(tagParts) == 1 {
return
}
if tagParts[1] == "omitempty" {
omitEmpty = true
} else {
d := strings.TrimPrefix(tagParts[1], "default=")
if d != tagParts[1] {
defaultVal = &d
}
}
return
}
func buildGroupTemplate(elem reflect.Type) GroupTemplate {
template := make(GroupTemplate, 0)
var walkFunc func(t reflect.Type)
walkFunc = func(t reflect.Type) {
for i := 0; i < t.NumField(); i++ {
sf := t.Field(i)
elementType := sf.Type
if elementType.Kind() == reflect.Ptr {
elementType = elementType.Elem()
}
// recurse if item is a component, optional or not
structTag := sf.Tag.Get("fix")
if structTag == "" && elementType.Kind() == reflect.Struct {
walkFunc(elementType)
continue
}
afixTag, _, _ := parseStructTag(structTag)
template = append(template, GroupElement(Tag(afixTag)))
}
}
walkFunc(elem)
return template
}