Skip to content

Commit 4d8524a

Browse files
committed
* Added public package dsn for making piped data source name (connection string)
* Marked `ydb.WithEndpoint`, `ydb.WithDatabase`, `ydb.WithSecure`, `ydb.WithInsecure` options as deprecated * Moved `ydb.RegisterParser` to package `dsn`
1 parent 6623d86 commit 4d8524a

File tree

4 files changed

+83
-9
lines changed

4 files changed

+83
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
* Added public package `dsn` for making piped data source name (connection string)
2+
* Marked `ydb.WithEndpoint`, `ydb.WithDatabase`, `ydb.WithSecure`, `ydb.WithInsecure` options as deprecated
3+
* Moved `ydb.RegisterParser` to package `dsn`
14
* Added version into all error and warm log messages
25

36
## v3.18.5

dsn/dsn.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package dsn
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// Usage of this package
8+
//
9+
// db, err := ydb.New(
10+
// ctx,
11+
// ydb.WithConnectionString(
12+
// New("endpoint", "database").WithSecure(false).String(),
13+
// ),
14+
//)
15+
16+
// DSN helps to make connection string from separated endpoint and database
17+
type DSN interface {
18+
fmt.Stringer
19+
20+
// WithSecure makes new DSN from current DSN with passed secure flag
21+
WithSecure(secure bool) DSN
22+
}
23+
24+
type connectionString struct {
25+
endpoint string
26+
database string
27+
secure bool
28+
}
29+
30+
func (cs *connectionString) String() (s string) {
31+
s = "grpc"
32+
if cs.secure {
33+
s += "s"
34+
}
35+
return s + "://" + cs.endpoint + "/?database=" + cs.database
36+
}
37+
38+
func (cs *connectionString) WithSecure(secure bool) DSN {
39+
return &connectionString{
40+
secure: secure,
41+
endpoint: cs.endpoint,
42+
database: cs.database,
43+
}
44+
}
45+
46+
// New makes secured DSN with endpoint and database
47+
func New(endpoint, database string) DSN {
48+
return &connectionString{
49+
endpoint: endpoint,
50+
database: database,
51+
secure: true,
52+
}
53+
}

dsn/parser.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package dsn
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/ydb-platform/ydb-go-sdk/v3/config"
7+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/dsn"
8+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
9+
)
10+
11+
func RegisterParser(param string, parser func(value string) ([]config.Option, error)) (err error) {
12+
err = dsn.Register(param, parser)
13+
if err != nil {
14+
return xerrors.WithStackTrace(fmt.Errorf("%w: %s", err, param))
15+
}
16+
return nil
17+
}

options.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"crypto/x509"
66
"encoding/pem"
7-
"fmt"
87
"io/ioutil"
98
"os"
109
"path/filepath"
@@ -69,6 +68,8 @@ func WithRequestsType(requestsType string) Option {
6968
}
7069

7170
// WithConnectionString accept connection string like 'grpc[s]://{endpoint}/?database={database}'
71+
// Warning: WithConnectionString will be removed at next major release
72+
// (connection string will be required string param of ydb.New)
7273
func WithConnectionString(connectionString string) Option {
7374
return func(ctx context.Context, c *connection) error {
7475
var (
@@ -96,14 +97,6 @@ func WithConnectionString(connectionString string) Option {
9697
}
9798
}
9899

99-
func RegisterParser(param string, parser func(value string) ([]config.Option, error)) (err error) {
100-
err = dsn.Register(param, parser)
101-
if err != nil {
102-
return xerrors.WithStackTrace(fmt.Errorf("%w: %s", err, param))
103-
}
104-
return nil
105-
}
106-
107100
// WithConnectionTTL defines duration for parking idle connections
108101
// Warning: if defined WithSessionPoolIdleThreshold - idleThreshold must be less than connectionTTL
109102
func WithConnectionTTL(ttl time.Duration) Option {
@@ -113,27 +106,35 @@ func WithConnectionTTL(ttl time.Duration) Option {
113106
}
114107
}
115108

109+
// WithEndpoint defines endpoint option
110+
// Deprecated: use WithConnectionString or dsn package instead
116111
func WithEndpoint(endpoint string) Option {
117112
return func(ctx context.Context, c *connection) error {
118113
c.options = append(c.options, config.WithEndpoint(endpoint))
119114
return nil
120115
}
121116
}
122117

118+
// WithDatabase defines database option
119+
// Deprecated: use WithConnectionString or dsn package instead
123120
func WithDatabase(database string) Option {
124121
return func(ctx context.Context, c *connection) error {
125122
c.options = append(c.options, config.WithDatabase(database))
126123
return nil
127124
}
128125
}
129126

127+
// WithSecure defines secure option
128+
// Deprecated: use WithConnectionString or dsn package instead
130129
func WithSecure(secure bool) Option {
131130
return func(ctx context.Context, c *connection) error {
132131
c.options = append(c.options, config.WithSecure(secure))
133132
return nil
134133
}
135134
}
136135

136+
// WithInsecure defines secure option
137+
// Deprecated: use WithConnectionString or dsn package instead
137138
func WithInsecure() Option {
138139
return func(ctx context.Context, c *connection) error {
139140
c.options = append(c.options, config.WithSecure(false))

0 commit comments

Comments
 (0)