-
Notifications
You must be signed in to change notification settings - Fork 2
/
context.go
108 lines (95 loc) · 2.96 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Package context implements a simple request-aware HTTP context designed
// to be used via middleware layer to share polymorfic data.
//
// context is not thread-safe by default.
package context
import (
"io"
"net/http"
)
// Set sets a context value on req.
// It currently accomplishes this by replacing the http.Request’s Body with
// a ReadCloser, which wraps the original io.ReadCloser.
// See “Invasion of the Body Snatchers.”
func Set(req *http.Request, key interface{}, value interface{}) {
crc := getContextReadCloser(req)
crc.Context()[key] = value
}
// Get gets a context value from req.
// Returns nil if key not found in the request context.
func Get(req *http.Request, key interface{}) interface{} {
crc := getContextReadCloser(req)
return crc.Context()[key]
}
// GetOk gets a context value from req.
// Returns (nil, false) if key not found in the request context.
func GetOk(req *http.Request, key interface{}) (val interface{}, ok bool) {
crc := getContextReadCloser(req)
val, ok = crc.Context()[key]
return
}
// GetString gets a string context value from req.
// Returns an empty string if key not found in the request context,
// or the value does not evaluate to a string.
func GetString(req *http.Request, key interface{}) string {
crc := getContextReadCloser(req)
if value, ok := crc.Context()[key]; ok {
if typed, ok := value.(string); ok {
return typed
}
}
return ""
}
// GetError gets a error context value from req.
// Returns nil if key not found in the request context,
// or the value does not evaluate to a error.
func GetError(req *http.Request, key interface{}) error {
crc := getContextReadCloser(req)
if value, ok := crc.Context()[key]; ok {
if typed, ok := value.(error); ok {
return typed
}
}
return nil
}
// GetAll returns all stored context values for a request.
// Will always return a valid map. Returns an empty map for
// requests context data previously set.
func GetAll(req *http.Request) map[interface{}]interface{} {
crc := getContextReadCloser(req)
return crc.Context()
}
// Delete deletes a stored value from a request’s context.
func Delete(req *http.Request, key interface{}) {
crc := getContextReadCloser(req)
delete(crc.Context(), key)
}
// Clear clears all stored values from a request’s context.
func Clear(req *http.Request) {
crc := getContextReadCloser(req).(*contextReadCloser)
crc.context = map[interface{}]interface{}{}
}
// ReadCloser augments the io.ReadCloser interface
// with a Context() method.
type ReadCloser interface {
io.ReadCloser
Context() map[interface{}]interface{}
}
type contextReadCloser struct {
io.ReadCloser
context map[interface{}]interface{}
}
func (crc *contextReadCloser) Context() map[interface{}]interface{} {
return crc.context
}
func getContextReadCloser(req *http.Request) ReadCloser {
crc, ok := req.Body.(ReadCloser)
if !ok {
crc = &contextReadCloser{
ReadCloser: req.Body,
context: make(map[interface{}]interface{}),
}
req.Body = crc
}
return crc
}