From 4ff82f250f23caeb0f89ddf8937bdc46f4d490e5 Mon Sep 17 00:00:00 2001 From: Aleksey Myasnikov Date: Sat, 9 Nov 2024 01:25:18 +0300 Subject: [PATCH] * 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)` --- CHANGELOG.md | 7 ++++++- sugar/dsn.go | 16 +++++++++++----- sugar/dsn_test.go | 12 ++++++------ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a062f3591..f4876f5e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +* 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 @@ -5,7 +10,7 @@ * 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 diff --git a/sugar/dsn.go b/sugar/dsn.go index 6c2707d7c..df6572928 100644 --- a/sugar/dsn.go +++ b/sugar/dsn.go @@ -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{ @@ -21,10 +21,6 @@ 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) } @@ -32,6 +28,16 @@ func DSN(endpoint, database string, secure bool, opts ...dsnOption) (s string) { 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) diff --git a/sugar/dsn_test.go b/sugar/dsn_test.go index 889e02b26..644316ab8 100644 --- a/sugar/dsn_test.go +++ b/sugar/dsn_test.go @@ -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", }, } {