123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- 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
- }
|