forked from expr-lang/expr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
65 lines (57 loc) · 1.07 KB
/
utils.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
package builtin
import (
"fmt"
"reflect"
)
var (
anyType = reflect.TypeOf(new(any)).Elem()
integerType = reflect.TypeOf(0)
floatType = reflect.TypeOf(float64(0))
arrayType = reflect.TypeOf([]any{})
mapType = reflect.TypeOf(map[any]any{})
)
func kind(t reflect.Type) reflect.Kind {
if t == nil {
return reflect.Invalid
}
return t.Kind()
}
func types(types ...any) []reflect.Type {
ts := make([]reflect.Type, len(types))
for i, t := range types {
t := reflect.TypeOf(t)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t.Kind() != reflect.Func {
panic("not a function")
}
ts[i] = t
}
return ts
}
func deref(v reflect.Value) reflect.Value {
if v.Kind() == reflect.Interface {
if v.IsNil() {
return v
}
v = v.Elem()
}
loop:
for v.Kind() == reflect.Ptr {
if v.IsNil() {
return v
}
indirect := reflect.Indirect(v)
switch indirect.Kind() {
case reflect.Struct, reflect.Map, reflect.Array, reflect.Slice:
break loop
default:
v = v.Elem()
}
}
if v.IsValid() {
return v
}
panic(fmt.Sprintf("cannot deref %s", v))
}