forked from expr-lang/expr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.go
68 lines (54 loc) · 1.15 KB
/
env.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
package conf
import (
"fmt"
"reflect"
. "github.com/expr-lang/expr/checker/nature"
"github.com/expr-lang/expr/internal/deref"
"github.com/expr-lang/expr/types"
)
func Env(env any) Nature {
if env == nil {
return Nature{
Type: reflect.TypeOf(map[string]any{}),
Strict: true,
}
}
switch env := env.(type) {
case types.Map:
return env.Nature()
}
v := reflect.ValueOf(env)
d := deref.Value(v)
switch d.Kind() {
case reflect.Struct:
return Nature{
Type: v.Type(),
Strict: true,
}
case reflect.Map:
n := Nature{
Type: v.Type(),
Fields: make(map[string]Nature, v.Len()),
Strict: true,
}
for _, key := range v.MapKeys() {
elem := v.MapIndex(key)
if !elem.IsValid() || !elem.CanInterface() {
panic(fmt.Sprintf("invalid map value: %s", key))
}
face := elem.Interface()
switch face := face.(type) {
case types.Map:
n.Fields[key.String()] = face.Nature()
default:
if face == nil {
n.Fields[key.String()] = Nature{Nil: true}
continue
}
n.Fields[key.String()] = Nature{Type: reflect.TypeOf(face)}
}
}
return n
}
panic(fmt.Sprintf("unknown type %T", env))
}