diff --git a/types/data.go b/types/data.go index b6bfe73..ee873f4 100644 --- a/types/data.go +++ b/types/data.go @@ -36,9 +36,9 @@ func (data Data) GetString(key string) (string, error) { return string(value), nil case string: return value, nil + default: + return "", ErrFormat } - - return "", ErrFormat } // MustGetString 強制取得字串 @@ -59,9 +59,9 @@ func (data Data) GetBytes(key string) ([]byte, error) { return value, nil case string: return []byte(value), nil + default: + return nil, ErrFormat } - - return nil, ErrFormat } // MustGetBytes 強制取得字串 @@ -127,6 +127,28 @@ func (data Data) GetIntSlice(key string) (res []int, err error) { for _, v := range values { switch v := v.(type) { + case int: + res = append(res, int(v)) + case uint: + res = append(res, int(v)) + case int8: + res = append(res, int(v)) + case uint8: + res = append(res, int(v)) + case int16: + res = append(res, int(v)) + case uint16: + res = append(res, int(v)) + case int32: + res = append(res, int(v)) + case uint32: + res = append(res, int(v)) + case int64: + res = append(res, int(v)) + case uint64: + res = append(res, int(v)) + case float32: + res = append(res, int(v)) case float64: res = append(res, int(v)) default: @@ -204,9 +226,9 @@ func (data Data) GetFloat64(key string) (float64, error) { return float64(value), nil case float64: return value, nil + default: + return 0, ErrFormat } - - return 0, ErrFormat } // MustGetFloat64 強制取得64位元浮點數 @@ -227,30 +249,25 @@ func (data Data) GetInt64(key string) (int64, error) { return int64(value), nil case uint: return int64(value), nil - case int8: return int64(value), nil case uint8: return int64(value), nil - case int16: return int64(value), nil case uint16: return int64(value), nil - case int32: return int64(value), nil case uint32: return int64(value), nil - case int64: return value, nil - case float64: return int64(value), nil + default: + return 0, ErrFormat } - - return 0, ErrFormat } // MustGetInt64 強制取得64位元整數 @@ -396,7 +413,11 @@ func (data Data) GetBool(key string) (bool, error) { switch value := value.(type) { case uint8: - return value == 1, nil + return value > 0, nil + case int64: + return value > 0, nil + case float64: + return value > 0, nil case bool: return value, nil } @@ -422,14 +443,13 @@ func (data Data) GetTimestamp(key string) (Timestamp, error) { return Timestamp(value), nil case uint: return Timestamp(value), nil - case int32: - return Timestamp(value), nil - case uint32: - return Timestamp(value), nil case int64: return Timestamp(value), nil case uint64: return Timestamp(value), nil + case float64: + return Timestamp(value), nil + case Timestamp: return value, nil }