-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdsn.go
88 lines (75 loc) · 1.5 KB
/
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
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package zapwriter
import (
"fmt"
"net/url"
"strconv"
"strings"
"time"
)
type DsnObj struct {
url.Values
}
func DSN(values url.Values) *DsnObj {
return &DsnObj{values}
}
func (dsn *DsnObj) Duration(key string, initial string) (time.Duration, error) {
s := dsn.Get(key)
if s == "" {
return time.ParseDuration(initial)
}
return time.ParseDuration(s)
}
func (dsn *DsnObj) Int(key string, initial int) (int, error) {
s := dsn.Get(key)
if s == "" {
return initial, nil
}
return strconv.Atoi(s)
}
func (dsn *DsnObj) Bool(key string, initial bool) (bool, error) {
s := dsn.Get(key)
if s == "" {
return initial, nil
}
return map[string]bool{
"true": true,
"1": true,
}[strings.ToLower(s)], nil
}
func (dsn *DsnObj) String(key string, initial string) (string, error) {
s := dsn.Get(key)
if s == "" {
return initial, nil
}
return s, nil
}
func (dsn *DsnObj) StringRequired(key string) (string, error) {
s := dsn.Get(key)
if s == "" {
return "", fmt.Errorf("%#v is required", key)
}
return s, nil
}
func (dsn *DsnObj) SetInt(p *int, key string) error {
var err error
*p, err = dsn.Int(key, *p)
return err
}
func (dsn *DsnObj) SetDuration(p *time.Duration, key string) error {
var err error
*p, err = dsn.Duration(key, p.String())
return err
}
func (dsn *DsnObj) SetString(p *string, key string) error {
var err error
*p, err = dsn.String(key, *p)
return err
}
func AnyError(e ...error) error {
for i := 0; i < len(e); i++ {
if e[i] != nil {
return e[i]
}
}
return nil
}