Skip to content

Commit

Permalink
fea: 辅助代码
Browse files Browse the repository at this point in the history
  • Loading branch information
cyz-home committed Mar 8, 2023
1 parent f3fda54 commit ab64562
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
62 changes: 62 additions & 0 deletions bootstrap/services/database/num.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package database

import (
"fmt"
"math"
"strconv"
)

// Int32 强制转换
func Int32(v interface{}) int32 {
switch val := v.(type) {
case int32:
return val
case int:
return int32(val)
case int64:
return int32(val)
case float32:
return int32(val)
case float64:
return int32(val)
case string:
num, err := strconv.Atoi(val)
if err != nil {
// 无法解析为数字,返回 0
return 0
}
if num > math.MaxInt32 || num < math.MinInt32 {
// 超出 int32 的范围,返回 0
return 0
}
return int32(num)
default:
s := fmt.Sprintf("%d", v)
return Int32(s)
}
}

// UInt32 强制转换
func UInt32(v interface{}) uint32 {
switch val := v.(type) {
case uint32:
return val
case uint:
return uint32(val)
case uint64:
return uint32(val)
case float32:
return uint32(val)
case float64:
return uint32(val)
case string:
num, err := strconv.Atoi(val)
if err != nil {
return 0
}
return uint32(num)
default:
s := fmt.Sprintf("%d", v)
return UInt32(s)
}
}
2 changes: 2 additions & 0 deletions bootstrap/services/database/str.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package database

// StrPointer 字符串转指针
func StrPointer(str interface{}) *string {
switch str.(type) {
case string:
Expand All @@ -15,6 +16,7 @@ func StrPointer(str interface{}) *string {
return nil
}

// IntPointer 转指针
func IntPointer(i interface{}) *int {
switch i.(type) {
case int:
Expand Down

0 comments on commit ab64562

Please sign in to comment.