diff --git a/unitauto/method_util.go b/unitauto/method_util.go index f24494c..b5b39f8 100644 --- a/unitauto/method_util.go +++ b/unitauto/method_util.go @@ -2376,9 +2376,12 @@ func cast(obj any, typ reflect.Type) (any, error) { } if IsMapType(typ.String()) { - var s = fmt.Sprint(obj) + var b, err = json.Marshal(obj) + if err != nil { + var s = fmt.Sprintf("%q\n", obj) + b = []byte(s) + } - var b = []byte(s) var m = map[string]any{} if err := json.Unmarshal(b, m); err != nil { return nil, err @@ -2401,9 +2404,12 @@ func cast(obj any, typ reflect.Type) (any, error) { var al = 0 if IsArrType(typ.String()) { - var s = fmt.Sprint(obj) + var b, err = json.Marshal(obj) + if err != nil { + var s = fmt.Sprintf("%q\n", obj) + b = []byte(s) + } - var b = []byte(s) var a []any if err := json.Unmarshal(b, &a); err != nil { return nil, err diff --git a/unitauto/server.go b/unitauto/server.go index 1511d74..1cc96a3 100644 --- a/unitauto/server.go +++ b/unitauto/server.go @@ -34,6 +34,16 @@ func Init() { CLASS_MAP["unitauto.test.Minus"] = test.Minus CLASS_MAP["unitauto.test.Multiply"] = test.Multiply CLASS_MAP["unitauto.test.Divide"] = test.Divide + CLASS_MAP["unitauto.test.Len"] = test.Len + CLASS_MAP["unitauto.test.Index"] = test.Index + CLASS_MAP["unitauto.test.GetByIndex"] = test.GetByIndex[any] + CLASS_MAP["unitauto.test.GetByIndex[int]"] = test.GetByIndex[int] + CLASS_MAP["unitauto.test.GetByIndex[float64]"] = test.GetByIndex[float64] + CLASS_MAP["unitauto.test.GetByIndex[string]"] = test.GetByIndex[string] + CLASS_MAP["unitauto.test.SetByIndex"] = test.SetByIndex[any] + CLASS_MAP["unitauto.test.SetByIndex[int]"] = test.SetByIndex[int] + CLASS_MAP["unitauto.test.SetByIndex[float64]"] = test.SetByIndex[float64] + CLASS_MAP["unitauto.test.SetByIndex[string]"] = test.SetByIndex[string] CLASS_MAP["unitauto.test.ComputeAsync"] = test.ComputeAsync CLASS_MAP["unitauto.test.New"] = test.New CLASS_MAP["unitauto.test.Compare"] = test.Compare @@ -53,6 +63,16 @@ func Init() { CLASS_MAP["test.Minus"] = test.Minus CLASS_MAP["test.Multiply"] = test.Multiply CLASS_MAP["test.Divide"] = test.Divide + CLASS_MAP["test.Len"] = test.Len + CLASS_MAP["test.Index"] = test.Index + CLASS_MAP["test.GetByIndex"] = test.GetByIndex[any] + CLASS_MAP["test.GetByIndex[int]"] = test.GetByIndex[int] + CLASS_MAP["test.GetByIndex[float64]"] = test.GetByIndex[float64] + CLASS_MAP["test.GetByIndex[string]"] = test.GetByIndex[string] + CLASS_MAP["test.SetByIndex"] = test.SetByIndex[any] + CLASS_MAP["test.SetByIndex[int]"] = test.SetByIndex[int] + CLASS_MAP["test.SetByIndex[float64]"] = test.SetByIndex[float64] + CLASS_MAP["test.SetByIndex[string]"] = test.SetByIndex[string] CLASS_MAP["test.TestGeneric"] = test.TestGeneric[float64, float64] CLASS_MAP["test.TestGeneric[int,int]"] = test.TestGeneric[int, int] CLASS_MAP["test.TestGeneric[int,float64]"] = test.TestGeneric[int, float64] diff --git a/unitauto/test/test_util.go b/unitauto/test/test_util.go index 56131b0..706c282 100644 --- a/unitauto/test/test_util.go +++ b/unitauto/test/test_util.go @@ -26,6 +26,46 @@ func Divide(a int, b int) float64 { return float64(a) / float64(b) } +func Len(arr []int) int { + if arr == nil { + return 0 + } + return len(arr) +} + +func Index(arr []any, item any) int { + l := len(arr) + if l <= 0 { + return -1 + } + + for i := 0; i < l; i++ { + if arr[i] == item { + return i + } + } + + return -1 +} + +func GetByIndex[T any](arr []T, index int) *T { + l := len(arr) + if l <= 0 || l <= index { + return nil + } + + return &arr[index] +} + +func SetByIndex[T any](arr []T, index int, item T) { + l := len(arr) + if l <= 0 || l <= index { + return + } + + arr[index] = item +} + func TestGeneric[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64, R int | float64](a T, b R) float64 { return float64(a) + float64(b) }