Skip to content

Commit

Permalink
修复:无法将自定义枚举转数字的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Nov 24, 2023
1 parent 10e2411 commit 3e911c9
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 68 deletions.
34 changes: 0 additions & 34 deletions parse/anyToNumber.go

This file was deleted.

33 changes: 0 additions & 33 deletions parse/numberToNumber.go

This file was deleted.

73 changes: 73 additions & 0 deletions parse/toNumber.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package parse

import "reflect"

func anyToNumber(source any, sourceKind reflect.Kind, defVal any, returnKind reflect.Kind) any {
switch s := source.(type) {
case int8:
return numberToNumber(s, defVal, returnKind)
case int16:
return numberToNumber(s, defVal, returnKind)
case int32:
return numberToNumber(s, defVal, returnKind)
case int64:
return numberToNumber(s, defVal, returnKind)
case int:
return numberToNumber(s, defVal, returnKind)
case uint:
return numberToNumber(s, defVal, returnKind)
case uint8:
return numberToNumber(s, defVal, returnKind)
case uint16:
return numberToNumber(s, defVal, returnKind)
case uint32:
return numberToNumber(s, defVal, returnKind)
case uint64:
return numberToNumber(s, defVal, returnKind)
case float32:
return numberToNumber(s, defVal, returnKind)
case float64:
return numberToNumber(s, defVal, returnKind)
default: // 自定义的数字类型
switch sourceKind {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return numberToNumber(reflect.ValueOf(source).Int(), defVal, returnKind)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return numberToNumber(reflect.ValueOf(source).Uint(), defVal, returnKind)
case reflect.Float32, reflect.Float64:
return numberToNumber(reflect.ValueOf(source).Float(), defVal, returnKind)
}
}
return source

}

func numberToNumber[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64](source T, defVal any, returnKind reflect.Kind) any {
switch returnKind {
case reflect.Int8:
defVal = int8(source)
case reflect.Int16:
defVal = int16(source)
case reflect.Int32:
defVal = int32(source)
case reflect.Int64:
defVal = int64(source)
case reflect.Int:
defVal = int(source)
case reflect.Uint:
defVal = uint(source)
case reflect.Uint8:
defVal = uint8(source)
case reflect.Uint16:
defVal = uint16(source)
case reflect.Uint32:
defVal = uint32(source)
case reflect.Uint64:
defVal = uint64(source)
case reflect.Float32:
defVal = float32(source)
case reflect.Float64:
defVal = float64(source)
}
return defVal
}
File renamed without changes.
8 changes: 7 additions & 1 deletion test/parse_convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import (
)

func TestConvert(t *testing.T) {
t.Run("enum转uint8", func(t *testing.T) {
type Enum uint8
const (
All Enum = 66 // 全部
)
assert.Equal(t, 66, parse.ToInt(All))
})

assert.Equal(t, 123, parse.ConvertValue("123", reflect.TypeOf(1)).Interface().(int))

Expand Down Expand Up @@ -166,7 +173,6 @@ func TestConvert(t *testing.T) {
assert.Equal(t, 9, dt.Month())
assert.Equal(t, 15, dt.Day())
})

//t.Run("string转List", func(t *testing.T) {
// lst := collections.NewList(1, 2, 3)
// lst2 := parse.Convert("1,2,3", collections.NewList[int]())
Expand Down

0 comments on commit 3e911c9

Please sign in to comment.