forked from xgfone/gconf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.go
251 lines (227 loc) · 7.21 KB
/
decoder.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright 2019 xgfone
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gconf
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"unicode"
"github.com/BurntSushi/toml"
"gopkg.in/yaml.v2"
)
// Decoder is used to decode the configuration data.
type Decoder struct {
// Type is the type of decoder, such as "json", "xml", which is case insensitive.
Type string
// Decode is used to decode the configuration data.
//
// The decoder maybe decode the src data to the formats as follow:
//
// map[string]interface{} {
// "opt1": "value1",
// "opt2": "value2",
// // ...
// "group1": map[string]interface{} {
// "opt11": "value11",
// "opt12": "value12",
// "group2": map[string]interface{} {
// // ...
// },
// "group3.group4": map[string]interface{} {
// // ...
// }
// },
// "group5.group6.group7": map[string]interface{} {
// "opt71": "value71",
// "opt72": "value72",
// "group8": map[string]interface{} {
// // ...
// },
// "group9.group10": map[string]interface{} {
// // ...
// }
// },
// "group11.group12.opt121": "value121",
// "group11.group12.opt122": "value122"
// }
//
// Notice: The option name must not contain the dot(.).
Decode func(src []byte, dst map[string]interface{}) error
}
// AddDecoder adds a decoder and returns true.
//
// If the decoder has added, it will do nothing and return false.
// But you can override it by setting force to true.
func (c *Config) AddDecoder(decoder Decoder, force ...bool) (ok bool) {
_type := strings.ToLower(decoder.Type)
c.lock.Lock()
if _, ok = c.decoders[_type]; !ok {
c.decoders[_type] = decoder
ok = true
}
c.lock.Unlock()
return
}
// GetDecoder returns the decoder by the type.
func (c *Config) GetDecoder(_type string) (decoder Decoder, ok bool) {
_type = strings.ToLower(_type)
c.lock.RLock()
if decoder, ok = c.decoders[_type]; !ok {
if alias, _ok := c.decAlias[_type]; _ok {
decoder, ok = c.decoders[alias]
}
}
c.lock.RUnlock()
return
}
// AddDecoderAlias adds the alias of the decoder typed _type. For example,
//
// c.AddDecoderAlias("conf", "ini")
//
// When you get the "conf" decoder and it does not exist, it will try to
// return the "ini" decoder.
//
// If the alias has existed, it will override it.
func (c *Config) AddDecoderAlias(_type, alias string) {
_type = strings.ToLower(_type)
alias = strings.ToLower(alias)
c.lock.Lock()
c.decAlias[_type] = alias
c.lock.Unlock()
}
// NewDecoder returns a new decoder.
func NewDecoder(_type string, decode func([]byte, map[string]interface{}) error) Decoder {
return Decoder{Type: _type, Decode: decode}
}
// NewJSONDecoder returns a json decoder to decode the json data.
//
// If the json data contains the comment line starting with "//", it will remove
// the comment line and parse the json data.
func NewJSONDecoder() Decoder {
comment := []byte("//")
newline := []byte("\n")
return NewDecoder("json", func(src []byte, dst map[string]interface{}) (err error) {
if bytes.Contains(src, comment) {
buf := bytes.NewBuffer(nil)
buf.Grow(len(src))
for _, line := range bytes.Split(src, newline) {
if line = bytes.TrimSpace(line); len(line) == 0 {
buf.WriteByte('\n')
continue
} else if len(line) > 1 && line[0] == '/' && line[1] == '/' {
continue
}
buf.Write(line)
buf.WriteByte('\n')
}
src = buf.Bytes()
}
return json.Unmarshal(src, &dst)
})
}
// NewYamlDecoder returns a yaml decoder to decode the yaml data.
func NewYamlDecoder() Decoder {
return NewDecoder("yaml", func(src []byte, dst map[string]interface{}) (err error) {
return yaml.Unmarshal([]byte(src), &dst)
})
}
// NewTomlDecoder returns a toml decoder to decode the toml data.
func NewTomlDecoder() Decoder {
return NewDecoder("toml", func(src []byte, dst map[string]interface{}) (err error) {
return toml.Unmarshal([]byte(src), &dst)
})
}
// NewIniDecoder returns a INI decoder to decode the INI data.
//
// Notice:
// 1. The empty line will be ignored.
// 2. The spacewhite on the beginning and end of line or value will be trimmed.
// 3. The comment line starts with the character '#' or ';', which is ignored.
// 4. The name of the default group is "DEFAULT", but it is optional.
// 5. The group can nest other groups by ".", such as "group1.group2.group3".
// 6. The key must only contain the printable non-spacewhite characters.
// 7. The line can continue to the next line with the last character "\",
// and the spacewhite on the beginning and end of the each line will be
// trimmed, then combines them with a space.
//
func NewIniDecoder(defaultGroupName ...string) Decoder {
defaultGroup := "DEFAULT"
if len(defaultGroupName) > 0 && defaultGroupName[0] != "" {
defaultGroup = defaultGroupName[0]
}
return NewDecoder("ini", func(src []byte, dst map[string]interface{}) (err error) {
var gname string
lines := strings.Split(string(src), "\n")
for index, maxIndex := 0, len(lines); index < maxIndex; {
line := strings.TrimSpace(lines[index])
index++
// Ignore the empty line and the comment line
if len(line) == 0 || line[0] == '#' || line[0] == ';' {
continue
}
// Start a new group
if last := len(line) - 1; line[0] == '[' && line[last] == ']' {
gname = strings.TrimSpace(line[1:last])
if gname == defaultGroup {
gname = ""
}
continue
}
n := strings.IndexByte(line, '=')
if n < 0 {
return fmt.Errorf("the %dth line misses the separator '='", index)
}
// Get the key
key := strings.TrimSpace(line[:n])
if len(key) == 0 {
return fmt.Errorf("empty identifier key")
}
for _, r := range key {
if unicode.IsSpace(r) || !unicode.IsPrint(r) {
return fmt.Errorf("invalid identifier key '%s'", key)
}
}
// Get the value
value := strings.TrimSpace(line[n+1:])
if value == "" { // Ignore the empty value
continue
} else if _len := len(value) - 1; value[_len] == '\\' { // The continuation line
vs := []string{strings.TrimSpace(strings.TrimRight(value, "\\"))}
for index < maxIndex {
value = strings.TrimSpace(lines[index])
var goon bool
if _len := len(value) - 1; value[_len] == '\\' {
goon = true
}
if value = strings.TrimSpace(strings.TrimRight(value, "\\")); value == "" {
break
}
index++
vs = append(vs, value)
if !goon {
break
}
}
value = strings.Join(vs, " ")
}
// Add the option
if gname != "" {
key = strings.Join([]string{gname, key}, ".")
}
dst[key] = value
}
return
})
}