-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
57 lines (48 loc) · 1.37 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
// Package context provides functionality for wrapping values into the context.
package context
import (
"context"
"errors"
"fmt"
"time"
)
// ctxKey is the type of the value for a context key.
type ctxKey int
// key is the key under which values are stored or retrieved.
const key ctxKey = 1
// Values represent a request's state.
type Values struct {
TraceID string
Now time.Time
StatusCode int
}
// GetValues returns the Values from the context.
// Returns an error is there's no or an invalid Values in the context.
func GetValues(ctx context.Context) (*Values, error) {
v, ok := ctx.Value(key).(*Values)
if !ok {
return nil, fmt.Errorf("values missing from context")
}
return v, nil
}
// GetTraceID returns the trace id from the context.
func GetTraceID(ctx context.Context) string {
v, ok := ctx.Value(key).(*Values)
if !ok {
return "00000000-0000-0000-0000-000000000000"
}
return v.TraceID
}
// SetStatusCode sets the status code back into the context.
func SetStatusCode(ctx context.Context, statusCode int) error {
v, ok := ctx.Value(key).(*Values)
if !ok {
return errors.New("values missing from context")
}
v.StatusCode = statusCode
return nil
}
// ContextWithValues returns a new context with the provided values encapsulated.
func ContextWithValues(ctx context.Context, v *Values) context.Context {
return context.WithValue(ctx, key, v)
}