From 388bc455696a921be827acf6d47c59ffa51a7307 Mon Sep 17 00:00:00 2001 From: nil <33080670+Arktische@users.noreply.github.com> Date: Sat, 13 Nov 2021 10:55:02 +0800 Subject: [PATCH 1/2] add iterator to Keys --- context.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/context.go b/context.go index 2aa91e11..fe983943 100644 --- a/context.go +++ b/context.go @@ -260,6 +260,16 @@ func (c *Context) Get(key string) (value interface{}, exists bool) { return } +// Do iterates the key/value pairs stored in c.Keys and executes the callback. +// It does nothing if c.Keys is nil or empty. +func (c *Context) Do(fn func(key string, value interface{})) { + c.mu.RLock() + for k, v := range c.Keys { + fn(k,v) + } + c.mu.RUnlock() +} + // MustGet returns the value for the given key if it exists, otherwise it panics. func (c *Context) MustGet(key string) interface{} { if value, exists := c.Get(key); exists { From 9401604c4e6bbdf10c208f3ab6ca740ec41a8a08 Mon Sep 17 00:00:00 2001 From: Arktische Date: Sat, 13 Nov 2021 13:23:39 +0800 Subject: [PATCH 2/2] add test for context iterate --- context.go | 20 ++++++++++---------- context_test.go | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/context.go b/context.go index fe983943..ffefa5f6 100644 --- a/context.go +++ b/context.go @@ -260,16 +260,6 @@ func (c *Context) Get(key string) (value interface{}, exists bool) { return } -// Do iterates the key/value pairs stored in c.Keys and executes the callback. -// It does nothing if c.Keys is nil or empty. -func (c *Context) Do(fn func(key string, value interface{})) { - c.mu.RLock() - for k, v := range c.Keys { - fn(k,v) - } - c.mu.RUnlock() -} - // MustGet returns the value for the given key if it exists, otherwise it panics. func (c *Context) MustGet(key string) interface{} { if value, exists := c.Get(key); exists { @@ -382,6 +372,16 @@ func (c *Context) GetStringMapStringSlice(key string) (smss map[string][]string) return } +// Iterate iterates the key/value pairs stored in c.Keys and executes the callback. +// It does nothing if c.Keys is nil or empty. +func (c *Context) Iterate(fn func(key string, value interface{})) { + c.mu.RLock() + for k, v := range c.Keys { + fn(k, v) + } + c.mu.RUnlock() +} + /************************************/ /************ INPUT DATA ************/ /************************************/ diff --git a/context_test.go b/context_test.go index c286c0f4..3bd29695 100644 --- a/context_test.go +++ b/context_test.go @@ -222,6 +222,22 @@ func TestContextSetGetValues(t *testing.T) { assert.Exactly(t, c.MustGet("intInterface").(int), 1) } +func TestContextIterate(t *testing.T) { + c, _ := CreateTestContext(httptest.NewRecorder()) + c.Set("string", "this is a string") + c.Set("int32", int32(-42)) + c.Set("int64", int64(42424242424242)) + c.Set("uint64", uint64(42)) + c.Set("float32", float32(4.2)) + c.Set("float64", 4.2) + var a interface{} = 1 + c.Set("intInterface", a) + + c.Iterate(func(key string, value interface{}) { + assert.Exactly(t, c.MustGet(key), value) + }) +} + func TestContextGetString(t *testing.T) { c, _ := CreateTestContext(httptest.NewRecorder()) c.Set("string", "this is a string")