forked from sv-tools/mongoifc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_context.go
58 lines (49 loc) · 1.51 KB
/
session_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
package mongoifc
import (
"context"
"go.mongodb.org/mongo-driver/mongo"
)
// SessionContext is an interface emulates `mongo.SessionContext`
// Documentation: https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#SessionContext
type SessionContext interface {
context.Context
Session
}
type sessionContext struct {
context.Context
Session
}
func wrapSessionContext(sc mongo.SessionContext, cl *client) SessionContext {
return &sessionContext{
Context: sc,
Session: wrapSession(sc, cl),
}
}
func wrapFn1(
fn func(sc SessionContext) error,
cl *client,
) func(sc mongo.SessionContext) error {
return func(sc mongo.SessionContext) error {
return fn(wrapSessionContext(sc, cl))
}
}
func wrapFn2(
fn func(sc SessionContext) (interface{}, error),
cl *client,
) func(sc mongo.SessionContext) (interface{}, error) {
return func(sc mongo.SessionContext) (interface{}, error) {
return fn(wrapSessionContext(sc, cl))
}
}
// NewSessionContext is wrapper for `mongo.NewSessionContext`
// Documentation: https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#NewSessionContext
func NewSessionContext(ctx context.Context, sess Session) SessionContext {
ms := mongo.NewSessionContext(ctx, UnWrapSession(sess))
return wrapSessionContext(ms, sess.Client().(*client))
}
// SessionFromContext for `mongo.SessionFromContext`
// Documentation: https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#SessionFromContext
func SessionFromContext(ctx context.Context) Session {
ms := mongo.SessionFromContext(ctx)
return WrapSession(ms)
}