Skip to content

Commit f17e331

Browse files
committed
* Allowed the use of DSN without specifying the protocol/scheme
1 parent bbb2201 commit f17e331

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Allowed the use of DSN without specifying the protocol/scheme
2+
13
## v3.74.0
24
* Added experimental range functions to the `query.Result` and `query.ResultSet` types, available as for-range loops starting with Go version 1.22. These features can be enabled by setting the environment variable `GOEXPERIMENT=rangefunc`.
35
* Added public types for `tx.Option`, `options.DoOption` and `options.DoTxOption`

internal/dsn/dsn.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ package dsn
33
import (
44
"fmt"
55
"net/url"
6+
"regexp"
67

78
"github.com/ydb-platform/ydb-go-sdk/v3/config"
89
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
910
)
1011

1112
var (
1213
insecureSchema = "grpc"
14+
secureSchema = "grpcs"
15+
reScheme = regexp.MustCompile(`^\w+://`)
1316
databaseParam = "database"
1417
)
1518

@@ -25,7 +28,13 @@ type parsedInfo struct {
2528
}
2629

2730
func Parse(dsn string) (info parsedInfo, err error) {
28-
uri, err := url.Parse(dsn)
31+
uri, err := url.ParseRequestURI(func() string {
32+
if reScheme.MatchString(dsn) {
33+
return dsn
34+
}
35+
36+
return secureSchema + "://" + dsn
37+
}())
2938
if err != nil {
3039
return info, xerrors.WithStackTrace(err)
3140
}

internal/dsn/dsn_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,22 @@ func TestParseConnectionString(t *testing.T) {
104104
"",
105105
"",
106106
},
107+
{
108+
"localhost:3049/Root",
109+
true,
110+
"localhost:3049",
111+
"/Root",
112+
"",
113+
"",
114+
},
115+
{
116+
"grpc://localhost:3049/Root",
117+
false,
118+
"localhost:3049",
119+
"/Root",
120+
"",
121+
"",
122+
},
107123
} {
108124
t.Run(test.connectionString, func(t *testing.T) {
109125
info, err := Parse(test.connectionString)

0 commit comments

Comments
 (0)