diff --git a/_examples/sessions/database/redis/main.go b/_examples/sessions/database/redis/main.go index 0b9d2e0e..5feeb90d 100644 --- a/_examples/sessions/database/redis/main.go +++ b/_examples/sessions/database/redis/main.go @@ -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") diff --git a/sessions/session.go b/sessions/session.go index fc7ded6d..80f9383d 100644 --- a/sessions/session.go +++ b/sessions/session.go @@ -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) }