-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
131 lines (119 loc) · 2.56 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package configures
import (
"errors"
"fmt"
"github.com/aacfactory/json"
"github.com/goccy/go-yaml"
"github.com/tidwall/gjson"
"reflect"
)
func NewJsonConfig(raw []byte) (v Config, err error) {
if !json.Validate(raw) {
err = fmt.Errorf("new json config failed, content is invalid")
return
}
v = &config{
raw: raw,
}
return
}
func NewYamlConfig(raw []byte) (v Config, err error) {
p, mapErr := yaml.YAMLToJSON(raw)
if mapErr != nil {
err = fmt.Errorf("new yaml config failed, content is invalid")
return
}
v = &config{
raw: p,
}
return
}
type config struct {
raw []byte
}
func (c *config) Raw() []byte {
return c.raw
}
func (c *config) As(v interface{}) (err error) {
switch v.(type) {
case *Raw:
p := v.(*Raw)
*p = append((*p)[0:0], c.raw...)
case *json.RawMessage:
p := v.(*json.RawMessage)
*p = append((*p)[0:0], c.raw...)
case *[]byte:
p := v.(*[]byte)
*p = append((*p)[0:0], c.raw...)
default:
decodeErr := json.Unmarshal(c.raw, v)
if decodeErr != nil {
err = fmt.Errorf("decode config as %v failed", reflect.TypeOf(v))
return
}
}
return
}
func (c *config) Get(path string, v interface{}) (has bool, err error) {
result := gjson.GetBytes(c.raw, path)
if !result.Exists() {
return
}
switch v.(type) {
case *Raw:
p := v.(*Raw)
*p = append((*p)[0:0], []byte(result.Raw)...)
case *json.RawMessage:
p := v.(*json.RawMessage)
*p = append((*p)[0:0], []byte(result.Raw)...)
case *[]byte:
p := v.(*[]byte)
*p = append((*p)[0:0], []byte(result.Raw)...)
default:
decodeErr := json.Unmarshal([]byte(result.Raw), v)
if decodeErr != nil {
err = fmt.Errorf("config get %s failed for decoding failed", path)
return
}
}
has = true
return
}
func (c *config) Node(path string) (v Config, has bool) {
p := make([]byte, 0, 1)
has, _ = c.Get(path, &p)
if !has {
return
}
v = &config{
raw: p,
}
return
}
type Raw json.RawMessage
func (r Raw) As(v interface{}) (err error) {
decodeErr := json.Unmarshal(r, v)
if decodeErr != nil {
err = fmt.Errorf("raw config as %s failed", reflect.TypeOf(v).String())
}
return
}
func (r Raw) MarshalJSON() ([]byte, error) {
if r == nil {
return []byte("null"), nil
}
if !json.Validate(r) {
return nil, errors.New("configuares.Raw: MarshalJSON on invalid message")
}
return r, nil
}
func (r *Raw) UnmarshalJSON(data []byte) error {
if r == nil {
return errors.New("configuares.Raw: UnmarshalJSON on nil pointer")
}
if !json.Validate(data) {
return errors.New("configuares.Raw: UnmarshalJSON on invalid message")
}
*r = append((*r)[0:0], data...)
return nil
}