This repository has been archived by the owner on Oct 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathopt.go
169 lines (138 loc) · 3.92 KB
/
opt.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package goBolt
import (
"github.com/mindstand/go-bolt/errors"
"time"
)
type Opt func(*Client) error
// WithConnectionString provides client option with connection string
func WithConnectionString(connString string) Opt {
return func(client *Client) error {
if client == nil {
return errors.Wrap(errors.ErrConfiguration, "client can not be nil")
}
if client.host != "" || client.port != 0 {
return errors.Wrap(errors.ErrConfiguration, "can not call WithConnectionString and WithHostPort")
}
client.connStr = connString
return nil
}
}
// allows setting the host and port of neo4j
func WithHostPort(host string, port int) Opt {
return func(client *Client) error {
if client == nil {
return errors.Wrap(errors.ErrConfiguration, "client can not be nil")
}
if client.connStr != "" {
return errors.Wrap(errors.ErrConfiguration, "can not call WithHostPort and WithConnectionString")
}
client.host = host
client.port = port
return nil
}
}
// allows setting protocol to bolt+routing
func WithRouting() Opt {
return func(client *Client) error {
if client == nil {
return errors.Wrap(errors.ErrConfiguration, "client can not be nil")
}
if client.connStr != "" {
return errors.Wrap(errors.ErrConfiguration, "can not call WithRouting and WithConnectionString")
}
client.routing = true
return nil
}
}
// allows setting chunk size
func WithChunkSize(size uint16) Opt {
return func(client *Client) error {
if client == nil {
return errors.Wrap(errors.ErrConfiguration, "client can not be nil")
}
client.chunkSize = size
return nil
}
}
// allows authentication with basic auth
func WithBasicAuth(username, password string) Opt {
return func(client *Client) error {
if client == nil {
return errors.Wrap(errors.ErrConfiguration, "client can not be nil")
}
client.user = username
client.password = password
return nil
}
}
// allows authentication with tls
func WithTLS(cacertPath, certPath, keyPath string, tlsNoVerify bool) Opt {
return func(client *Client) error {
if client == nil {
return errors.Wrap(errors.ErrConfiguration, "client can not be nil")
}
client.caCertFile = cacertPath
client.certFile = certPath
client.keyFile = keyPath
client.useTLS = true
client.tlsNoVerify = tlsNoVerify
return nil
}
}
// tells client to negotiate version
func WithProtocolVersionNegotiation() Opt {
return func(client *Client) error {
if client == nil {
return errors.Wrap(errors.ErrConfiguration, "client can not be nil")
}
client.negotiateVersion = true
return nil
}
}
func WithProtocolVersionGreaterThan(version int) Opt {
return func(client *Client) error {
if client == nil {
return errors.Wrap(errors.ErrConfiguration, "client can not be nil")
}
if version < 1 || version > 3 {
return errors.Wrap(errors.ErrConfiguration, "protocol greater than version must between 1 and 3")
}
client.protocolGreaterThan = version
return nil
}
}
func WithProtocolVersionLessThan(version int) Opt {
return func(client *Client) error {
if client == nil {
return errors.Wrap(errors.ErrConfiguration, "client can not be nil")
}
if version < 2 || version > 4 {
return errors.Wrap(errors.ErrConfiguration, "protocol greater than version must between 2 and 4")
}
client.protocolLessThan = version
return nil
}
}
// requires protocol version
func WithStrictProtocolVersion(version int) Opt {
return func(client *Client) error {
if client == nil {
return errors.Wrap(errors.ErrConfiguration, "client can not be nil")
}
if version < 1 || version > 4 {
return errors.Wrap(errors.ErrConfiguration, "strict version must between 1 and 4")
}
client.serverVersion = version
return nil
}
}
// tells client what timeout it should use
func WithTimeout(timeout time.Duration) Opt {
return func(client *Client) error {
if client == nil {
return errors.Wrap(errors.ErrConfiguration, "client can not be nil")
}
client.timeout = timeout
return nil
}
}