From a839508b273b89a21c8e9da4d17418cf843efc46 Mon Sep 17 00:00:00 2001 From: tursom Date: Sun, 27 Nov 2022 18:34:18 +0800 Subject: [PATCH] add lang.Slice --- lang/Lang.go | 31 ++++++++++++++++--------------- lang/Slice.go | 31 +++++++++++++++++++++++++++++++ lang/Slice_test.go | 20 ++++++++++++++++++++ lang/TypeCastException.go | 6 ++++++ 4 files changed, 73 insertions(+), 15 deletions(-) create mode 100644 lang/Slice.go create mode 100644 lang/Slice_test.go diff --git a/lang/Lang.go b/lang/Lang.go index b483167..b2e8dc9 100644 --- a/lang/Lang.go +++ b/lang/Lang.go @@ -7,9 +7,20 @@ package lang import ( + "reflect" "unsafe" ) +func TypeName[T any]() string { + t := reflect.TypeOf(Nil[T]()) + return t.Name() +} + +func TypeNameOf(v any) string { + t := reflect.TypeOf(v) + return t.Name() +} + func Nil[T any]() T { var n T return n @@ -33,28 +44,18 @@ func TryCast[T any](v any) (T, bool) { } func Cast[T any](v any) T { - defer func() { - r := recover() - if r != nil { - panic(NewTypeCastException("", r, nil)) - } - }() - if v == nil { return Nil[T]() } else { - return v.(T) + t, ok := v.(T) + if !ok { + panic(NewTypeCastException2[T](v, nil)) + } + return t } } func ForceCast[T any](v unsafe.Pointer) *T { - defer func() { - r := recover() - if r != nil { - panic(NewTypeCastException("", r, nil)) - } - }() - if v == nil { return nil } else { diff --git a/lang/Slice.go b/lang/Slice.go new file mode 100644 index 0000000..24534df --- /dev/null +++ b/lang/Slice.go @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2022 tursom. All rights reserved. + * Use of this source code is governed by a GPL-3 + * license that can be found in the LICENSE file. + */ + +package lang + +type Slice[T Object] []T + +func NewSlice[T Object](size, cap int) Slice[T] { + return make(Slice[T], size, cap) +} + +func (s Slice[T]) Contains(value T) bool { + for _, e := range s { + if e.Equals(value) { + return true + } + } + + return false +} + +func (s *Slice[T]) Append(value T) { + *s = append(*s, value) +} + +func (s Slice[T]) Size() int { + return len(s) +} diff --git a/lang/Slice_test.go b/lang/Slice_test.go new file mode 100644 index 0000000..ea3d08e --- /dev/null +++ b/lang/Slice_test.go @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2022 tursom. All rights reserved. + * Use of this source code is governed by a GPL-3 + * license that can be found in the LICENSE file. + */ + +package lang + +import ( + "fmt" + "testing" +) + +func TestSlice_Append(t *testing.T) { + s := NewSlice[Int](0, 0) + for i := 0; i < 16; i++ { + s.Append(Int(i)) + fmt.Println(s) + } +} diff --git a/lang/TypeCastException.go b/lang/TypeCastException.go index 060e8fd..341d173 100644 --- a/lang/TypeCastException.go +++ b/lang/TypeCastException.go @@ -6,6 +6,8 @@ package lang +import "fmt" + type TypeCastException struct { RuntimeException } @@ -21,3 +23,7 @@ func NewTypeCastException(message string, err any, config *ExceptionConfig) *Typ SetExceptionName("github.com.tursom.GoCollections.exceptions.TypeCastException")), } } + +func NewTypeCastException2[T any](v any, config *ExceptionConfig) *TypeCastException { + return NewTypeCastException(fmt.Sprintf("type %s cannot cast to %s", TypeNameOf(v), TypeName[T]()), nil, config) +}