forked from rotationalio/honu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsn.go
31 lines (26 loc) · 654 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
package honu
import (
"errors"
"fmt"
"net/url"
"strings"
)
// DSN represents the parsed components of an embedded database service.
type DSN struct {
Scheme string
Path string
}
// ParseDSN converts a database URI or URL string into a structured DSN representation.
func ParseDSN(uri string) (_ *DSN, err error) {
dsn, err := url.Parse(uri)
if err != nil {
return nil, fmt.Errorf("could not parse dsn: %s", err)
}
if dsn.Scheme == "" || dsn.Path == "" {
return nil, errors.New("could not parse dsn, specify scheme:https:///relative/path/to/db")
}
return &DSN{
Scheme: dsn.Scheme,
Path: strings.TrimPrefix(dsn.Path, "/"),
}, nil
}