Skip to content

Commit

Permalink
* Added sugar.WithUserPassword(user,password) option for `sugar.DSN…
Browse files Browse the repository at this point in the history
…()` 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)`
  • Loading branch information
asmyasnikov committed Nov 8, 2024
1 parent fa858a8 commit 4ff82f2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
* 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)`

## v3.91.0
* Added `ydb.WithPreferredNodeID(ctx, nodeID)` context modifier for trying to execute queries on given nodeID

## v3.90.2
* Set the `pick_first` balancer for short-lived grpc connection inside ydb cluster discovery attempt

## v3.90.1
* Small broken change: added method `ID()` into `spans.Span` interface (need to implement in adapter)
* Small breaking change: added method `ID()` into `spans.Span` interface (need to implement in adapter)
* Fixed traceparent header for tracing grpc requests

## v3.90.0
Expand Down
16 changes: 11 additions & 5 deletions sugar/dsn.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type dsnOption func(dsn *url.URL)
// )

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

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

if secure {
dsn.Scheme = "grpcs"
}

for _, opt := range opts {
opt(&dsn)
}

return dsn.String()
}

func WithSecure(secure bool) dsnOption {
return func(dsn *url.URL) {
if secure {
dsn.Scheme = "grpcs"
} else {
dsn.Scheme = "grpc"
}
}
}

func WithUserPassword(user string, password string) dsnOption {
return func(dsn *url.URL) {
dsn.User = url.UserPassword(user, password)
Expand Down
12 changes: 6 additions & 6 deletions sugar/dsn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ func TestDSN(t *testing.T) {
exp string
}{
{
DSN("localhost:2135", "/local", false),
DSN("localhost:2135", "/local"),
"grpc://localhost:2135/local",
},
{
DSN("localhost:2135", "/local", false, WithUserPassword("user", "")),
DSN("localhost:2135", "/local", WithUserPassword("user", "")),
"grpc://user@localhost:2135/local",
},
{
DSN("localhost:2135", "/local", false, WithUserPassword("user", "password")),
DSN("localhost:2135", "/local", WithUserPassword("user", "password")),
"grpc://user:password@localhost:2135/local",
},
{
DSN("ydb-ru.yandex.net:2135", "/ru/home/gvit/mydb", false),
DSN("ydb-ru.yandex.net:2135", "/ru/home/gvit/mydb"),
"grpc://ydb-ru.yandex.net:2135/ru/home/gvit/mydb",
},
{
DSN("ydb.serverless.yandexcloud.net:2135", "/ru-central1/b1g8skpblkos03malf3s/etn02qso4v3isjb00te1", true),
DSN("ydb.serverless.yandexcloud.net:2135", "/ru-central1/b1g8skpblkos03malf3s/etn02qso4v3isjb00te1", WithSecure(true)), //nolint:lll
"grpcs://ydb.serverless.yandexcloud.net:2135/ru-central1/b1g8skpblkos03malf3s/etn02qso4v3isjb00te1",
},
{
DSN("lb.etn03r9df42nb631unbv.ydb.mdb.yandexcloud.net:2135", "/ru-central1/b1g8skpblkos03malf3s/etn03r9df42nb631unbv", true), //nolint:lll
DSN("lb.etn03r9df42nb631unbv.ydb.mdb.yandexcloud.net:2135", "/ru-central1/b1g8skpblkos03malf3s/etn03r9df42nb631unbv", WithSecure(true)), //nolint:lll
"grpcs://lb.etn03r9df42nb631unbv.ydb.mdb.yandexcloud.net:2135/ru-central1/b1g8skpblkos03malf3s/etn03r9df42nb631unbv",
},
} {
Expand Down

0 comments on commit 4ff82f2

Please sign in to comment.