package client import ( "fmt" "time" ) func ToStrings(data interface{}) []string { var i []string if v, ok := data.([]interface{}); ok { for _, in := range v { i = append(i, fmt.Sprint(in)) } } return i } func ToString(data interface{}) string { if data == nil { return "" } return fmt.Sprint(data) } func ToFloat64(data interface{}) float64 { if v, ok := data.(float64); ok { return v } if v, ok := data.(int64); ok { return float64(v) } return 0 } func ToFloat32(data interface{}) float32 { if v, ok := data.(float32); ok { return v } if v, ok := data.(float64); ok { return float32(v) } if v, ok := data.(int64); ok { return float32(v) } return 0 } func ToInt(data interface{}) int { if v, ok := data.(int); ok { return v } if v, ok := data.(int64); ok { return int(v) } if v, ok := data.(float64); ok { return int(v) } if v, ok := data.(float32); ok { return int(v) } return 0 } func ToBool(data interface{}) bool { if v, ok := data.(bool); ok { return v } return ToInt(data) == 1 } const ( GRPC_DATETIME_FROM_SQL_FORMAT = "2006-01-02T15:04:05.999999999-07:00" GRPC_DATETIME_FROM_SQL_FORMAT2 = "2006-01-02T15:04:05.999999999Z" DATETIME_FROM_ISO_FORMAT = "2006-01-02T15:04:05.999-07:00" ) func ToTime(data interface{}) (*time.Time, error) { if v, ok := data.(time.Time); ok { return &v, nil } str := ToString(data) dateTime, err := time.Parse(GRPC_DATETIME_FROM_SQL_FORMAT, str) if err != nil { dateTime, err = time.Parse(GRPC_DATETIME_FROM_SQL_FORMAT2, str) if err != nil { return nil, err } } return &dateTime, nil } func ToTimeFromISO(data interface{}) (*time.Time, error) { str := ToString(data) dateTime, err := time.Parse(DATETIME_FROM_ISO_FORMAT, str) if err != nil { return nil, err } return &dateTime, nil }