fix value type

tags/v1.3.5 v1.3.5
Stanly 3 years ago
parent 29b17ab180
commit 2edb855dd1

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

Loading…
Cancel
Save