-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigurationProvider.go
68 lines (59 loc) · 1.57 KB
/
configurationProvider.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
package goconf
import (
"sort"
"strings"
)
var _ Provider = (*ConfigurationProvider)(nil)
type ConfigurationProvider struct {
Data map[string]*ExtractedValue
}
func NewConfigurationProvider() *ConfigurationProvider {
return &ConfigurationProvider{
Data: make(map[string]*ExtractedValue),
}
}
func (provider *ConfigurationProvider) Load() error {
return nil
}
// 尝试获取一个配置key的值
func (provider *ConfigurationProvider) GetString(name string) (string, bool) {
if v, ok := provider.GetExtracted(name); ok {
return v.Value, true
}
return "", false
}
func (provider *ConfigurationProvider) GetExtracted(name string) (*ExtractedValue, bool) {
value, ok := provider.Data[name]
if value == nil {
return nil, false
}
return value, ok
}
// 基于此返回给定父路径的直接后代配置键
func (provider *ConfigurationProvider) GetChildKeys(path string, earlierKeys ...string) []string {
results := make([]string, 0)
if len(path) == 0 {
for key, _ := range provider.Data {
results = append(results, Segment(key, 0))
}
} else {
for key, _ := range provider.Data {
if len(key) > len(path) &&
strings.HasPrefix(key, path) &&
string(key[len(path)]) == KeyDelimiter {
results = append(results, Segment(key, len(path)+1))
}
}
}
results = append(results, earlierKeys...)
sort.Strings(results)
return results
}
func Segment(key string, prefixLength int) string {
indexOf := strings.Index(key[prefixLength:], KeyDelimiter)
if indexOf < 0 {
return key[prefixLength:]
} else {
return key[prefixLength : indexOf-prefixLength]
}
}