Skip to content

Commit 667ca4e

Browse files
committed
feat: add authentication support to connection string
1 parent 637e21a commit 667ca4e

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

utils.go

+16-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package bigquery
22

33
import (
44
"fmt"
5+
"net/url"
56
"strings"
67
)
78

@@ -11,15 +12,23 @@ func ConfigFromConnString(in string) (*Config, error) {
1112
// anything else will fail
1213
cfg := &Config{}
1314
if strings.HasPrefix(in, "bigquery://") {
14-
in = strings.ToLower(in)
15-
path := strings.TrimPrefix(in, "bigquery://")
16-
fields := strings.Split(path, "/")
17-
if len(fields) != 3 {
15+
u, err := url.Parse(in)
16+
if err != nil {
17+
return nil, fmt.Errorf("invalid connection string : %s (%s)", in, err.Error())
18+
}
19+
v, err := url.ParseQuery(u.RawQuery)
20+
if err != nil {
21+
return nil, fmt.Errorf("invalid connection string : %s (%s)", in, err.Error())
22+
}
23+
fields := strings.Split(strings.TrimPrefix(u.Path, "/"), "/")
24+
if len(fields) != 2 {
1825
return nil, fmt.Errorf("invalid connection string : %s", in)
1926
}
20-
cfg.ProjectID = fields[0]
21-
cfg.Location = fields[1]
22-
cfg.DataSet = fields[2]
27+
cfg.ProjectID = u.Host
28+
cfg.Location = fields[0]
29+
cfg.DatasetID = fields[1]
30+
cfg.ApiKey = v.Get("apiKey")
31+
cfg.Credentials = v.Get("credentials")
2332
return cfg, nil
2433
} else {
2534
// Nope, bad prefix

0 commit comments

Comments
 (0)