Skip to content

Commit 8d9b8e4

Browse files
committed
* Added sugar.WithUserPassword(user,password) option for sugar.DSN() helper
* Added `sugar.WithSecure(bool)` option for `sugar.DSN()` helper * Small breaking change: `sugar.DSN` have only two required parameters (endpoint and database) from now on. Third parameter `secure` must be passed as option `sugar.WithSecure(bool)`
1 parent 2f1bb69 commit 8d9b8e4

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Added `sugar.WithUserPassword(user,password)` option for `sugar.DSN()` helper
2+
* Added `sugar.WithSecure(bool)` option for `sugar.DSN()` helper
3+
* Small breaking change: `sugar.DSN` have only two required parameters (endpoint and database) from now on.
4+
Third parameter `secure` must be passed as option `sugar.WithSecure(bool)`
5+
16
## v3.92.0
27
* Added experimental ydb.ParamsFromMap and ydb.MustParamsFromMap for build query parameters
38
* Refactored coordination traces

sugar/dsn.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type dsnOption func(dsn *url.URL)
1111
// )
1212

1313
// DSN makes connection string (data source name) by endpoint, database and secure
14-
func DSN(endpoint, database string, secure bool, opts ...dsnOption) (s string) {
14+
func DSN(endpoint, database string, opts ...dsnOption) (s string) {
1515
qp := url.Values{}
1616

1717
dsn := url.URL{
@@ -21,17 +21,23 @@ func DSN(endpoint, database string, secure bool, opts ...dsnOption) (s string) {
2121
RawQuery: qp.Encode(),
2222
}
2323

24-
if secure {
25-
dsn.Scheme = "grpcs"
26-
}
27-
2824
for _, opt := range opts {
2925
opt(&dsn)
3026
}
3127

3228
return dsn.String()
3329
}
3430

31+
func WithSecure(secure bool) dsnOption {
32+
return func(dsn *url.URL) {
33+
if secure {
34+
dsn.Scheme = "grpcs"
35+
} else {
36+
dsn.Scheme = "grpc"
37+
}
38+
}
39+
}
40+
3541
func WithUserPassword(user string, password string) dsnOption {
3642
return func(dsn *url.URL) {
3743
dsn.User = url.UserPassword(user, password)

sugar/dsn_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,27 @@ func TestDSN(t *testing.T) {
1515
exp string
1616
}{
1717
{
18-
DSN("localhost:2135", "/local", false),
18+
DSN("localhost:2135", "/local"),
1919
"grpc://localhost:2135/local",
2020
},
2121
{
22-
DSN("localhost:2135", "/local", false, WithUserPassword("user", "")),
22+
DSN("localhost:2135", "/local", WithUserPassword("user", "")),
2323
"grpc://user@localhost:2135/local",
2424
},
2525
{
26-
DSN("localhost:2135", "/local", false, WithUserPassword("user", "password")),
26+
DSN("localhost:2135", "/local", WithUserPassword("user", "password")),
2727
"grpc://user:password@localhost:2135/local",
2828
},
2929
{
30-
DSN("ydb-ru.yandex.net:2135", "/ru/home/gvit/mydb", false),
30+
DSN("ydb-ru.yandex.net:2135", "/ru/home/gvit/mydb"),
3131
"grpc://ydb-ru.yandex.net:2135/ru/home/gvit/mydb",
3232
},
3333
{
34-
DSN("ydb.serverless.yandexcloud.net:2135", "/ru-central1/b1g8skpblkos03malf3s/etn02qso4v3isjb00te1", true),
34+
DSN("ydb.serverless.yandexcloud.net:2135", "/ru-central1/b1g8skpblkos03malf3s/etn02qso4v3isjb00te1", WithSecure(true)), //nolint:lll
3535
"grpcs://ydb.serverless.yandexcloud.net:2135/ru-central1/b1g8skpblkos03malf3s/etn02qso4v3isjb00te1",
3636
},
3737
{
38-
DSN("lb.etn03r9df42nb631unbv.ydb.mdb.yandexcloud.net:2135", "/ru-central1/b1g8skpblkos03malf3s/etn03r9df42nb631unbv", true), //nolint:lll
38+
DSN("lb.etn03r9df42nb631unbv.ydb.mdb.yandexcloud.net:2135", "/ru-central1/b1g8skpblkos03malf3s/etn03r9df42nb631unbv", WithSecure(true)), //nolint:lll
3939
"grpcs://lb.etn03r9df42nb631unbv.ydb.mdb.yandexcloud.net:2135/ru-central1/b1g8skpblkos03malf3s/etn03r9df42nb631unbv",
4040
},
4141
} {

0 commit comments

Comments
 (0)