package stardb import ( "strconv" "time" ) // convertToInt64 converts any value to int64 func convertToInt64(val interface{}) int64 { switch v := val.(type) { case nil: return 0 case int: return int64(v) case int32: return int64(v) case int64: return v case uint64: return int64(v) case float32: return int64(v) case float64: return int64(v) case string: result, _ := strconv.ParseInt(v, 10, 64) return result case bool: if v { return 1 } return 0 case time.Time: return v.Unix() case []byte: result, _ := strconv.ParseInt(string(v), 10, 64) return result default: return 0 } } // convertToUint64 converts any value to uint64 func convertToUint64(val interface{}) uint64 { switch v := val.(type) { case nil: return 0 case int: return uint64(v) case int32: return uint64(v) case int64: return uint64(v) case uint64: return v case float32: return uint64(v) case float64: return uint64(v) case string: result, _ := strconv.ParseUint(v, 10, 64) return result case bool: if v { return 1 } return 0 case time.Time: return uint64(v.Unix()) case []byte: result, _ := strconv.ParseUint(string(v), 10, 64) return result default: return 0 } } // convertToFloat64 converts any value to float64 func convertToFloat64(val interface{}) float64 { switch v := val.(type) { case nil: return 0 case int: return float64(v) case int32: return float64(v) case int64: return float64(v) case uint64: return float64(v) case float32: return float64(v) case float64: return v case string: result, _ := strconv.ParseFloat(v, 64) return result case bool: if v { return 1 } return 0 case time.Time: return float64(v.Unix()) case []byte: result, _ := strconv.ParseFloat(string(v), 64) return result default: return 0 } } // convertToBool converts any value to bool // Non-zero numbers are considered true func convertToBool(val interface{}) bool { switch v := val.(type) { case nil: return false case bool: return v case int: return v != 0 case int32: return v != 0 case int64: return v != 0 case uint64: return v != 0 case float32: return v != 0 case float64: return v != 0 case string: result, _ := strconv.ParseBool(v) return result case []byte: result, _ := strconv.ParseBool(string(v)) return result default: return false } } // convertToTime converts any value to time.Time func convertToTime(val interface{}, layout string) time.Time { switch v := val.(type) { case nil: return time.Time{} case time.Time: return v case int: return time.Unix(int64(v), 0) case int32: return time.Unix(int64(v), 0) case int64: return time.Unix(v, 0) case uint64: return time.Unix(int64(v), 0) case float32: sec := int64(v) nsec := int64((v - float32(sec)) * 1e9) return time.Unix(sec, nsec) case float64: sec := int64(v) nsec := int64((v - float64(sec)) * 1e9) return time.Unix(sec, nsec) case string: result, _ := time.Parse(layout, v) return result case []byte: result, _ := time.Parse(layout, string(v)) return result default: return time.Time{} } }