Skip to content

Commit

Permalink
cc.ToBoolean
Browse files Browse the repository at this point in the history
  • Loading branch information
qwenode committed Jun 5, 2024
1 parent 030490d commit e8d56eb
Showing 1 changed file with 44 additions and 18 deletions.
62 changes: 44 additions & 18 deletions cc/string.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,52 @@
package cc

import (
"fmt"
"strconv"
"fmt"
"strconv"
"strings"
)

// ToString convert to string
func ToString(str interface{}) string {
switch str.(type) {
case int:
return strconv.Itoa(str.(int))
case int32:
return fmt.Sprintf("%v", str.(int32))
case int64:
return fmt.Sprintf("%v", str.(int64))
case string:
return str.(string)
case float64:
return fmt.Sprintf("%v", str.(float64))
case float32:
return fmt.Sprintf("%v", str.(float32))
default:
return ""
}
switch str.(type) {
case int:
return strconv.Itoa(str.(int))
case int32:
return fmt.Sprintf("%v", str.(int32))
case int64:
return fmt.Sprintf("%v", str.(int64))
case string:
return str.(string)
case float64:
return fmt.Sprintf("%v", str.(float64))
case float32:
return fmt.Sprintf("%v", str.(float32))
default:
return ""
}
}

// ToBoolean convert to boolean
func ToBoolean(data interface{}) bool {
switch data.(type) {
case int:
case int32:
case int64:
case uint:
case uint32:
case uint64:
case uint8:
case int8:
toInt := ToInt(data)
return toInt > 0
case string:
s := strings.TrimSpace(strings.ToLower(data.(string)))
if s == "1" || s == "ok" || s == "true" || s == "y" || s == "yes" || s == "t" {
return true
}
return false
case bool:
return data.(bool)
}
return false
}

0 comments on commit e8d56eb

Please sign in to comment.