Merge branch 'master' of https://github.com/ngerakines/gin into ngerakines-master
It adds MustGet() method in context. Conflicts: examples/example_basic.go
This commit is contained in:
		@ -307,7 +307,7 @@ func main() {
 | 
				
			|||||||
    r.Use(Logger())
 | 
					    r.Use(Logger())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    r.GET("/test", func(c *gin.Context){
 | 
					    r.GET("/test", func(c *gin.Context){
 | 
				
			||||||
        example := r.Get("example").(string)
 | 
					        example := c.MustGet("example").(string)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // it would print: "12345"
 | 
					        // it would print: "12345"
 | 
				
			||||||
        log.Println(example)
 | 
					        log.Println(example)
 | 
				
			||||||
 | 
				
			|||||||
@ -38,7 +38,7 @@ func main() {
 | 
				
			|||||||
	}))
 | 
						}))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	authorized.POST("admin", func(c *gin.Context) {
 | 
						authorized.POST("admin", func(c *gin.Context) {
 | 
				
			||||||
		user := c.Get(gin.AuthUserKey).(string)
 | 
							user := c.MustGet(gin.AuthUserKey).(string)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Parse JSON
 | 
							// Parse JSON
 | 
				
			||||||
		var json struct {
 | 
							var json struct {
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										24
									
								
								gin.go
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								gin.go
									
									
									
									
									
								
							@ -355,20 +355,24 @@ func (c *Context) Set(key string, item interface{}) {
 | 
				
			|||||||
	c.Keys[key] = item
 | 
						c.Keys[key] = item
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Returns the value for the given key.
 | 
					// Get returns the value for the given key or an error if the key does not exist.
 | 
				
			||||||
// It panics if the value doesn't exist.
 | 
					func (c *Context) Get(key string) (interface{}, error) {
 | 
				
			||||||
func (c *Context) Get(key string) interface{} {
 | 
					 | 
				
			||||||
	var ok bool
 | 
					 | 
				
			||||||
	var item interface{}
 | 
					 | 
				
			||||||
	if c.Keys != nil {
 | 
						if c.Keys != nil {
 | 
				
			||||||
		item, ok = c.Keys[key]
 | 
							item, ok := c.Keys[key]
 | 
				
			||||||
	} else {
 | 
							if ok {
 | 
				
			||||||
		item, ok = nil, false
 | 
								return item, nil
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	if !ok || item == nil {
 | 
						}
 | 
				
			||||||
 | 
						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)
 | 
							log.Panicf("Key %s doesn't exist", key)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return item
 | 
						return value
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/************************************/
 | 
					/************************************/
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user