Browse Source

Return a shallow copy of the result in GetAll and GetAllOk.

Fixes #13
Kamil Kisiel 12 years ago
parent
commit
1f3e8a46c5
1 changed files with 12 additions and 4 deletions
  1. +12
    -4
      context.go

+ 12
- 4
context.go View File

@ -54,20 +54,28 @@ func GetOk(r *http.Request, key interface{}) (interface{}, bool) {
func GetAll(r *http.Request) map[interface{}]interface{} { func GetAll(r *http.Request) map[interface{}]interface{} {
mutex.RLock() mutex.RLock()
if context, ok := data[r]; ok { if context, ok := data[r]; ok {
result := make(map[interface{}]interface{}, len(context))
for k, v := range context {
result[k] = v
}
mutex.RUnlock() mutex.RUnlock()
return context
return result
} }
mutex.RUnlock() mutex.RUnlock()
return nil return nil
} }
// GetAllOk returns all stored values for the request as a map. It returns not
// ok if the request was never registered.
// GetAllOk returns all stored values for the request as a map and a boolean value that indicates if
// the request was registered.
func GetAllOk(r *http.Request) (map[interface{}]interface{}, bool) { func GetAllOk(r *http.Request) (map[interface{}]interface{}, bool) {
mutex.RLock() mutex.RLock()
context, ok := data[r] context, ok := data[r]
result := make(map[interface{}]interface{}, len(context))
for k, v := range context {
result[k] = v
}
mutex.RUnlock() mutex.RUnlock()
return context, ok
return result, ok
} }
// Delete removes a value stored for a given key in a given request. // Delete removes a value stored for a given key in a given request.


Loading…
Cancel
Save