/
interfaces.go
125 lines (110 loc) · 2.9 KB
/
interfaces.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
package goconf
import (
"fmt"
"reflect"
)
type Configuration interface {
GetString(name string) (string, bool)
GetExtracted(name string) (*ExtractedValue, bool)
GetSection(name string) Section
GetChildren() []Section
}
type Section interface {
Configuration
GetName() string
GetPath() string
GetValue() (string, bool)
GetRaw() (interface{}, bool)
Bind(recv interface{}) error
}
type Provider interface {
Load() error
// 尝试获取一个配置key的值
GetString(name string) (string, bool)
GetExtracted(name string) (*ExtractedValue, bool)
// 基于此返回给定父路径的直接后代配置键
GetChildKeys(path string, earlierKeys ...string) []string
}
type Builder interface {
Properties() map[string]interface{}
Sources() []Source
AddSource(source Source) Builder
BuildRoot() Root
}
type Source interface {
BuildProvider(builder Builder) Provider
}
type Root interface {
Configuration
Reload() error
GetProviders() []Provider
}
type ExtractedValue struct {
Name string
Value string
Raw interface{}
}
func NewExtractedValue(name string, value string, raw interface{}) *ExtractedValue {
return &ExtractedValue{
Name: name,
Value: value,
Raw: raw,
}
}
func ExtractStructToMap(data interface{}, path string) map[string]*ExtractedValue {
ret := make(map[string]*ExtractedValue)
valueType := reflect.TypeOf(data)
if valueType.Kind() == reflect.Map {
ret[path] = NewExtractedValue(path, "", data)
subMap := ExtractMap(data, path)
for subKey, subValue := range subMap {
ret[subKey] = subValue
}
} else if valueType.Kind() == reflect.Slice {
ret[path] = NewExtractedValue(path, "", data)
sliceValue := reflect.ValueOf(data)
for i := 0; i < sliceValue.Len(); i++ {
structValue := sliceValue.Index(i)
sliceKey := CombinePath(path, fmt.Sprintf("$%d", i))
subMap := ExtractStructToMap(structValue.Interface(), sliceKey)
for subKey, subValue := range subMap {
ret[subKey] = subValue
}
}
} else if valueType.Kind() == reflect.Ptr {
ptrValue := reflect.ValueOf(data)
if ptrValue.IsNil() {
return nil
}
subMap := ExtractStructToMap(ptrValue.Elem().Interface(), path)
for subKey, subValue := range subMap {
ret[subKey] = subValue
}
} else {
ret[path] = NewExtractedValue(path, fmt.Sprintf("%v", data), data)
}
return ret
}
func ExtractMap(data interface{}, path string) map[string]*ExtractedValue {
ret := make(map[string]*ExtractedValue)
mapRange := reflect.ValueOf(data).MapRange()
for mapRange.Next() {
var subPath string
key := mapRange.Key()
v := mapRange.Value()
if k, ok := key.Interface().(string); ok {
subPath = CombinePath(path, k)
} else {
subPath = CombinePath(path, fmt.Sprintf("%v", key.Interface()))
}
subMap := ExtractStructToMap(v.Interface(), subPath)
if subMap == nil || len(subMap) == 0 {
continue
}
for subKey, subValue := range subMap {
ret[subKey] = subValue
}
continue
}
return ret
}