summaryrefslogtreecommitdiff
path: root/plugins/http/attributes/attributes.go
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2020-11-18 18:15:09 +0300
committerValery Piashchynski <[email protected]>2020-11-18 18:15:09 +0300
commit8fab090abc369237d5f9be2ee676005b24c2a470 (patch)
tree9f7fc358b66357f0218b8256d8073616995b91db /plugins/http/attributes/attributes.go
parent4ccd58fc363264d24f642ab7e0ccfe6538a0b91c (diff)
Handler test
Diffstat (limited to 'plugins/http/attributes/attributes.go')
-rw-r--r--plugins/http/attributes/attributes.go21
1 files changed, 15 insertions, 6 deletions
diff --git a/plugins/http/attributes/attributes.go b/plugins/http/attributes/attributes.go
index 77d6ea69..4c453766 100644
--- a/plugins/http/attributes/attributes.go
+++ b/plugins/http/attributes/attributes.go
@@ -6,9 +6,18 @@ import (
"net/http"
)
-type attrKey int
+// contextKey is a value for use with context.WithValue. It's used as
+// a pointer so it fits in an interface{} without allocation.
+type contextKey struct {
+ name string
+}
+
+func (k *contextKey) String() string { return k.name }
-const contextKey attrKey = iota
+var (
+ // PsrContextKey is a context key. It can be used in the http attributes
+ PsrContextKey = &contextKey{"psr_attributes"}
+)
type attrs map[string]interface{}
@@ -30,12 +39,12 @@ func (v attrs) del(key string) {
// Init returns request with new context and attribute bag.
func Init(r *http.Request) *http.Request {
- return r.WithContext(context.WithValue(r.Context(), contextKey, attrs{}))
+ return r.WithContext(context.WithValue(r.Context(), PsrContextKey, attrs{}))
}
// All returns all context attributes.
func All(r *http.Request) map[string]interface{} {
- v := r.Context().Value(contextKey)
+ v := r.Context().Value(PsrContextKey)
if v == nil {
return attrs{}
}
@@ -46,7 +55,7 @@ func All(r *http.Request) map[string]interface{} {
// Get gets the value from request context. It replaces any existing
// values.
func Get(r *http.Request, key string) interface{} {
- v := r.Context().Value(contextKey)
+ v := r.Context().Value(PsrContextKey)
if v == nil {
return nil
}
@@ -57,7 +66,7 @@ func Get(r *http.Request, key string) interface{} {
// Set sets the key to value. It replaces any existing
// values. Context specific.
func Set(r *http.Request, key string, value interface{}) error {
- v := r.Context().Value(contextKey)
+ v := r.Context().Value(PsrContextKey)
if v == nil {
return errors.New("unable to find `psr:attributes` context key")
}