|
1 | 1 | package types
|
2 | 2 |
|
3 |
| -type Map map[string]any |
| 3 | +import ( |
| 4 | + "reflect" |
4 | 5 |
|
5 |
| -type StrictMap map[string]any |
| 6 | + . "github.com/expr-lang/expr/checker/nature" |
| 7 | +) |
| 8 | + |
| 9 | +func TypeOf(v any) Type { |
| 10 | + return rtype{t: reflect.TypeOf(v)} |
| 11 | +} |
| 12 | + |
| 13 | +var ( |
| 14 | + Int = TypeOf(0) |
| 15 | + Int8 = TypeOf(int8(0)) |
| 16 | + Int16 = TypeOf(int16(0)) |
| 17 | + Int32 = TypeOf(int32(0)) |
| 18 | + Int64 = TypeOf(int64(0)) |
| 19 | + Uint = TypeOf(uint(0)) |
| 20 | + Uint8 = TypeOf(uint8(0)) |
| 21 | + Uint16 = TypeOf(uint16(0)) |
| 22 | + Uint32 = TypeOf(uint32(0)) |
| 23 | + Uint64 = TypeOf(uint64(0)) |
| 24 | + Float = TypeOf(float32(0)) |
| 25 | + Float64 = TypeOf(float64(0)) |
| 26 | + String = TypeOf("") |
| 27 | + Bool = TypeOf(true) |
| 28 | + Nil = nilType{} |
| 29 | +) |
| 30 | + |
| 31 | +type Type interface { |
| 32 | + Nature() Nature |
| 33 | +} |
| 34 | + |
| 35 | +type nilType struct{} |
| 36 | + |
| 37 | +func (nilType) Nature() Nature { |
| 38 | + return Nature{Nil: true} |
| 39 | +} |
| 40 | + |
| 41 | +type rtype struct { |
| 42 | + t reflect.Type |
| 43 | +} |
| 44 | + |
| 45 | +func (r rtype) Nature() Nature { |
| 46 | + return Nature{Type: r.t} |
| 47 | +} |
| 48 | + |
| 49 | +type Map map[string]Type |
| 50 | + |
| 51 | +func (m Map) Nature() Nature { |
| 52 | + nt := Nature{ |
| 53 | + Type: reflect.TypeOf(map[string]any{}), |
| 54 | + Fields: make(map[string]Nature, len(m)), |
| 55 | + } |
| 56 | + for k, v := range m { |
| 57 | + nt.Fields[k] = v.Nature() |
| 58 | + } |
| 59 | + return nt |
| 60 | +} |
| 61 | + |
| 62 | +type StrictMap map[string]Type |
| 63 | + |
| 64 | +func (m StrictMap) Nature() Nature { |
| 65 | + nt := Nature{ |
| 66 | + Type: reflect.TypeOf(map[string]any{}), |
| 67 | + Fields: make(map[string]Nature, len(m)), |
| 68 | + Strict: true, |
| 69 | + } |
| 70 | + for k, v := range m { |
| 71 | + nt.Fields[k] = v.Nature() |
| 72 | + } |
| 73 | + return nt |
| 74 | +} |
0 commit comments