forked from dtylman/settings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.go
110 lines (98 loc) · 2.15 KB
/
settings.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package settings
import (
"fmt"
"strings"
)
//FileFormat configuration file format
type FileFormat int
const (
//FormatJSON ...
FormatJSON = iota + 1
)
//View represents a settings view
type View map[string]interface{}
//New creates a new empty setting view
func New() View {
return make(View)
}
//Load loads a file and populate into a view
func (v View) Load(fileName string, format FileFormat) error {
switch format {
case FormatJSON:
return v.loadJSON(fileName)
}
return fmt.Errorf("Format not supported: %v", format)
}
//Print prints the view in the format provided
func (v View) Print(format FileFormat) error {
switch format {
case FormatJSON:
return v.printJSON()
}
return fmt.Errorf("Format not supported: %v", format)
}
//Save saves the view to a file
func (v View) Save(fileName string, format FileFormat) error {
switch format {
case FormatJSON:
return v.saveJSON(fileName)
}
return fmt.Errorf("Format not supported: %v", format)
}
//Set sets a configuration key
func (v View) Set(key string, value interface{}) {
subkeys := strings.SplitN(key, ".", 2)
if len(subkeys) == 1 {
v[key] = value
} else {
name := subkeys[0]
view, ok := v[name].(View)
if !ok {
view, ok = v[name].(map[string]interface{})
if !ok {
view = make(View)
}
}
view.Set(subkeys[1], value)
v[name] = view
}
}
//Get gets configuration key
func (v View) Get(key string, def interface{}) interface{} {
subkeys := strings.SplitN(key, ".", 2)
val, ok := v[subkeys[0]]
if ok {
view, ok := val.(View)
if ok {
return view.Get(subkeys[1], def)
}
m, ok := val.(map[string]interface{})
if ok {
return View(m).Get(subkeys[1], def)
}
return val
}
return def
}
//Has returns true if view has the key
func (v View) Has(key string) bool {
subkeys := strings.SplitN(key, ".", 2)
val, ok := v[subkeys[0]]
if ok {
view, ok := val.(map[string]interface{})
if ok {
return View(view).Has(subkeys[1])
}
return true
}
return false
}
//GetString gets a configuration item as a string
func (v View) GetString(key string, def string) string {
val := v.Get(key, def)
s, ok := val.(string)
if ok {
return s
}
return fmt.Sprintf("%v", val)
}