fix value type

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

@ -36,10 +36,10 @@ func (data Data) GetString(key string) (string, error) {
return string(value), nil
case string:
return value, nil
}
default:
return "", ErrFormat
}
}
// MustGetString 強制取得字串
func (data Data) MustGetString(key string) string {
@ -59,10 +59,10 @@ func (data Data) GetBytes(key string) ([]byte, error) {
return value, nil
case string:
return []byte(value), nil
}
default:
return nil, ErrFormat
}
}
// MustGetBytes 強制取得字串
func (data Data) MustGetBytes(key string) []byte {
@ -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,10 +226,10 @@ func (data Data) GetFloat64(key string) (float64, error) {
return float64(value), nil
case float64:
return value, nil
}
default:
return 0, ErrFormat
}
}
// MustGetFloat64 強制取得64位元浮點數
func (data Data) MustGetFloat64(key string) float64 {
@ -227,31 +249,26 @@ 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
}
}
// MustGetInt64 強制取得64位元整數
func (data Data) MustGetInt64(key string) int64 {
@ -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
}

Loading…
Cancel
Save