/
conf.go
370 lines (330 loc) · 9.25 KB
/
conf.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* Copyright 2012-2019 the original author or authors.
*
* 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
*
* https://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 conf reads configuration from many format file, such as Java
// properties, yaml, toml, etc.
package conf
import (
"errors"
"fmt"
"io"
"io/ioutil"
"path/filepath"
"reflect"
"sort"
"strings"
"time"
"github.com/go-spring/spring-base/cast"
"github.com/go-spring/spring-base/util"
"github.com/go-spring/spring-core/conf/internal"
"github.com/go-spring/spring-core/conf/prop"
"github.com/go-spring/spring-core/conf/toml"
"github.com/go-spring/spring-core/conf/yaml"
)
// Splitter splits string into []string by some characters.
type Splitter func(string) ([]string, error)
// Reader parses []byte into nested map[string]interface{}.
type Reader func(b []byte) (map[string]interface{}, error)
var (
readers = map[string]Reader{}
splitters = map[string]Splitter{}
converters = map[reflect.Type]util.Converter{}
)
func init() {
RegisterReader(prop.Read, ".properties")
RegisterReader(yaml.Read, ".yaml", ".yml")
RegisterReader(toml.Read, ".toml", ".tml")
// converts string into time.Time. The string value may have its own
// time format defined after >> splitter, otherwise it uses a default
// time format `2006-01-02 15:04:05 -0700`.
RegisterConverter(func(s string) (time.Time, error) {
s = strings.TrimSpace(s)
format := "2006-01-02 15:04:05 -0700"
if ss := strings.Split(s, ">>"); len(ss) == 2 {
format = strings.TrimSpace(ss[1])
s = strings.TrimSpace(ss[0])
}
return cast.ToTimeE(s, format)
})
// converts string into time.Duration. The string should have its own
// time unit such as "ns", "ms", "s", "m", etc.
RegisterConverter(func(s string) (time.Duration, error) {
return cast.ToDurationE(s)
})
}
// RegisterReader registers its Reader for some kind of file extension.
func RegisterReader(r Reader, ext ...string) {
for _, s := range ext {
readers[s] = r
}
}
// RegisterSplitter registers a Splitter and named it.
func RegisterSplitter(name string, fn Splitter) {
splitters[name] = fn
}
// RegisterConverter registers its converter for non-primitive type such as
// time.Time, time.Duration, or other user-defined value type.
func RegisterConverter(fn util.Converter) {
t := reflect.TypeOf(fn)
if !util.IsConverter(t) {
panic(errors.New("converter should be func(string)(type,error)"))
}
converters[t.Out(0)] = fn
}
// Properties stores the data with map[string]string and the keys are case-sensitive,
// you can get one of them by its key, or bind some of them to a value.
// There are too many formats of configuration files, and too many conflicts between
// them. Each format of configuration file provides its special characteristics, but
// usually they are not all necessary, and complementary. For example, `conf` disabled
// Java properties' expansion when reading file, but also provides similar function
// when getting or binding properties.
// A good rule of thumb is that treating application configuration as a tree, but not
// all formats of configuration files designed as a tree or not ideal, for instance
// Java properties isn't strictly verified. Although configuration can store as a tree,
// but it costs more CPU time when getting properties because it reads property node
// by node. So `conf` uses a tree to strictly verify and a flat map to store.
type Properties struct {
storage *internal.Storage
}
// New creates empty *Properties.
func New() *Properties {
return &Properties{
storage: internal.NewStorage(),
}
}
// Map creates *Properties from map.
func Map(m map[string]interface{}) (*Properties, error) {
p := New()
var keys []string
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
if err := p.Set(k, m[k]); err != nil {
return nil, err
}
}
return p, nil
}
// Load creates *Properties from file.
func Load(file string) (*Properties, error) {
p := New()
if err := p.Load(file); err != nil {
return nil, err
}
return p, nil
}
// Load loads properties from file.
func (p *Properties) Load(file string) error {
b, err := ioutil.ReadFile(file)
if err != nil {
return err
}
return p.Bytes(b, filepath.Ext(file))
}
// Read creates *Properties from io.Reader, ext is the file name extension.
func Read(r io.Reader, ext string) (*Properties, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
return Bytes(b, ext)
}
// Bytes creates *Properties from []byte, ext is the file name extension.
func Bytes(b []byte, ext string) (*Properties, error) {
p := New()
if err := p.Bytes(b, ext); err != nil {
return nil, err
}
return p, nil
}
// Bytes loads properties from []byte, ext is the file name extension.
func (p *Properties) Bytes(b []byte, ext string) error {
r, ok := readers[ext]
if !ok {
return fmt.Errorf("unsupported file type %s", ext)
}
m, err := r(b)
if err != nil {
return err
}
var keys []string
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
if err = p.Set(k, m[k]); err != nil {
return err
}
}
return nil
}
func (p *Properties) Copy() *Properties {
return &Properties{
storage: p.storage.Copy(),
}
}
// Keys returns all sorted keys.
func (p *Properties) Keys() []string {
return p.storage.Keys()
}
// Has returns whether key exists.
func (p *Properties) Has(key string) bool {
return p.storage.Has(key)
}
type getArg struct {
def string
}
type GetOption func(arg *getArg)
// Def returns v when key not exits.
func Def(v string) GetOption {
return func(arg *getArg) {
arg.def = v
}
}
// Get returns key's value, using Def to return a default value.
func (p *Properties) Get(key string, opts ...GetOption) string {
val := p.storage.Get(key)
if val != "" {
return val
}
arg := getArg{}
for _, opt := range opts {
opt(&arg)
}
return arg.def
}
func Flatten(key string, val interface{}, result map[string]string) error {
switch v := reflect.ValueOf(val); v.Kind() {
case reflect.Map:
if v.Len() == 0 {
result[key] = ""
return nil
}
for _, k := range v.MapKeys() {
mapKey := cast.ToString(k.Interface())
mapValue := v.MapIndex(k).Interface()
err := Flatten(key+"."+mapKey, mapValue, result)
if err != nil {
return err
}
}
case reflect.Array, reflect.Slice:
if v.Len() == 0 {
result[key] = ""
return nil
}
for i := 0; i < v.Len(); i++ {
subKey := fmt.Sprintf("%s[%d]", key, i)
subValue := v.Index(i).Interface()
err := Flatten(subKey, subValue, result)
if err != nil {
return err
}
}
default:
result[key] = cast.ToString(val)
}
return nil
}
// Set sets key's value to be a primitive type as int or string,
// or a slice or map nested with primitive type elements. One thing
// you should know is Set actions as overlap but not replace, that
// means when you set a slice or a map, an existing path will remain
// when it doesn't exist in the slice or map even they share a same
// prefix path.
func (p *Properties) Set(key string, val interface{}) error {
if key == "" {
return nil
}
m := make(map[string]string)
err := Flatten(key, val, m)
if err != nil {
return err
}
var keys []string
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
err = p.storage.Set(k, m[k])
if err != nil {
return err
}
}
return nil
}
// Resolve resolves string value that contains references to other
// properties, the references are defined by ${key:=def}.
func (p *Properties) Resolve(s string) (string, error) {
return resolveString(p, s)
}
type bindArg struct {
tag string
}
type BindOption func(arg *bindArg)
// Key binds properties using one key.
func Key(key string) BindOption {
return func(arg *bindArg) {
arg.tag = "${" + key + "}"
}
}
// Tag binds properties using one tag.
func Tag(tag string) BindOption {
return func(arg *bindArg) {
arg.tag = tag
}
}
// Bind binds properties to a value, the bind value can be primitive type,
// map, slice, struct. When binding to struct, the tag 'value' indicates
// which properties should be bind. The 'value' tags are defined by
// value:"${a:=b|splitter}", 'a' is the key, 'b' is the default value,
// 'splitter' is the Splitter's name when you want split string value
// into []string value.
func (p *Properties) Bind(i interface{}, opts ...BindOption) error {
var v reflect.Value
{
switch e := i.(type) {
case reflect.Value:
v = e
default:
v = reflect.ValueOf(i)
if v.Kind() != reflect.Ptr {
return errors.New("i should be a ptr")
}
v = v.Elem()
}
}
arg := bindArg{tag: "${ROOT}"}
for _, opt := range opts {
opt(&arg)
}
t := v.Type()
typeName := t.Name()
if typeName == "" { // primitive type has no name
typeName = t.String()
}
param := BindParam{
Path: typeName,
}
err := param.BindTag(arg.tag, "")
if err != nil {
return err
}
return BindValue(p, v, t, param, nil)
}