From 1f3e8a46c5bb4c29971d9867ede6e198ca5eddce Mon Sep 17 00:00:00 2001 From: Kamil Kisiel Date: Thu, 22 May 2014 15:03:43 -0700 Subject: [PATCH] Return a shallow copy of the result in GetAll and GetAllOk. Fixes #13 --- context.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/context.go b/context.go index a7f7d85..2a7357f 100644 --- a/context.go +++ b/context.go @@ -54,20 +54,28 @@ func GetOk(r *http.Request, key interface{}) (interface{}, bool) { func GetAll(r *http.Request) map[interface{}]interface{} { mutex.RLock() if context, ok := data[r]; ok { + result := make(map[interface{}]interface{}, len(context)) + for k, v := range context { + result[k] = v + } mutex.RUnlock() - return context + return result } mutex.RUnlock() 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) { mutex.RLock() context, ok := data[r] + result := make(map[interface{}]interface{}, len(context)) + for k, v := range context { + result[k] = v + } mutex.RUnlock() - return context, ok + return result, ok } // Delete removes a value stored for a given key in a given request.