-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
80 additions
and
68 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters