- Refactored the getBool function to handle boolean values as well as int64, int, and float64 types, returning true for non-zero values. - Improved type safety and flexibility in data handling within the utility functions.
47 lines
792 B
Go
47 lines
792 B
Go
package xun
|
|
|
|
// Helper functions for type conversion
|
|
func getString(data map[string]interface{}, key string) string {
|
|
if v, ok := data[key].(string); ok {
|
|
return v
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func getBool(data map[string]interface{}, key string) bool {
|
|
switch v := data[key].(type) {
|
|
case bool:
|
|
return v
|
|
case int64:
|
|
return v != 0
|
|
case int:
|
|
return v != 0
|
|
case float64:
|
|
return v != 0
|
|
}
|
|
return false
|
|
}
|
|
|
|
func getInt(data map[string]interface{}, key string) int {
|
|
switch v := data[key].(type) {
|
|
case int:
|
|
return v
|
|
case int64:
|
|
return int(v)
|
|
case float64:
|
|
return int(v)
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func getInt64(data map[string]interface{}, key string) int64 {
|
|
switch v := data[key].(type) {
|
|
case int64:
|
|
return v
|
|
case int:
|
|
return int64(v)
|
|
case float64:
|
|
return int64(v)
|
|
}
|
|
return 0
|
|
}
|