-
Notifications
You must be signed in to change notification settings - Fork 9
/
func.go
117 lines (93 loc) · 2.16 KB
/
func.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package stow
import (
"fmt"
"reflect"
)
type funcCall struct {
s *Store
Value reflect.Value
Type reflect.Type
hasKey bool
isKeyPtr bool
keyType reflect.Type
isValPtr bool
valType reflect.Type
}
func newFuncCall(s *Store, fn interface{}) (fc funcCall, err error) {
fc.s = s
fc.Value = reflect.ValueOf(fn)
fc.Type = fc.Value.Type()
if fc.Value.Kind() != reflect.Func {
return fc, fmt.Errorf("fn is not a func()")
}
if fc.Type.NumIn() == 1 {
fc.setValue(fc.Type.In(0))
} else if fc.Type.NumIn() == 2 {
fc.setKey(fc.Type.In(0))
fc.setValue(fc.Type.In(1))
} else {
return fc, fmt.Errorf("bad number of args in ForEach func()")
}
return fc, nil
}
func isPtr(typ reflect.Type) bool { return typ.Kind() == reflect.Ptr }
func (fc *funcCall) setKey(typ reflect.Type) {
fc.hasKey = true
fc.keyType = typ
if fc.isKeyPtr = isPtr(fc.keyType); fc.isKeyPtr {
fc.keyType = fc.keyType.Elem()
}
}
func (fc *funcCall) setValue(typ reflect.Type) {
fc.valType = typ
if fc.isValPtr = isPtr(fc.valType); fc.isValPtr {
fc.valType = fc.valType.Elem()
}
}
func (fc *funcCall) getKey(v []byte) (key reflect.Value, err error) {
if fc.keyType.Kind() == reflect.String {
return reflect.ValueOf(string(v)), nil
} else if fc.keyType.Kind() == reflect.Slice && fc.keyType.Elem().Kind() == reflect.Uint8 {
return reflect.ValueOf(v), nil
}
key = reflect.New(fc.keyType)
if err := fc.s.unmarshal(v, key.Interface()); err != nil {
return key, err
}
if !fc.isKeyPtr {
key = deref(key)
}
return key, err
}
func (fc *funcCall) getValue(v []byte) (val reflect.Value, err error) {
val = reflect.New(fc.valType)
if err := fc.s.unmarshal(v, val.Interface()); err != nil {
return val, err
}
if !fc.isValPtr {
val = deref(val)
}
return val, err
}
func (fc *funcCall) call(k, v []byte) error {
val, err := fc.getValue(v)
if err != nil {
return err
}
if !fc.hasKey {
fc.Value.Call([]reflect.Value{val})
return nil
}
key, err := fc.getKey(k)
if err != nil {
return err
}
fc.Value.Call([]reflect.Value{key, val})
return nil
}
func deref(val reflect.Value) reflect.Value {
if val.IsValid() {
return val.Elem()
}
return reflect.Zero(val.Type())
}