This repository was archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig_kv.go
More file actions
132 lines (119 loc) · 2.88 KB
/
Copy pathconfig_kv.go
File metadata and controls
132 lines (119 loc) · 2.88 KB
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
/*
Copyright © 2017 Henry Huang <hhh@rutcode.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package config
import (
"os"
"reflect"
"strings"
"github.com/iTrellis/common/formats"
)
func (p *AdapterConfig) copyDollarSymbol(key string, maps *map[string]interface{}) error {
tokens := []string{}
if key != "" {
tokens = append(tokens, key)
}
for k, v := range *maps {
if v == nil {
return nil
}
keys := append(tokens, k)
switch reflect.TypeOf(v).Kind() {
case reflect.Map:
{
vm, ok := v.(map[string]interface{})
if !ok {
continue
}
err := p.copyDollarSymbol(strings.Join(keys, "."), &vm)
if err != nil {
return err
}
}
case reflect.String:
{
s, ok := v.(string)
if !ok {
continue
}
_, matched := formats.FindStringSubmatchMap(s, includeReg)
if !matched {
continue
}
pkey := s[2 : len(s)-1]
if p.EnvAllowed && (p.EnvPrefix == "" || strings.HasPrefix(pkey, p.EnvPrefix)) {
if env := os.Getenv(pkey); env != "" {
if err := p.setKeyValue(strings.Join(keys, "."), env); err != nil {
return err
}
continue
}
}
vm, err := p.getKeyValue(pkey)
if err != nil {
return err
}
err = p.setKeyValue(strings.Join(keys, "."), vm)
if err != nil {
return err
}
}
}
}
return nil
}
func (p *AdapterConfig) getKeyValue(key string) (interface{}, error) {
tokens := strings.Split(key, ".")
vm := p.configs[tokens[0]]
for i, t := range tokens {
if i == 0 {
continue
}
switch v := vm.(type) {
case Options:
vm = v[t]
case map[string]interface{}:
vm = v[t]
case map[interface{}]interface{}:
vm = v[t]
default:
return nil, ErrNotMap
}
}
return vm, nil
}
// setKeyValue set key value into *configs
func (p *AdapterConfig) setKeyValue(key string, value interface{}) (err error) {
tokens := strings.Split(key, ".")
for i := len(tokens) - 1; i >= 0; i-- {
if i == 0 {
p.configs[tokens[0]] = value
return
}
v, _ := p.getKeyValue(strings.Join(tokens[:i], "."))
switch vm := v.(type) {
case Options:
vm[tokens[i]] = value
value = vm
case map[string]interface{}:
vm[tokens[i]] = value
value = vm
case map[interface{}]interface{}:
vm[tokens[i]] = value
value = vm
default:
value = map[string]interface{}{tokens[i]: value}
}
}
return
}