-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.go
36 lines (33 loc) · 945 Bytes
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package bigquery
import (
"fmt"
"net/url"
"strings"
)
// ConfigFromConnString will return the Config structures
func ConfigFromConnString(in string) (*Config, error) {
cfg := &Config{}
if strings.HasPrefix(in, "bigquery://") {
u, err := url.Parse(in)
if err != nil {
return nil, fmt.Errorf("invalid connection string: %s (%s)", in, err.Error())
}
v, err := url.ParseQuery(u.RawQuery)
if err != nil {
return nil, fmt.Errorf("invalid connection string: %s (%s)", in, err.Error())
}
fields := strings.Split(strings.TrimPrefix(u.Path, "/"), "/")
if len(fields) != 2 {
return nil, fmt.Errorf("invalid connection string: %s", in)
}
cfg.ProjectID = u.Host
cfg.Location = fields[0]
cfg.DatasetID = fields[1]
cfg.ApiKey = v.Get("apiKey")
cfg.Credentials = v.Get("credentials")
return cfg, nil
} else {
// Nope, bad prefix
return nil, fmt.Errorf("invalid prefix, expected bigquery:// got: %s", in)
}
}