fix https://github.com/kataras/iris/issues/1020, redis database stores the int as float64, so make that type assertion on GetInt as well

Former-commit-id: d29abdfe3a39fa1e046acbc5d118421a153d9c04
This commit is contained in:
Gerasimos Maropoulos 2018-06-03 02:47:48 +03:00
parent b4856d542d
commit f83e125d7f
2 changed files with 38 additions and 0 deletions

View File

@ -66,6 +66,24 @@ func main() {
ctx.Writef("All ok session value of the '%s' is: %s", key, s.GetString(key))
})
app.Get("/set/int/{key}/{value}", func(ctx iris.Context) {
key := ctx.Params().Get("key")
value, _ := ctx.Params().GetInt("value")
s := sess.Start(ctx)
// set session values
s.Set(key, value)
valueSet := s.Get(key)
// test if setted here
ctx.Writef("All ok session value of the '%s' is: %v", key, valueSet)
})
app.Get("/get/{key}", func(ctx iris.Context) {
key := ctx.Params().Get("key")
value := sess.Start(ctx).Get(key)
ctx.Writef("The '%s' on the /set was: %v", key, value)
})
app.Get("/get", func(ctx iris.Context) {
// get a specific key, as string, if no found returns just an empty string
name := sess.Start(ctx).GetString("name")

View File

@ -176,6 +176,14 @@ func (s *Session) GetInt(key string) (int, error) {
return vint, nil
}
if vfloat64, ok := v.(float64); ok {
return int(vfloat64), nil
}
if vint64, ok := v.(int64); ok {
return int(vint64), nil
}
if vstring, sok := v.(string); sok {
return strconv.Atoi(vstring)
}
@ -221,6 +229,10 @@ func (s *Session) GetInt64(key string) (int64, error) {
return vint64, nil
}
if vfloat64, ok := v.(float64); ok {
return int64(vfloat64), nil
}
if vint, ok := v.(int); ok {
return int64(vint), nil
}
@ -259,6 +271,10 @@ func (s *Session) GetFloat32(key string) (float32, error) {
return float32(vint), nil
}
if vint64, ok := v.(int64); ok {
return float32(vint64), nil
}
if vstring, sok := v.(string); sok {
vfloat64, err := strconv.ParseFloat(vstring, 32)
if err != nil {
@ -297,6 +313,10 @@ func (s *Session) GetFloat64(key string) (float64, error) {
return float64(vint), nil
}
if vint64, ok := v.(int64); ok {
return float64(vint64), nil
}
if vstring, sok := v.(string); sok {
return strconv.ParseFloat(vstring, 32)
}