From bc3090ad401d1d388140b4b2f2164fd3a4dbd218 Mon Sep 17 00:00:00 2001 From: Graza Date: Mon, 18 Dec 2023 15:12:26 +0000 Subject: [PATCH] feat: added type checks --- types/type_checks.go | 40 +++++++++++++ types/type_checks_test.go | 118 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 types/type_checks.go create mode 100644 types/type_checks_test.go diff --git a/types/type_checks.go b/types/type_checks.go new file mode 100644 index 0000000..c40ed29 --- /dev/null +++ b/types/type_checks.go @@ -0,0 +1,40 @@ +package types + +import "reflect" + +func IsSimpleType(v any) bool { + kind := reflect.TypeOf(v).Kind() + switch kind { + case + reflect.Bool, + reflect.String, + reflect.Float32, + reflect.Float64, + reflect.Int, + reflect.Int8, + reflect.Int16, + reflect.Int32, + reflect.Int64, + reflect.Uint, + reflect.Uint8, + reflect.Uint16, + reflect.Uint32, + reflect.Uint64: + return true + default: + return false + } +} + +func IsCollectionType(v any) bool { + kind := reflect.TypeOf(v).Kind() + switch kind { + case + reflect.Slice, + reflect.Array, + reflect.Map: + return true + default: + return false + } +} diff --git a/types/type_checks_test.go b/types/type_checks_test.go new file mode 100644 index 0000000..e36af73 --- /dev/null +++ b/types/type_checks_test.go @@ -0,0 +1,118 @@ +package types + +import "testing" + +type typeCheckTest struct { + name string + input any + expected bool +} + +type typeCheckInput struct { + _int int + _int8 int8 + _int16 int16 + _int32 int32 + _int64 int64 + _uint uint + _uint8 uint8 + _uint16 uint16 + _uint32 uint32 + _uint64 uint64 + _float32 float32 + _float64 float64 + _byte byte // is this required as it's an alias for uint8? + _bool bool + _slice []string + _array [3]string + _map map[string]string + _struct structType + _enum enumType + _func func() +} + +var typeCheckInputData = typeCheckInput{ + _int: 100, + _int8: int8(100), + _int16: int16(100), + _int32: int32(100), + _int64: int64(100), + _uint: uint(100), + _uint8: uint8(100), + _uint16: uint16(100), + _uint32: uint32(100), + _uint64: uint64(100), + _float32: float32(100.1234), + _float64: float64(100.123456), + _bool: true, + _slice: []string{"hello", "world"}, + _array: [3]string{"one", "two", "three"}, + _map: map[string]string{ + "hello": "world", + }, + _enum: enumType_a, + _func: func() {}, + _struct: structType{"Bob"}, +} + +var testCasesIsSimpleType = []typeCheckTest{ + {"int", typeCheckInputData._int, true}, + {"int8", typeCheckInputData._int8, true}, + {"int16", typeCheckInputData._int16, true}, + {"int32", typeCheckInputData._int32, true}, + {"int64", typeCheckInputData._int64, true}, + {"uint", typeCheckInputData._uint, true}, + {"uint8", typeCheckInputData._uint8, true}, + {"uint16", typeCheckInputData._uint16, true}, + {"uint32", typeCheckInputData._uint32, true}, + {"uint64", typeCheckInputData._uint64, true}, + {"float32", typeCheckInputData._float32, true}, + {"float64", typeCheckInputData._float64, true}, + {"bool", typeCheckInputData._bool, true}, + {"slice", typeCheckInputData._slice, false}, + {"array", typeCheckInputData._array, false}, + {"map", typeCheckInputData._map, false}, + {"enum", typeCheckInputData._enum, true}, + {"func", typeCheckInputData._func, false}, + {"struct", typeCheckInputData._struct, false}, +} + +var testCasesIsCollectionType = []typeCheckTest{ + {"int", typeCheckInputData._int, false}, + {"int8", typeCheckInputData._int8, false}, + {"int16", typeCheckInputData._int16, false}, + {"int32", typeCheckInputData._int32, false}, + {"int64", typeCheckInputData._int64, false}, + {"uint", typeCheckInputData._uint, false}, + {"uint8", typeCheckInputData._uint8, false}, + {"uint16", typeCheckInputData._uint16, false}, + {"uint32", typeCheckInputData._uint32, false}, + {"uint64", typeCheckInputData._uint64, false}, + {"float32", typeCheckInputData._float32, false}, + {"float64", typeCheckInputData._float64, false}, + {"bool", typeCheckInputData._bool, false}, + {"slice", typeCheckInputData._slice, true}, + {"array", typeCheckInputData._array, true}, + {"map", typeCheckInputData._map, true}, + {"enum", typeCheckInputData._enum, false}, + {"func", typeCheckInputData._func, false}, + {"struct", typeCheckInputData._struct, false}, +} + +func TestIsSimpleType(t *testing.T) { + for _, test := range testCasesIsSimpleType { + output := IsSimpleType(test.input) + if output != test.expected { + t.Errorf("Test: '%s' FAILED: expected: %t, got %t", test.name, test.expected, output) + } + } +} + +func TestIsCollectionType(t *testing.T) { + for _, test := range testCasesIsCollectionType { + output := IsCollectionType(test.input) + if output != test.expected { + t.Errorf("Test: '%s' FAILED: expected: %t, got %t", test.name, test.expected, output) + } + } +}