-
Notifications
You must be signed in to change notification settings - Fork 5
/
ctx_helper.go
33 lines (30 loc) · 1017 Bytes
/
ctx_helper.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
package gobis
import (
"context"
"fmt"
"net/http"
"reflect"
)
// AddContextValue Add a context value to a http request without having to override request by yourself
func AddContextValue(req *http.Request, key, val interface{}) {
parentContext := req.Context()
ctxValueReq := req.WithContext(context.WithValue(parentContext, key, val))
*req = *ctxValueReq
}
// InjectContextValue Inject a value from a request context to an interface
// This is the same behaviour as json.Unmarshal
func InjectContextValue(req *http.Request, key, inject interface{}) error {
if reflect.TypeOf(inject).Kind() != reflect.Ptr {
return fmt.Errorf("you should pass a pointer")
}
reflectType := reflect.TypeOf(inject).Elem()
val := req.Context().Value(key)
if val == nil {
return nil
}
if !reflect.TypeOf(val).AssignableTo(reflectType) {
return fmt.Errorf("inject for type '%s' incompatible with type '%s'", reflect.TypeOf(val), reflectType)
}
reflect.ValueOf(inject).Elem().Set(reflect.ValueOf(val))
return nil
}