mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-15 13:02:14 +08:00
Append queue
This commit is contained in:
parent
05d01d2282
commit
1853e16bc9
20
context.go
20
context.go
@ -69,6 +69,7 @@ type Context struct {
|
|||||||
|
|
||||||
Engine *Engine
|
Engine *Engine
|
||||||
Keys map[string]interface{}
|
Keys map[string]interface{}
|
||||||
|
Queue []interface{}
|
||||||
Errors errorMsgs
|
Errors errorMsgs
|
||||||
Accepted []string
|
Accepted []string
|
||||||
}
|
}
|
||||||
@ -271,6 +272,25 @@ func (c *Context) Value(key interface{}) interface{} {
|
|||||||
return c.Context.Value(key)
|
return c.Context.Value(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Enqueue new item
|
||||||
|
func (c *Context) Enqueue(item interface{}) {
|
||||||
|
if c.Queue == nil {
|
||||||
|
c.Queue = []interface{}{}
|
||||||
|
}
|
||||||
|
c.Queue = append(c.Queue, item)
|
||||||
|
}
|
||||||
|
|
||||||
|
//Dequeue returns the value from top of the queue
|
||||||
|
func (c *Context) Dequeue () (interface{}, error) {
|
||||||
|
size := len(c.Queue)
|
||||||
|
if c.Queue == nil || size == 0 {
|
||||||
|
return nil, errors.New("Queue is empty")
|
||||||
|
}
|
||||||
|
result := c.Queue[size-1]
|
||||||
|
c.Queue = c.Queue[0:size-1]
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
/************************************/
|
/************************************/
|
||||||
/********* PARSING REQUEST **********/
|
/********* PARSING REQUEST **********/
|
||||||
/************************************/
|
/************************************/
|
||||||
|
@ -91,6 +91,16 @@ func TestContextSetGetValues(t *testing.T) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContextQueue(t *testing.T) {
|
||||||
|
c, _, _ := createTestContext()
|
||||||
|
c.Enqueue("value")
|
||||||
|
c.Enqueue("data")
|
||||||
|
v, _ := c.Dequeue()
|
||||||
|
assert.Exactly(t, v, "data")
|
||||||
|
v2, _ := c.Dequeue()
|
||||||
|
assert.Exactly(t, v2, "value")
|
||||||
|
}
|
||||||
|
|
||||||
func TestContextCopy(t *testing.T) {
|
func TestContextCopy(t *testing.T) {
|
||||||
c, _, _ := createTestContext()
|
c, _, _ := createTestContext()
|
||||||
c.index = 2
|
c.index = 2
|
||||||
|
Loading…
x
Reference in New Issue
Block a user