generated from sv-tools/go-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
session.go
105 lines (82 loc) · 2.55 KB
/
session.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
package mongoifc
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// Session is an interface for `mongo.Session` structure
// Documentation: https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#Session
type Session interface {
// Functions to modify session state.
StartTransaction(opts ...*options.TransactionOptions) error
AbortTransaction(ctx context.Context) error
CommitTransaction(ctx context.Context) error
WithTransaction(
ctx context.Context,
fn func(sc SessionContext) (interface{}, error),
opts ...*options.TransactionOptions,
) (interface{}, error)
EndSession(ctx context.Context)
// Functions to retrieve session properties.
ClusterTime() bson.Raw
OperationTime() *primitive.Timestamp
Client() Client
ID() bson.Raw
// Functions to modify mutable session properties.
AdvanceClusterTime(bson.Raw) error
AdvanceOperationTime(*primitive.Timestamp) error
}
type session struct {
ss mongo.Session
cl *client
}
func (s *session) StartTransaction(opts ...*options.TransactionOptions) error {
return s.ss.StartTransaction(opts...)
}
func (s *session) AbortTransaction(ctx context.Context) error {
return s.ss.AbortTransaction(ctx)
}
func (s *session) CommitTransaction(ctx context.Context) error {
return s.ss.CommitTransaction(ctx)
}
func (s *session) WithTransaction(
ctx context.Context,
fn func(sc SessionContext) (interface{}, error),
opts ...*options.TransactionOptions,
) (interface{}, error) {
return s.ss.WithTransaction(ctx, wrapFn2(fn, s.cl), opts...)
}
func (s *session) EndSession(ctx context.Context) {
s.ss.EndSession(ctx)
}
func (s *session) ClusterTime() bson.Raw {
return s.ss.ClusterTime()
}
func (s *session) OperationTime() *primitive.Timestamp {
return s.ss.OperationTime()
}
func (s *session) Client() Client {
return s.cl
}
func (s *session) ID() bson.Raw {
return s.ss.ID()
}
func (s *session) AdvanceClusterTime(d bson.Raw) error {
return s.ss.AdvanceClusterTime(d)
}
func (s *session) AdvanceOperationTime(ts *primitive.Timestamp) error {
return s.ss.AdvanceOperationTime(ts)
}
func wrapSession(ss mongo.Session, cl *client) Session {
return &session{ss: ss, cl: cl}
}
// WrapSession returns an instance of Session interface for given mongo.Session object
func WrapSession(ss mongo.Session) Session {
return wrapSession(ss, WrapClient(ss.Client()).(*client))
}
// UnWrapSession returns original mongo.Session
func UnWrapSession(ss Session) mongo.Session {
return ss.(*session).ss
}