diff options
Diffstat (limited to 'service/http/attributes.go')
-rw-r--r-- | service/http/attributes.go | 69 |
1 files changed, 0 insertions, 69 deletions
diff --git a/service/http/attributes.go b/service/http/attributes.go deleted file mode 100644 index acea38a1..00000000 --- a/service/http/attributes.go +++ /dev/null @@ -1,69 +0,0 @@ -package http - -import ( - "context" - "net/http" - "errors" -) - -const contextKey = "psr:attributes" - -type attrs map[string]interface{} - -// InitAttributes returns request with new context and attribute bag. -func InitAttributes(r *http.Request) *http.Request { - return r.WithContext(context.WithValue(r.Context(), contextKey, attrs{})) -} - -// AllAttributes returns all context attributes. -func AllAttributes(r *http.Request) map[string]interface{} { - v := r.Context().Value(contextKey) - if v == nil { - return attrs{} - } - - return v.(attrs) -} - -// Get gets the value from request context. It replaces any existing -// values. -func GetAttribute(r *http.Request, key string) interface{} { - v := r.Context().Value(contextKey) - if v == nil { - return nil - } - - return v.(attrs).Get(key) -} - -// Set sets the key to value. It replaces any existing -// values. Context specific. -func SetAttribute(r *http.Request, key string, value interface{}) error { - v := r.Context().Value(contextKey) - if v == nil { - return errors.New("unable to find psr:attributes context value") - } - - v.(attrs).Set(key, value) - return nil -} - -// Get gets the value associated with the given key. -func (v attrs) Get(key string) interface{} { - if v == nil { - return "" - } - - return v[key] -} - -// Set sets the key to value. It replaces any existing -// values. -func (v attrs) Set(key string, value interface{}) { - v[key] = value -} - -// Del deletes the value associated with key. -func (v attrs) Del(key string) { - delete(v, key) -} |