-
Notifications
You must be signed in to change notification settings - Fork 247
/
datastore.go
85 lines (70 loc) · 2.63 KB
/
datastore.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
package datastore
import (
"context"
middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2"
"google.golang.org/grpc"
"github.com/authzed/spicedb/pkg/datastore"
)
type ctxKeyType struct{}
var datastoreKey ctxKeyType = struct{}{}
type datastoreHandle struct {
datastore datastore.Datastore
}
// ContextWithHandle adds a placeholder to a context that will later be
// filled by the datastore
func ContextWithHandle(ctx context.Context) context.Context {
return context.WithValue(ctx, datastoreKey, &datastoreHandle{})
}
// FromContext reads the selected datastore out of a context.Context
// and returns nil if it does not exist.
func FromContext(ctx context.Context) datastore.Datastore {
if c := ctx.Value(datastoreKey); c != nil {
handle := c.(*datastoreHandle)
return handle.datastore
}
return nil
}
// MustFromContext reads the selected datastore out of a context.Context and panics if it does not exist
func MustFromContext(ctx context.Context) datastore.Datastore {
datastore := FromContext(ctx)
if datastore == nil {
panic("datastore middleware did not inject datastore")
}
return datastore
}
// SetInContext adds a datastore to the given context
func SetInContext(ctx context.Context, datastore datastore.Datastore) error {
handle := ctx.Value(datastoreKey)
if handle == nil {
return nil
}
handle.(*datastoreHandle).datastore = datastore
return nil
}
// ContextWithDatastore adds the handle and datastore in one step
func ContextWithDatastore(ctx context.Context, datastore datastore.Datastore) context.Context {
return context.WithValue(ctx, datastoreKey, &datastoreHandle{datastore: datastore})
}
// UnaryServerInterceptor returns a new unary server interceptor that adds the
// datastore to the context
func UnaryServerInterceptor(datastore datastore.Datastore) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
newCtx := ContextWithHandle(ctx)
if err := SetInContext(newCtx, datastore); err != nil {
return nil, err
}
return handler(newCtx, req)
}
}
// StreamServerInterceptor returns a new stream server interceptor that adds the
// datastore to the context
func StreamServerInterceptor(datastore datastore.Datastore) grpc.StreamServerInterceptor {
return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
wrapped := middleware.WrapServerStream(stream)
wrapped.WrappedContext = ContextWithHandle(wrapped.WrappedContext)
if err := SetInContext(wrapped.WrappedContext, datastore); err != nil {
return err
}
return handler(srv, wrapped)
}
}