forked from naoina/kocha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
71 lines (60 loc) · 1.32 KB
/
config.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
package kocha
type config map[string]interface{}
var configs = make(map[string]config)
func Config(appName string) config {
if c, ok := configs[appName]; ok {
return c
}
c := make(config)
configs[appName] = c
return c
}
func (c config) Get(name string) (result interface{}, ok bool) {
result, ok = c[name]
return result, ok
}
func (c config) Set(name string, value interface{}) {
c[name] = value
}
func (c config) Int(name string) (result int, ok bool) {
if v, ok := c[name]; ok {
if result, ok := v.(int); ok {
return result, true
}
}
return 0, false
}
func (c config) IntDefault(name string, def int) (result int) {
if result, ok := c.Int(name); ok {
return result
}
return def
}
func (c config) String(name string) (result string, ok bool) {
if v, ok := c[name]; ok {
if result, ok := v.(string); ok {
return result, true
}
}
return "", false
}
func (c config) StringDefault(name string, def string) (result string) {
if result, ok := c.String(name); ok {
return result
}
return def
}
func (c config) Bool(name string) (result bool, ok bool) {
if v, ok := c[name]; ok {
if result, ok := v.(bool); ok {
return result, true
}
}
return false, false
}
func (c config) BoolDefault(name string, def bool) (result bool) {
if result, ok := c.Bool(name); ok {
return result
}
return def
}