forked from denisenkom/go-mssqldb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect_interceptor.go
42 lines (35 loc) · 1.5 KB
/
connect_interceptor.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
package mssql
import "context"
// contextKey is, as the name implied, a type reserved
// for keys when passing values into the context
type contextKey string
const connectInterceptorKey contextKey = "connectInterceptor"
// ConnectInterceptor is used to exchange values between the driver and the user during
// the connection phase
type ConnectInterceptor struct {
// ServerPreLoginResponse is used to obtain PreLogin Response fields
ServerPreLoginResponse chan map[uint8][]byte
// ClientLoginRequest is used to pass the client LoginRequest
ClientLoginRequest chan *LoginRequest
}
// NewConnectInterceptor is a constructor for a blank ConnectInterceptor
func NewConnectInterceptor() *ConnectInterceptor {
return &ConnectInterceptor{
// Create a channel for receiving the prelogin response through the context
ServerPreLoginResponse: make(chan map[uint8][]byte),
// Create a channel for sending the client login to the driver through the context
ClientLoginRequest: make(chan *LoginRequest),
}
}
// NewContextWithConnectInterceptor returns a new Context that carries value ci.
func NewContextWithConnectInterceptor(ctx context.Context, u *ConnectInterceptor) context.Context {
return context.WithValue(ctx, connectInterceptorKey, u)
}
// ConnectInterceptorFromContext returns the ConnectInterceptor value stored in ctx, if any.
func ConnectInterceptorFromContext(ctx context.Context) *ConnectInterceptor {
ci := ctx.Value(connectInterceptorKey)
if ci == nil {
return nil
}
return ci.(*ConnectInterceptor)
}