-
Notifications
You must be signed in to change notification settings - Fork 4
/
params.go
69 lines (58 loc) · 1.43 KB
/
params.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
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package pine
import (
"strconv"
)
type Params map[string]string
// compile to @see runtime/map.go:mapclear
func (c Params) reset() {
for k := range c {
delete(c, k)
}
}
func (c Params) Set(key, value string) {
c[key] = value
}
func (c Params) Get(key string) string {
return c[key]
}
func (c Params) GetDefault(key, defaultVal string) string {
if val := c.Get(key); len(val) > 0 {
return val
}
return defaultVal
}
func (c Params) GetInt(key string, defaultVal ...int) (val int, err error) {
val, err = strconv.Atoi(c.Get(key))
if err != nil && len(defaultVal) > 0 {
val, err = defaultVal[0], nil
}
return
}
func (c Params) GetInt64(key string, defaultVal ...int64) (val int64, err error) {
val, err = strconv.ParseInt(c.Get(key), 10, 64)
if err != nil && len(defaultVal) > 0 {
val, err = defaultVal[0], nil
}
return
}
func (c Params) GetFloat64(key string, defaultVal ...float64) (val float64, err error) {
val, err = strconv.ParseFloat(c.Get(key), 64)
if err != nil && len(defaultVal) > 0 {
val, err = defaultVal[0], nil
}
return
}
func (c Params) GetBool(key string, defaultVal bool) bool {
val := c.Get(key)
if len(val) == 0 {
return defaultVal
}
if v, err := strconv.ParseBool(val); err != nil {
return defaultVal
} else {
return v
}
}