You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lib/types/data.go

879 lines
17 KiB
Go

4 years ago
package types
import (
4 years ago
"encoding/json"
4 years ago
"errors"
)
4 years ago
// 錯誤訊息
4 years ago
var (
4 years ago
ErrExistKey = errors.New("not exist")
ErrFormat = errors.New("error format")
4 years ago
)
4 years ago
// Data 資料
4 years ago
type Data map[string]interface{}
4 years ago
// Get 取得資料
4 years ago
func (data Data) Get(key string) (interface{}, error) {
value, exist := data[key]
if !exist {
return "", ErrExistKey
}
return value, nil
}
// GetSlice 取得切片
func (data Data) GetSlice(key string) (res Slice, err error) {
3 years ago
value, err := data.Get(key)
if err != nil {
return nil, err
}
switch value := value.(type) {
case []interface{}:
return value, nil
case Slice:
3 years ago
return value, nil
3 years ago
default:
return nil, ErrFormat
3 years ago
}
}
// MustGetSlice 強制取得切片
func (data Data) MustGetSlice(key string) Slice {
values, _ := data.GetSlice(key)
return values
3 years ago
}
// GetString 取得字串
func (data Data) GetString(key string) (string, error) {
4 years ago
value, err := data.Get(key)
if err != nil {
return "", err
4 years ago
}
3 years ago
switch value := value.(type) {
case []byte:
return string(value), nil
case string:
3 years ago
return value, nil
4 years ago
default:
return "", ErrFormat
4 years ago
}
}
// MustGetString 強制取得字串
func (data Data) MustGetString(key string) string {
value, _ := data.GetString(key)
return value
}
4 years ago
// GetStringSlice 取得字串切片
4 years ago
func (data Data) GetStringSlice(key string) (res []string, err error) {
values, err := data.GetSlice(key)
if err != nil {
return nil, err
}
for _, v := range values {
3 years ago
switch v := v.(type) {
4 years ago
case string:
3 years ago
res = append(res, v)
4 years ago
default:
return nil, ErrFormat
}
}
return res, nil
}
4 years ago
// MustGetStringSlice 強制取得字串切片
func (data Data) MustGetStringSlice(key string) []string {
values, _ := data.GetStringSlice(key)
return values
}
// GetBytes 取得字串
func (data Data) GetBytes(key string) ([]byte, error) {
value, err := data.Get(key)
4 years ago
if err != nil {
return nil, err
}
switch value := value.(type) {
case []byte:
return value, nil
case string:
return []byte(value), nil
default:
return nil, ErrFormat
4 years ago
}
}
// MustGetBytes 強制取得字串
func (data Data) MustGetBytes(key string) []byte {
value, _ := data.GetBytes(key)
return value
}
4 years ago
// GetData 取得資料
func (data Data) GetData(key string) (Data, error) {
value, err := data.Get(key)
if err != nil {
return nil, err
}
3 years ago
switch value := value.(type) {
4 years ago
case map[string]interface{}:
3 years ago
return Data(value), nil
4 years ago
case Data:
3 years ago
return value, nil
4 years ago
default:
return nil, ErrFormat
}
}
// MustGetData 強制取得資料
func (data Data) MustGetData(key string) Data {
value, _ := data.GetData(key)
return value
}
// GetDataSlice 取得資料切片
func (data Data) GetDataSlice(key string) ([]Data, error) {
values, err := data.GetSlice(key)
if err != nil {
return nil, err
}
var list []Data
for _, v := range values {
3 years ago
switch v := v.(type) {
case Data:
list = append(list, v)
default:
return nil, ErrFormat
}
}
return list, nil
}
// MustGetDataSlice 強制取得資料切片
func (data Data) MustGetDataSlice(key string) []Data {
values, _ := data.GetDataSlice(key)
return values
}
4 years ago
// GetFloat64 取得64位元浮點數
func (data Data) GetFloat64(key string) (float64, error) {
value, err := data.Get(key)
if err != nil {
return 0, err
}
3 years ago
switch value := value.(type) {
case float32:
return float64(value), nil
4 years ago
case float64:
3 years ago
return value, nil
3 years ago
default:
return 0, ErrFormat
4 years ago
}
}
// MustGetFloat64 強制取得64位元浮點數
func (data Data) MustGetFloat64(key string) float64 {
value, _ := data.GetFloat64(key)
return value
}
// GetFloat64Slice 取得64位元浮點數切片
func (data Data) GetFloat64Slice(key string) (res []float64, err error) {
values, err := data.GetSlice(key)
if err != nil {
return nil, err
}
for _, v := range values {
switch v := v.(type) {
case int:
res = append(res, float64(v))
case uint:
res = append(res, float64(v))
case int8:
res = append(res, float64(v))
case uint8:
res = append(res, float64(v))
case int16:
res = append(res, float64(v))
case uint16:
res = append(res, float64(v))
case int32:
res = append(res, float64(v))
case uint32:
res = append(res, float64(v))
case int64:
res = append(res, float64(v))
case uint64:
res = append(res, float64(v))
case float32:
res = append(res, float64(v))
case float64:
res = append(res, v)
default:
return nil, ErrFormat
}
}
return res, nil
}
// MustGetFloat64Slice 強制取得64位元浮點數切片
func (data Data) MustGetFloat64Slice(key string) []float64 {
values, _ := data.GetFloat64Slice(key)
return values
}
4 years ago
// GetInt64 取得64位元整數
4 years ago
func (data Data) GetInt64(key string) (int64, error) {
value, err := data.Get(key)
if err != nil {
return 0, err
}
3 years ago
switch value := value.(type) {
4 years ago
case int:
3 years ago
return int64(value), nil
case uint:
3 years ago
return int64(value), nil
case int8:
3 years ago
return int64(value), nil
case uint8:
3 years ago
return int64(value), nil
case int16:
3 years ago
return int64(value), nil
case uint16:
3 years ago
return int64(value), nil
case int32:
3 years ago
return int64(value), nil
case uint32:
3 years ago
return int64(value), nil
4 years ago
case int64:
3 years ago
return value, nil
4 years ago
case float64:
3 years ago
return int64(value), nil
3 years ago
default:
return 0, ErrFormat
4 years ago
}
}
4 years ago
// MustGetInt64 強制取得64位元整數
func (data Data) MustGetInt64(key string) int64 {
value, _ := data.GetInt64(key)
return value
}
// GetInt64Slice 取得64位元整數切片
func (data Data) GetInt64Slice(key string) (res []int64, err error) {
values, err := data.GetSlice(key)
if err != nil {
return nil, err
}
for _, v := range values {
switch v := v.(type) {
case int:
res = append(res, int64(v))
case uint:
res = append(res, int64(v))
case int8:
res = append(res, int64(v))
case uint8:
res = append(res, int64(v))
case int16:
res = append(res, int64(v))
case uint16:
res = append(res, int64(v))
case int32:
res = append(res, int64(v))
case uint32:
res = append(res, int64(v))
case int64:
res = append(res, v)
case uint64:
res = append(res, int64(v))
case float32:
res = append(res, int64(v))
case float64:
res = append(res, int64(v))
default:
return nil, ErrFormat
}
}
return res, nil
}
// MustGetInt64Slice 強制取得64位元整數切片
func (data Data) MustGetInt64Slice(key string) []int64 {
values, _ := data.GetInt64Slice(key)
return values
}
4 years ago
// GetUint64 取得64位元正整數
4 years ago
func (data Data) GetUint64(key string) (uint64, error) {
value, err := data.GetInt64(key)
if err != nil {
return 0, err
}
if value < 0 {
return 0, ErrFormat
}
return uint64(value), nil
}
4 years ago
// MustGetUint64 強制取得64位元正整數
func (data Data) MustGetUint64(key string) uint64 {
value, _ := data.GetUint64(key)
return value
}
// GetUint64Slice 取得64位元正整數切片
func (data Data) GetUint64Slice(key string) (res []uint64, err error) {
values, err := data.GetSlice(key)
if err != nil {
return nil, err
}
for _, v := range values {
switch v := v.(type) {
case int:
res = append(res, uint64(v))
case uint:
res = append(res, uint64(v))
case int8:
res = append(res, uint64(v))
case uint8:
res = append(res, uint64(v))
case int16:
res = append(res, uint64(v))
case uint16:
res = append(res, uint64(v))
case int32:
res = append(res, uint64(v))
case uint32:
res = append(res, uint64(v))
case int64:
res = append(res, uint64(v))
case uint64:
res = append(res, v)
case float32:
res = append(res, uint64(v))
case float64:
res = append(res, uint64(v))
default:
return nil, ErrFormat
}
}
return res, nil
}
// MustGetUint64Slice 強制取得64位元正整數切片
func (data Data) MustGetUint64Slice(key string) []uint64 {
values, _ := data.GetUint64Slice(key)
return values
}
4 years ago
// GetInt 取得整數
4 years ago
func (data Data) GetInt(key string) (int, error) {
value, err := data.GetInt64(key)
if err != nil {
return 0, err
}
return int(value), nil
}
4 years ago
// MustGetInt 強制取得整數
func (data Data) MustGetInt(key string) int {
value, _ := data.GetInt(key)
return value
}
// GetIntSlice 取得整數切片
func (data Data) GetIntSlice(key string) (res []int, err error) {
values, err := data.GetSlice(key)
if err != nil {
return nil, err
}
for _, v := range values {
switch v := v.(type) {
case int:
res = append(res, 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:
return nil, ErrFormat
}
}
return res, nil
}
// MustGetIntSlice 強制取得整數切片
func (data Data) MustGetIntSlice(key string) []int {
values, _ := data.GetIntSlice(key)
return values
}
4 years ago
// GetUint 取得正整數
4 years ago
func (data Data) GetUint(key string) (uint, error) {
value, err := data.GetInt64(key)
if err != nil {
return 0, err
}
if value < 0 {
return 0, ErrFormat
}
return uint(value), nil
}
4 years ago
// MustGetUint 強制取得正整數
func (data Data) MustGetUint(key string) uint {
value, _ := data.GetUint(key)
return value
}
// GetUintSlice 取得正整數切片
func (data Data) GetUintSlice(key string) (res []uint, err error) {
values, err := data.GetSlice(key)
if err != nil {
return nil, err
}
for _, v := range values {
switch v := v.(type) {
case int:
res = append(res, uint(v))
case uint:
res = append(res, v)
case int8:
res = append(res, uint(v))
case uint8:
res = append(res, uint(v))
case int16:
res = append(res, uint(v))
case uint16:
res = append(res, uint(v))
case int32:
res = append(res, uint(v))
case uint32:
res = append(res, uint(v))
case int64:
res = append(res, uint(v))
case uint64:
res = append(res, uint(v))
case float32:
res = append(res, uint(v))
case float64:
res = append(res, uint(v))
default:
return nil, ErrFormat
}
}
return res, nil
}
// MustGetUintSlice 強制取得正整數切片
func (data Data) MustGetUintSlice(key string) []uint {
values, _ := data.GetUintSlice(key)
return values
}
4 years ago
// GetInt16 取得16位元整數
4 years ago
func (data Data) GetInt16(key string) (int16, error) {
value, err := data.GetInt64(key)
if err != nil {
return 0, err
}
return int16(value), nil
}
4 years ago
// MustGetInt16 強制取得16位元整數
func (data Data) MustGetInt16(key string) uint16 {
value, _ := data.GetUint16(key)
return value
}
// GetInt16Slice 取得16位元整數切片
func (data Data) GetInt16Slice(key string) (res []int16, err error) {
values, err := data.GetSlice(key)
if err != nil {
return nil, err
}
for _, v := range values {
switch v := v.(type) {
case int:
res = append(res, int16(v))
case uint:
res = append(res, int16(v))
case int8:
res = append(res, int16(v))
case uint8:
res = append(res, int16(v))
case int16:
res = append(res, v)
case uint16:
res = append(res, int16(v))
case int32:
res = append(res, int16(v))
case uint32:
res = append(res, int16(v))
case int64:
res = append(res, int16(v))
case uint64:
res = append(res, int16(v))
case float32:
res = append(res, int16(v))
case float64:
res = append(res, int16(v))
default:
return nil, ErrFormat
}
}
return res, nil
}
// MustGetInt16Slice 強制取得16位元整數切片
func (data Data) MustGetInt16Slice(key string) []int16 {
values, _ := data.GetInt16Slice(key)
return values
}
4 years ago
// GetUint16 取得16位元正整數
4 years ago
func (data Data) GetUint16(key string) (uint16, error) {
value, err := data.GetInt64(key)
if err != nil {
return 0, err
}
if value < 0 {
return 0, ErrFormat
}
return uint16(value), nil
}
4 years ago
// MustGetUint16 強制取得16位元正整數
func (data Data) MustGetUint16(key string) uint16 {
value, _ := data.GetUint16(key)
return value
}
// GetUint16Slice 取得16位元正整數切片
func (data Data) GetUint16Slice(key string) (res []uint16, err error) {
values, err := data.GetSlice(key)
if err != nil {
return nil, err
}
for _, v := range values {
switch v := v.(type) {
case int:
res = append(res, uint16(v))
case uint:
res = append(res, uint16(v))
case int8:
res = append(res, uint16(v))
case uint8:
res = append(res, uint16(v))
case int16:
res = append(res, uint16(v))
case uint16:
res = append(res, v)
case int32:
res = append(res, uint16(v))
case uint32:
res = append(res, uint16(v))
case int64:
res = append(res, uint16(v))
case uint64:
res = append(res, uint16(v))
case float32:
res = append(res, uint16(v))
case float64:
res = append(res, uint16(v))
default:
return nil, ErrFormat
}
}
return res, nil
}
// MustGetUint16Slice 強制取得16位元正整數切片
func (data Data) MustGetUint16Slice(key string) []uint16 {
values, _ := data.GetUint16Slice(key)
return values
}
4 years ago
// GetInt8 取得8位元整數
4 years ago
func (data Data) GetInt8(key string) (int8, error) {
value, err := data.GetInt64(key)
if err != nil {
return 0, err
}
return int8(value), nil
}
4 years ago
// MustGetInt8 強制取得8位元整數
func (data Data) MustGetInt8(key string) int8 {
value, _ := data.GetInt8(key)
return value
}
// GetInt8Slice 取得8位元整數切片
func (data Data) GetInt8Slice(key string) (res []int8, err error) {
values, err := data.GetSlice(key)
if err != nil {
return nil, err
}
for _, v := range values {
switch v := v.(type) {
case int:
res = append(res, int8(v))
case uint:
res = append(res, int8(v))
case int8:
res = append(res, v)
case uint8:
res = append(res, int8(v))
case int16:
res = append(res, int8(v))
case uint16:
res = append(res, int8(v))
case int32:
res = append(res, int8(v))
case uint32:
res = append(res, int8(v))
case int64:
res = append(res, int8(v))
case uint64:
res = append(res, int8(v))
case float32:
res = append(res, int8(v))
case float64:
res = append(res, int8(v))
default:
return nil, ErrFormat
}
}
return res, nil
}
// MustGetInt8Slice 強制取得8位元整數切片
func (data Data) MustGetInt8Slice(key string) []int8 {
values, _ := data.GetInt8Slice(key)
return values
}
4 years ago
// GetUint8 取得8位元正整數
4 years ago
func (data Data) GetUint8(key string) (uint8, error) {
value, err := data.GetInt64(key)
if err != nil {
return 0, err
}
if value < 0 {
return 0, ErrFormat
}
return uint8(value), nil
}
4 years ago
// MustGetUint8 強制取得8位元正整數
func (data Data) MustGetUint8(key string) uint8 {
value, _ := data.GetUint8(key)
return value
}
// GetUint8Slice 取得8位元正整數切片
func (data Data) GetUint8Slice(key string) (res []uint8, err error) {
values, err := data.GetSlice(key)
if err != nil {
return nil, err
}
for _, v := range values {
switch v := v.(type) {
case int:
res = append(res, uint8(v))
case uint:
res = append(res, uint8(v))
case int8:
res = append(res, uint8(v))
case uint8:
res = append(res, v)
case int16:
res = append(res, uint8(v))
case uint16:
res = append(res, uint8(v))
case int32:
res = append(res, uint8(v))
case uint32:
res = append(res, uint8(v))
case int64:
res = append(res, uint8(v))
case uint64:
res = append(res, uint8(v))
case float32:
res = append(res, uint8(v))
case float64:
res = append(res, uint8(v))
default:
return nil, ErrFormat
}
}
return res, nil
}
// MustGetUint8Slice 強制取得8位元正整數切片
func (data Data) MustGetUint8Slice(key string) []uint8 {
values, _ := data.GetUint8Slice(key)
return values
}
4 years ago
// GetBool 取得布林值
4 years ago
func (data Data) GetBool(key string) (bool, error) {
value, err := data.Get(key)
if err != nil {
return false, err
}
3 years ago
switch value := value.(type) {
case uint8:
3 years ago
return value > 0, nil
case int64:
return value > 0, nil
case float64:
return value > 0, nil
4 years ago
case bool:
3 years ago
return value, nil
4 years ago
}
return false, ErrFormat
}
4 years ago
// MustGetBool 強制取得布林值
func (data Data) MustGetBool(key string) bool {
value, _ := data.GetBool(key)
return value
}
4 years ago
// GetTimestamp 取得時間戳記
4 years ago
func (data Data) GetTimestamp(key string) (Timestamp, error) {
value, err := data.Get(key)
4 years ago
if err != nil {
return 0, err
}
3 years ago
switch value := value.(type) {
case int:
return Timestamp(value), nil
case uint:
return Timestamp(value), nil
case int64:
return Timestamp(value), nil
case uint64:
return Timestamp(value), nil
3 years ago
case float64:
return Timestamp(value), nil
case Timestamp:
3 years ago
return value, nil
}
return 0, ErrFormat
4 years ago
}
4 years ago
// MustGetTimestamp 強制取得時間戳記
func (data Data) MustGetTimestamp(key string) Timestamp {
value, _ := data.GetTimestamp(key)
return value
}
4 years ago
4 years ago
// JSON 取得json
4 years ago
func (data Data) JSON() ([]byte, error) {
bs, err := json.Marshal(&data)
if err != nil {
return nil, err
}
return bs, nil
}
4 years ago
// MustJSON 強制取得json
4 years ago
func (data Data) MustJSON() []byte {
bs, _ := data.JSON()
return bs
}
3 years ago
// GetPage 取得頁數
3 years ago
func (data Data) GetPage(key string) (*Page, error) {
value, err := data.Get(key)
if err != nil {
return nil, err
}
3 years ago
switch value := value.(type) {
3 years ago
case Page:
3 years ago
return &value, nil
3 years ago
}
return nil, ErrFormat
}
// MustGetPage 強制取得頁數
3 years ago
func (data Data) MustGetPage(key string) *Page {
value, _ := data.GetPage(key)
return value
}