Skip to content

Commit

Permalink
Add any type
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Jun 14, 2024
1 parent 0c7f720 commit b24c7f3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
30 changes: 30 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
String = TypeOf("")
Bool = TypeOf(true)
Nil = nilType{}
Any = anyType{}
)

func TypeOf(v any) Type {
Expand All @@ -40,13 +41,30 @@ func TypeOf(v any) Type {
return rtype{t: reflect.TypeOf(v)}
}

type anyType struct{}

func (anyType) Nature() Nature {
return Nature{Type: nil}
}

func (anyType) Equal(t Type) bool {
return true
}

func (anyType) String() string {
return "any"
}

type nilType struct{}

func (nilType) Nature() Nature {
return Nature{Nil: true}
}

func (nilType) Equal(t Type) bool {
if t == Any {
return true
}
return t == Nil
}

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

func (r rtype) Equal(t Type) bool {
if t == Any {
return true
}
if rt, ok := t.(rtype); ok {
return r.t.String() == rt.t.String()
}
Expand All @@ -89,6 +110,9 @@ func (m Map) Nature() Nature {
}

func (m Map) Equal(t Type) bool {
if t == Any {
return true
}
mt, ok := t.(Map)
if !ok {
return false
Expand Down Expand Up @@ -129,6 +153,9 @@ func (m StrictMap) Nature() Nature {
}

func (m StrictMap) Equal(t Type) bool {
if t == Any {
return true
}
mt, ok := t.(StrictMap)
if !ok {
return false
Expand Down Expand Up @@ -171,6 +198,9 @@ func (a array) Nature() Nature {
}

func (a array) Equal(t Type) bool {
if t == Any {
return true
}
at, ok := t.(array)
if !ok {
return false
Expand Down
9 changes: 9 additions & 0 deletions types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ func TestType_Equal(t *testing.T) {
{"18", StrictMap{"foo": Int}, StrictMap{"foo": Float}, false},
{"19", Map{"foo": Map{"bar": Int}}, Map{"foo": Map{"bar": Int}}, true},
{"20", Map{"foo": Map{"bar": Int}}, Map{"foo": Map{"bar": Float}}, false},
{"21", Any, Any, true},
{"22", Any, Int, true},
{"23", Int, Any, true},
{"24", Any, Map{"foo": Int}, true},
{"25", Map{"foo": Int}, Any, true},
{"26", Any, StrictMap{"foo": Int}, true},
{"27", StrictMap{"foo": Int}, Any, true},
{"28", Any, Array(Int), true},
{"29", Array(Int), Any, true},
}

for _, tt := range tests {
Expand Down

0 comments on commit b24c7f3

Please sign in to comment.