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

326 lines
6.0 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
}
4 years ago
// GetString 取得字串
4 years ago
func (data Data) GetString(key string) (string, error) {
value, err := data.Get(key)
if err != nil {
return "", err
}
switch value.(type) {
case string:
return value.(string), nil
}
return "", ErrFormat
}
4 years ago
// MustGetString 強制取得字串
func (data Data) MustGetString(key string) string {
value, _ := data.GetString(key)
return value
}
4 years ago
// GetSlice 取得切片
4 years ago
func (data Data) GetSlice(key string) (res []interface{}, err error) {
value, err := data.Get(key)
if err != nil {
return nil, err
}
switch value.(type) {
case []interface{}:
return value.([]interface{}), nil
default:
return nil, ErrFormat
}
}
4 years ago
// MustGetSlice 強制取得切片
func (data Data) MustGetSlice(key string) []interface{} {
values, _ := data.GetSlice(key)
return values
}
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 {
switch v.(type) {
case string:
res = append(res, v.(string))
default:
return nil, ErrFormat
}
}
return res, nil
}
4 years ago
// MustGetStringSlice 強制取得字串切片
func (data Data) MustGetStringSlice(key string) []string {
values, _ := data.GetStringSlice(key)
return values
}
4 years ago
// GetIntSlice 取得整數切片
4 years ago
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.(type) {
case float64:
res = append(res, int(v.(float64)))
default:
return nil, ErrFormat
}
}
return res, nil
}
4 years ago
// MustGetIntSlice 強制取得整數切片
func (data Data) MustGetIntSlice(key string) []int {
values, _ := data.GetIntSlice(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
}
switch value.(type) {
case int:
return int64(value.(int)), nil
case int64:
return value.(int64), nil
case float64:
return int64(value.(float64)), nil
}
return 0, ErrFormat
}
4 years ago
// MustGetInt64 強制取得64位元整數
func (data Data) MustGetInt64(key string) int64 {
value, _ := data.GetInt64(key)
return value
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
switch value.(type) {
case bool:
return value.(bool), nil
}
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.GetInt64(key)
if err != nil {
return 0, err
}
return Timestamp(value), nil
}
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
}