-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflection.go
95 lines (84 loc) · 2.5 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package gats
import (
"exp/html"
"fmt"
"reflect"
"unsafe"
)
type context struct {
enclosure *context
data interface{}
}
func makeContext(data interface{}, parent *context) *context {
return &context{
enclosure: parent,
data: data,
}
}
func getBool(name string, cont *context) (bool, error) {
field := getField(name, cont)
if field.IsValid() {
return field.Bool(), nil
}
return false, fmt.Errorf("No field named %v in the data (or no data provided)", name)
}
func getString(name string, cont *context) (string, error) {
field := getField(name, cont)
if field.IsValid() {
return field.String(), nil
}
return "", fmt.Errorf("No field named %v in the data (or no data provided)", name)
}
func isString(name string, cont *context) bool {
return getField(name, cont).Type().String() == "string"
}
func getStringMap(name string, cont *context) (map[string]string, error) {
field := getField(name, cont)
if field.IsValid() {
results := make(map[string]string)
for _, k := range field.MapKeys() {
results[k.String()] = field.MapIndex(k).String()
}
return results, nil
}
return nil, fmt.Errorf("No field named %v in the data (or no data provided)", name)
}
func getHtmlNode(name string, cont *context) (*html.Node, error) {
field := getField(name, cont)
if field.IsValid() {
return (*html.Node)(unsafe.Pointer(field.Pointer())), nil
}
return nil, fmt.Errorf("No field named %v in the data (or no data provided)", name)
}
func getLength(name string, cont *context) (int, error) {
field := getField(name, cont)
if field.IsValid() {
return field.Len(), nil
}
return -1, fmt.Errorf("No field named %v in the data (or no data provided)", name)
}
func getItem(name string, index int, cont *context) (*context, error) {
field := getField(name, cont)
if field.IsValid() {
if 0 <= index && index <= field.Len() {
return makeContext(field.Index(index).Interface(), cont), nil
}
return nil, fmt.Errorf("Index %v out of bounds.", index)
}
return nil, fmt.Errorf("No field named %v in the data (or no data provided)", name)
}
func getField(name string, cont *context) (result reflect.Value) {
if cont == nil {
return reflect.Zero(reflect.TypeOf(name)) // any value of zero is fine here, all are !IsValid
}
switch reflect.ValueOf(cont.data).Kind().String() {
case "ptr":
result = reflect.ValueOf(cont.data).Elem().FieldByName(name)
case "struct":
result = reflect.ValueOf(cont.data).FieldByName(name)
}
if result.IsValid() {
return result
}
return getField(name, cont.enclosure)
}