-
Notifications
You must be signed in to change notification settings - Fork 3
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
1 changed file
with
44 additions
and
18 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -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 | ||
} |