From b24c7f3a1066a712181f1ddd27a0d6911d193542 Mon Sep 17 00:00:00 2001 From: Anton Medvedev Date: Fri, 14 Jun 2024 09:32:52 +0200 Subject: [PATCH] Add any type --- types/types.go | 30 ++++++++++++++++++++++++++++++ types/types_test.go | 9 +++++++++ 2 files changed, 39 insertions(+) diff --git a/types/types.go b/types/types.go index 52b8d9b5..92923f9a 100644 --- a/types/types.go +++ b/types/types.go @@ -31,6 +31,7 @@ var ( String = TypeOf("") Bool = TypeOf(true) Nil = nilType{} + Any = anyType{} ) func TypeOf(v any) Type { @@ -40,6 +41,20 @@ 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 { @@ -47,6 +62,9 @@ func (nilType) Nature() Nature { } func (nilType) Equal(t Type) bool { + if t == Any { + return true + } return t == Nil } @@ -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() } @@ -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 @@ -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 @@ -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 diff --git a/types/types_test.go b/types/types_test.go index 52b6c86a..eacfef22 100644 --- a/types/types_test.go +++ b/types/types_test.go @@ -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 {