/
config.go
52 lines (48 loc) · 1001 Bytes
/
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
package main
import (
"encoding/json"
"log"
"strings"
"os"
)
type Config struct {
path string
config map[string]interface{}
}
func NewConfig(path string) *Config {
c := Config{
path: path,
config: make(map[string]interface{})}
file, err := os.Open(path)
if err != nil {
log.Printf("Config Open Error (%s): %s", path, err.Error())
return nil
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(&c.config)
if err != nil {
log.Printf("Config Decode Error (%s): %s", path, err.Error())
}
return &c
}
func (c *Config) Get(fullname string) (string, bool) {
var m interface{}
m = c.config
nameSlice := strings.Split(fullname, ".")
for _, name := range nameSlice {
_, isMap := m.(map[string]interface{})
if !isMap {
return "", false
}
value, present := m.(map[string]interface{})[name]
if !present {
return "", false
}
m, isMap = value.(map[string]interface{})
if !isMap {
m = value.(string)
}
}
return m.(string), true
}