Merge 9401604c4e6bbdf10c208f3ab6ca740ec41a8a08 into 8763f33c65f7df8be5b9fe7504ab7fcf20abb41d

This commit is contained in:
MizuKuma 2025-03-23 09:00:37 +08:00 committed by GitHub
commit 203d174232
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 0 deletions

View File

@ -458,6 +458,16 @@ func (c *Context) GetStringMapStringSlice(key string) (smss map[string][]string)
return getTyped[map[string][]string](c, key) return getTyped[map[string][]string](c, key)
} }
// 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 ************/ /************ INPUT DATA ************/
/************************************/ /************************************/

View File

@ -276,6 +276,22 @@ func TestContextSetGetValues(t *testing.T) {
assert.Exactly(t, 1, c.MustGet("intInterface").(int)) assert.Exactly(t, 1, c.MustGet("intInterface").(int))
} }
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) { func TestContextGetString(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder()) c, _ := CreateTestContext(httptest.NewRecorder())
c.Set("string", "this is a string") c.Set("string", "this is a string")