Skip to content

Commit b24c7f3

Browse files
committed
Add any type
1 parent 0c7f720 commit b24c7f3

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

types/types.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ var (
3131
String = TypeOf("")
3232
Bool = TypeOf(true)
3333
Nil = nilType{}
34+
Any = anyType{}
3435
)
3536

3637
func TypeOf(v any) Type {
@@ -40,13 +41,30 @@ func TypeOf(v any) Type {
4041
return rtype{t: reflect.TypeOf(v)}
4142
}
4243

44+
type anyType struct{}
45+
46+
func (anyType) Nature() Nature {
47+
return Nature{Type: nil}
48+
}
49+
50+
func (anyType) Equal(t Type) bool {
51+
return true
52+
}
53+
54+
func (anyType) String() string {
55+
return "any"
56+
}
57+
4358
type nilType struct{}
4459

4560
func (nilType) Nature() Nature {
4661
return Nature{Nil: true}
4762
}
4863

4964
func (nilType) Equal(t Type) bool {
65+
if t == Any {
66+
return true
67+
}
5068
return t == Nil
5169
}
5270

@@ -63,6 +81,9 @@ func (r rtype) Nature() Nature {
6381
}
6482

6583
func (r rtype) Equal(t Type) bool {
84+
if t == Any {
85+
return true
86+
}
6687
if rt, ok := t.(rtype); ok {
6788
return r.t.String() == rt.t.String()
6889
}
@@ -89,6 +110,9 @@ func (m Map) Nature() Nature {
89110
}
90111

91112
func (m Map) Equal(t Type) bool {
113+
if t == Any {
114+
return true
115+
}
92116
mt, ok := t.(Map)
93117
if !ok {
94118
return false
@@ -129,6 +153,9 @@ func (m StrictMap) Nature() Nature {
129153
}
130154

131155
func (m StrictMap) Equal(t Type) bool {
156+
if t == Any {
157+
return true
158+
}
132159
mt, ok := t.(StrictMap)
133160
if !ok {
134161
return false
@@ -171,6 +198,9 @@ func (a array) Nature() Nature {
171198
}
172199

173200
func (a array) Equal(t Type) bool {
201+
if t == Any {
202+
return true
203+
}
174204
at, ok := t.(array)
175205
if !ok {
176206
return false

types/types_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ func TestType_Equal(t *testing.T) {
3333
{"18", StrictMap{"foo": Int}, StrictMap{"foo": Float}, false},
3434
{"19", Map{"foo": Map{"bar": Int}}, Map{"foo": Map{"bar": Int}}, true},
3535
{"20", Map{"foo": Map{"bar": Int}}, Map{"foo": Map{"bar": Float}}, false},
36+
{"21", Any, Any, true},
37+
{"22", Any, Int, true},
38+
{"23", Int, Any, true},
39+
{"24", Any, Map{"foo": Int}, true},
40+
{"25", Map{"foo": Int}, Any, true},
41+
{"26", Any, StrictMap{"foo": Int}, true},
42+
{"27", StrictMap{"foo": Int}, Any, true},
43+
{"28", Any, Array(Int), true},
44+
{"29", Array(Int), Any, true},
3645
}
3746

3847
for _, tt := range tests {

0 commit comments

Comments
 (0)