Last active
January 12, 2019 12:13
-
-
Save ivanzoid/30827930b59d265ca8085335125af228 to your computer and use it in GitHub Desktop.
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
package main | |
import "strconv" | |
func interfaceToFloat(value interface{}) float64 { | |
if value == nil { | |
return 0.0 | |
} | |
switch typedValue := value.(type) { | |
case float64: | |
return typedValue | |
case float32: | |
return float64(typedValue) | |
case int: | |
return float64(typedValue) | |
case uint: | |
return float64(typedValue) | |
case string: | |
floatValue, _ := strconv.ParseFloat(typedValue, 10) | |
return floatValue | |
default: | |
return 0.0 | |
} | |
} | |
func interfaceToString(value interface{}) string { | |
switch typedValue := value.(type) { | |
case string: | |
return typedValue | |
default: | |
return "" | |
} | |
} | |
func interfaceToInt64(value interface{}) int64 { | |
if value == nil { | |
return 0 | |
} | |
switch typedValue := value.(type) { | |
case int64: | |
return typedValue | |
case int8: | |
return int64(typedValue) | |
case int16: | |
return int64(typedValue) | |
case int32: | |
return int64(typedValue) | |
case uint8: | |
return int64(typedValue) | |
case uint16: | |
return int64(typedValue) | |
case uint32: | |
return int64(typedValue) | |
case int: | |
return int64(typedValue) | |
case uint: | |
return int64(typedValue) | |
default: | |
return 0 | |
} | |
} | |
func interfaceToArray(value interface{}) []interface{} { | |
if value == nil { | |
return make([]interface{}, 0) | |
} | |
switch typedValue := value.(type) { | |
case []interface{}: | |
return typedValue | |
default: | |
return make([]interface{}, 0) | |
} | |
} | |
func interfaceToMap(value interface{}) map[interface{}]interface{} { | |
if value == nil { | |
return make(map[interface{}]interface{}, 0) | |
} | |
switch typedValue := value.(type) { | |
case map[interface{}]interface{}: | |
return typedValue | |
default: | |
return make(map[interface{}]interface{}) | |
} | |
} | |
func interfaceArrayToStringsArray(args []interface{}) []string { | |
result := make([]string, len(args), len(args)) | |
for _, arg := range args { | |
s := interfaceToString(arg) | |
if len(s) != 0 { | |
result = append(result, s) | |
} | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment