/
json.go
63 lines (53 loc) · 1.15 KB
/
json.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
package cfgur
import (
"github.com/tidwall/gjson"
"os"
)
type jsonCfg struct {
filepath string
val gjson.Result
}
func (cfg *jsonCfg) Init() error {
bytes, err := os.ReadFile(cfg.filepath)
if err != nil {
return err
}
cfg.val = gjson.ParseBytes(bytes)
return nil
}
func NewJsonMarshaller(filepath string) ConfigUnmarshaler {
return &commonMarshaller{ConfigProvider: &jsonCfg{filepath: filepath}}
}
func (cfg *jsonCfg) GetStringE(key string) (string, bool) {
result := cfg.val.Get(key)
if result.Exists() {
return result.String(), true
}
return "", false
}
func (cfg *jsonCfg) GetIntE(key string) (int64, bool) {
result := cfg.val.Get(key)
if result.Exists() {
return result.Int(), true
}
return 0, false
}
func (cfg *jsonCfg) GetBoolE(key string) (bool, bool) {
result := cfg.val.Get(key)
if result.Exists() {
return result.Bool(), true
}
return false, false
}
func (cfg *jsonCfg) GetString(key string) string {
val, _ := cfg.GetStringE(key)
return val
}
func (cfg *jsonCfg) GetInt(key string) int64 {
val, _ := cfg.GetIntE(key)
return val
}
func (cfg *jsonCfg) GetBool(key string) bool {
val, _ := cfg.GetBoolE(key)
return val
}