/
url.go
61 lines (51 loc) 路 1.27 KB
/
url.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package mongo_backend
import (
"errors"
"fmt"
"strings"
"time"
)
type options struct {
uri string
database string
collectionPrefix string
autoReleaseAfter time.Duration
}
func parseBackendURL(backendURL string) (*options, error) {
components := strings.Split(backendURL, ";")
opts := &options{}
for _, s := range components {
components := strings.SplitN(s, "=", 2)
if len(components) != 2 {
return nil, errors.New("URL format error: options must have format \"key=value\"")
}
key := components[0]
value := components[1]
switch key {
case "uri":
opts.uri = value
case "database":
opts.database = value
case "collectionPrefix":
opts.collectionPrefix = value
case "autoReleaseAfter":
var err error
opts.autoReleaseAfter, err = time.ParseDuration(value)
if err != nil {
return nil, fmt.Errorf("cannot parse duration for autoReleaseAfter: %v", err)
}
default:
return nil, fmt.Errorf("unrecognized option \"%s\"", key)
}
}
if opts.uri == "" {
return nil, errors.New("uri option is mandatory")
}
if opts.database == "" {
return nil, errors.New("database option is mandatory")
}
if opts.collectionPrefix == "" {
return nil, errors.New("collectionPrefix option is mandatory")
}
return opts, nil
}