Skip to content

Commit

Permalink
解决 methodArgs: { type: "[]int", value: ["a", "b"] } 转换类型失败,因为 fmt.Spr…
Browse files Browse the repository at this point in the history
…int(["a", "b"]) 结果是 "[a, b]"
  • Loading branch information
TommyLemon committed Apr 8, 2024
1 parent cf67c16 commit 24bd612
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
14 changes: 10 additions & 4 deletions unitauto/method_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
20 changes: 20 additions & 0 deletions unitauto/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down
40 changes: 40 additions & 0 deletions unitauto/test/test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 24bd612

Please sign in to comment.