forked from sql-machine-learning/gohive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsn.go
36 lines (31 loc) · 883 Bytes
/
dsn.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 gohive
import (
"fmt"
"regexp"
)
type config struct {
User string
Passwd string
Addr string
}
var (
// Regexp syntax: https://github.com/google/re2/wiki/Syntax
reDSN = regexp.MustCompile(`(.+@)?([^@/]+)`)
reUserPasswd = regexp.MustCompile(`([^:@]+)(:[^:@]+)?@`)
)
// parseDSN requires DSN names in the format [user[:password]@]addr/dbname.
func parseDSN(dsn string) (*config, error) {
// Please read https://play.golang.org/p/_CSLvl1AxOX before code review.
sub := reDSN.FindStringSubmatch(dsn)
if len(sub) != 3 {
return nil, fmt.Errorf("The DSN %s doesn't match [user[:password]@]addr", dsn)
}
up := reUserPasswd.FindStringSubmatch(sub[1])
if len(up) == 3 {
if len(up[2]) > 0 {
return &config{User: up[1], Passwd: up[2][1:], Addr: sub[2]}, nil
}
return &config{User: up[1], Addr: sub[2]}, nil
}
return &config{Addr: sub[2]}, nil
}