-
Notifications
You must be signed in to change notification settings - Fork 1
/
decoder.go
184 lines (165 loc) · 5.2 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
// Copyright 2021 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"
)
// Decoder is used to decode the configuration data.
type Decoder func(src []byte, dst map[string]interface{}) error
// AddDecoder is equal to Conf.AddDecoder(_type, decoder).
func AddDecoder(_type string, decoder Decoder) {
Conf.AddDecoder(_type, decoder)
}
// AddDecoder adds a decoder, which will override it if it has been added.s
func (c *Config) AddDecoder(_type string, decoder Decoder) {
c.decoders[strings.ToLower(_type)] = decoder
}
// GetDecoder returns the decoder by the type.
//
// Return nil if the decoder does not exist.
func (c *Config) GetDecoder(_type string) (decoder Decoder) {
_type = strings.ToLower(_type)
decoder, ok := c.decoders[_type]
if !ok {
if alias, ok := c.daliases[_type]; ok {
decoder = c.decoders[alias]
}
}
return
}
// AddDecoderTypeAliases adds the aliases of the decoder typed _type.
//
// For example,
//
// c.AddDecoderTypeAliases("yaml", "yml")
//
// When acquiring the "yml" decoder and it does not exist, it will try to
// return the "yaml" decoder.
func (c *Config) AddDecoderTypeAliases(_type string, aliases ...string) {
_type = strings.ToLower(_type)
for _, alias := range aliases {
c.daliases[strings.ToLower(alias)] = _type
}
}
// 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 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)
}
}
// 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 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
}
}