/
plugin_field.go
57 lines (52 loc) · 1.33 KB
/
plugin_field.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
package summer
import (
"reflect"
"strconv"
"strings"
)
type FieldReferencePlugin struct {
}
func (plugin *FieldReferencePlugin) Look(holder *Holder, path string, sf *reflect.StructField) reflect.Value {
stack := strings.Split(path, ".")
logger.Debug("[ref]", path, holder.Type)
foundHolder := holder.Basket.GetStoneHolderWithName(stack[0])
if foundHolder == nil {
panic("the " + stack[0] + " not found")
}
holder.Dependencies[foundHolder] = true
root := foundHolder.Stone
value := reflect.ValueOf(root)
for index, name := range stack {
if index == 0 {
continue
}
value = plugin.lookChildren(value, name)
}
return value
}
func (plugin *FieldReferencePlugin) lookChildren(parent reflect.Value, childrenName string) reflect.Value {
pKind := parent.Kind()
if pKind == reflect.Ptr {
return plugin.lookChildren(parent.Elem(), childrenName)
}
if pKind == reflect.Struct {
return parent.FieldByName(childrenName)
}
if pKind == reflect.Map {
return parent.MapIndex(reflect.ValueOf(childrenName))
}
if pKind == reflect.Array || pKind == reflect.Slice {
c, err := strconv.Atoi(childrenName)
if err != nil {
panic(err)
}
return parent.Index(c)
}
panic("sorry i dont know what happended")
}
func (plugin *FieldReferencePlugin) Prefix() string {
return "$"
}
func (plugin *FieldReferencePlugin) ZIndex() int {
return 1
}