From 108bfb44704ce68ca5c6e05972445965edd247e0 Mon Sep 17 00:00:00 2001 From: Nick Gerakines Date: Thu, 3 Jul 2014 15:17:24 -0400 Subject: [PATCH 1/2] Setting Get metadata method to return both an interface as well as an error to remove panic. --- examples/example_basic.go | 4 ++-- gin.go | 17 ++++++----------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/examples/example_basic.go b/examples/example_basic.go index ea343437..4586907c 100644 --- a/examples/example_basic.go +++ b/examples/example_basic.go @@ -38,14 +38,14 @@ func main() { })) authorized.POST("admin", func(c *gin.Context) { - user := c.Get("user").(string) + user, _ := c.Get("user") // Parse JSON var json struct { Value string `json:"value" binding:"required"` } if c.EnsureBody(&json) { - DB[user] = json.Value + DB[user.(string)] = json.Value c.JSON(200, gin.H{"status": "ok"}) } }) diff --git a/gin.go b/gin.go index 197b98ca..e29fe9dc 100644 --- a/gin.go +++ b/gin.go @@ -8,7 +8,6 @@ import ( "fmt" "github.com/julienschmidt/httprouter" "html/template" - "log" "math" "net/http" "path" @@ -283,18 +282,14 @@ func (c *Context) Set(key string, item interface{}) { // Returns the value for the given key. // It panics if the value doesn't exist. -func (c *Context) Get(key string) interface{} { - var ok bool - var item interface{} +func (c *Context) Get(key string) (interface{}, error) { if c.Keys != nil { - item, ok = c.Keys[key] - } else { - item, ok = nil, false + item, ok := c.Keys[key] + if ok { + return item, nil + } } - if !ok || item == nil { - log.Panicf("Key %s doesn't exist", key) - } - return item + return nil, errors.New("Key does not exist.") } /************************************/ From abe076b8f8961378daecca4d8309929302f3c37a Mon Sep 17 00:00:00 2001 From: Nick Gerakines Date: Thu, 3 Jul 2014 18:16:41 -0400 Subject: [PATCH 2/2] Adding MustGet method. Updating README. --- README.md | 14 +++++++------- gin.go | 13 +++++++++++-- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 53efb203..7c36793e 100644 --- a/README.md +++ b/README.md @@ -275,14 +275,14 @@ func main() { func Logger() gin.HandlerFunc { return func(c *gin.Context) { t := time.Now() - + // Set example variable c.Set("example", "12345") - + // before request - + c.Next() - + // after request latency := time.Since(t) log.Print(latency) @@ -292,10 +292,10 @@ func Logger() gin.HandlerFunc { func main() { r := gin.New() r.Use(Logger()) - + r.GET("/test", func(c *gin.Context){ - example := r.Get("example").(string) - + example := c.MustGet("example").(string) + // it would print: "12345" log.Println(example) }) diff --git a/gin.go b/gin.go index e29fe9dc..48a2b6db 100644 --- a/gin.go +++ b/gin.go @@ -8,6 +8,7 @@ import ( "fmt" "github.com/julienschmidt/httprouter" "html/template" + "log" "math" "net/http" "path" @@ -280,8 +281,7 @@ func (c *Context) Set(key string, item interface{}) { c.Keys[key] = item } -// Returns the value for the given key. -// It panics if the value doesn't exist. +// Get returns the value for the given key or an error if the key does not exist. func (c *Context) Get(key string) (interface{}, error) { if c.Keys != nil { item, ok := c.Keys[key] @@ -292,6 +292,15 @@ func (c *Context) Get(key string) (interface{}, error) { return nil, errors.New("Key does not exist.") } +// MustGet returns the value for the given key or panics if the value doesn't exist. +func (c *Context) MustGet(key string) interface{} { + value, err := c.Get(key) + if err != nil || value == nil { + log.Panicf("Key %s doesn't exist", key) + } + return value +} + /************************************/ /******** ENCOGING MANAGEMENT********/ /************************************/