This repository was archived by the owner on May 27, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsession.go
More file actions
131 lines (106 loc) · 3.82 KB
/
Copy pathsession.go
File metadata and controls
131 lines (106 loc) · 3.82 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package mongoifc
import (
"context"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
// Session is an interface for `mongo.Session` structure
// Documentation: https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/mongo#Session
type Session interface {
// Functions to modify session state.
StartTransaction(opts ...options.Lister[options.TransactionOptions]) error
AbortTransaction(ctx context.Context) error
CommitTransaction(ctx context.Context) error
WithTransaction(
ctx context.Context,
fn func(ctx context.Context) (any, error),
opts ...options.Lister[options.TransactionOptions],
) (any, error)
EndSession(ctx context.Context)
// Functions to retrieve session properties.
ClusterTime() bson.Raw
OperationTime() *bson.Timestamp
Client() Client
ID() bson.Raw
SnapshotTime() bson.Timestamp
// Functions to modify mutable session properties.
AdvanceClusterTime(d bson.Raw) error
AdvanceOperationTime(ts *bson.Timestamp) error
}
type session struct {
ss *mongo.Session
cl *client
}
// StartTransaction is a wrapper for `mongo.Session.StartTransaction` method
func (s *session) StartTransaction(opts ...options.Lister[options.TransactionOptions]) error {
return s.ss.StartTransaction(opts...)
}
// AbortTransaction is a wrapper for `mongo.Session.AbortTransaction` method
func (s *session) AbortTransaction(ctx context.Context) error {
return s.ss.AbortTransaction(ctx)
}
// CommitTransaction is a wrapper for `mongo.Session.CommitTransaction` method
func (s *session) CommitTransaction(ctx context.Context) error {
return s.ss.CommitTransaction(ctx)
}
// WithTransaction is a wrapper for `mongo.Session.WithTransaction` method
func (s *session) WithTransaction(
ctx context.Context,
fn func(ctx context.Context) (any, error),
opts ...options.Lister[options.TransactionOptions],
) (any, error) {
return s.ss.WithTransaction(ctx, fn, opts...)
}
// EndSession is a wrapper for `mongo.Session.EndSession` method
func (s *session) EndSession(ctx context.Context) {
s.ss.EndSession(ctx)
}
// ClusterTime is a wrapper for `mongo.Session.ClusterTime` method
func (s *session) ClusterTime() bson.Raw {
return s.ss.ClusterTime()
}
// OperationTime is a wrapper for `mongo.Session.OperationTime` method
func (s *session) OperationTime() *bson.Timestamp {
return s.ss.OperationTime()
}
// Client is a wrapper for `mongo.Session.Client` method
func (s *session) Client() Client {
return s.cl
}
// ID is a wrapper for `mongo.Session.ID` method
func (s *session) ID() bson.Raw {
return s.ss.ID()
}
// SnapshotTime is a wrapper for `mongo.Session.SnapshotTime` method
func (s *session) SnapshotTime() bson.Timestamp {
return s.ss.SnapshotTime()
}
// AdvanceClusterTime is a wrapper for `mongo.Session.AdvanceClusterTime` method
func (s *session) AdvanceClusterTime(d bson.Raw) error {
return s.ss.AdvanceClusterTime(d)
}
// AdvanceOperationTime is a wrapper for `mongo.Session.AdvanceOperationTime` method
func (s *session) AdvanceOperationTime(ts *bson.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
}
// SessionFromContext is a wrapper for `mongo.SessionFromContext` function to return the object as `Session` interface
// Documentation: https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/mongo#SessionFromContext
func SessionFromContext(ctx context.Context) Session {
ss := mongo.SessionFromContext(ctx)
if ss == nil {
return nil
}
return WrapSession(ss)
}