-
-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
150 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
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() | ||
|
||
case types.StrictMap: | ||
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()), | ||
} | ||
|
||
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: | ||
n.Fields[key.String()] = face.(types.Map).Nature() | ||
|
||
case types.StrictMap: | ||
n.Fields[key.String()] = face.(types.StrictMap).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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,74 @@ | ||
package types | ||
|
||
type Map map[string]any | ||
import ( | ||
"reflect" | ||
|
||
type StrictMap map[string]any | ||
. "github.com/expr-lang/expr/checker/nature" | ||
) | ||
|
||
func TypeOf(v any) Type { | ||
return rtype{t: reflect.TypeOf(v)} | ||
} | ||
|
||
var ( | ||
Int = TypeOf(0) | ||
Int8 = TypeOf(int8(0)) | ||
Int16 = TypeOf(int16(0)) | ||
Int32 = TypeOf(int32(0)) | ||
Int64 = TypeOf(int64(0)) | ||
Uint = TypeOf(uint(0)) | ||
Uint8 = TypeOf(uint8(0)) | ||
Uint16 = TypeOf(uint16(0)) | ||
Uint32 = TypeOf(uint32(0)) | ||
Uint64 = TypeOf(uint64(0)) | ||
Float = TypeOf(float32(0)) | ||
Float64 = TypeOf(float64(0)) | ||
String = TypeOf("") | ||
Bool = TypeOf(true) | ||
Nil = nilType{} | ||
) | ||
|
||
type Type interface { | ||
Nature() Nature | ||
} | ||
|
||
type nilType struct{} | ||
|
||
func (nilType) Nature() Nature { | ||
return Nature{Nil: true} | ||
} | ||
|
||
type rtype struct { | ||
t reflect.Type | ||
} | ||
|
||
func (r rtype) Nature() Nature { | ||
return Nature{Type: r.t} | ||
} | ||
|
||
type Map map[string]Type | ||
|
||
func (m Map) Nature() Nature { | ||
nt := Nature{ | ||
Type: reflect.TypeOf(map[string]any{}), | ||
Fields: make(map[string]Nature, len(m)), | ||
} | ||
for k, v := range m { | ||
nt.Fields[k] = v.Nature() | ||
} | ||
return nt | ||
} | ||
|
||
type StrictMap map[string]Type | ||
|
||
func (m StrictMap) Nature() Nature { | ||
nt := Nature{ | ||
Type: reflect.TypeOf(map[string]any{}), | ||
Fields: make(map[string]Nature, len(m)), | ||
Strict: true, | ||
} | ||
for k, v := range m { | ||
nt.Fields[k] = v.Nature() | ||
} | ||
return nt | ||
} |