mirror of
https://github.com/hanzoai/expr.git
synced 2026-09-24 16:58:58 +00:00
Improve types pkg (#668)
This commit is contained in:
@@ -1074,7 +1074,7 @@ func TestCheck_types(t *testing.T) {
|
||||
env := types.Map{
|
||||
"foo": types.StrictMap{
|
||||
"bar": types.Map{
|
||||
"baz": "",
|
||||
"baz": types.String,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
package nature
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/expr-lang/expr/types"
|
||||
)
|
||||
|
||||
func Of(value any) Nature {
|
||||
if value == nil {
|
||||
return Nature{Nil: true}
|
||||
}
|
||||
|
||||
v := reflect.ValueOf(value)
|
||||
|
||||
switch v.Kind() {
|
||||
case reflect.Map:
|
||||
_, strict := value.(types.StrictMap)
|
||||
fields := make(map[string]Nature, v.Len())
|
||||
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.(type) {
|
||||
case types.Map, types.StrictMap:
|
||||
fields[key.String()] = Of(face)
|
||||
default:
|
||||
if face == nil {
|
||||
fields[key.String()] = Nature{Nil: true}
|
||||
continue
|
||||
}
|
||||
fields[key.String()] = Nature{Type: reflect.TypeOf(face)}
|
||||
|
||||
}
|
||||
}
|
||||
return Nature{
|
||||
Type: v.Type(),
|
||||
Fields: fields,
|
||||
Strict: strict,
|
||||
}
|
||||
}
|
||||
|
||||
return Nature{Type: v.Type()}
|
||||
}
|
||||
Reference in New Issue
Block a user