|
1 | 1 | package config |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "errors" |
5 | 4 | "os" |
6 | 5 | "path/filepath" |
7 | | - "strconv" |
8 | 6 | "strings" |
9 | 7 | "sync" |
10 | 8 |
|
@@ -139,63 +137,53 @@ func normalizeKeys(config *koanf.Koanf) { |
139 | 137 | // Transforms Children of path |
140 | 138 | func transformChildren(config *koanf.Koanf, path string, transform func(key string, value any) (string, any)) error { |
141 | 139 | var sub map[string]any |
142 | | - |
143 | 140 | if err := config.Unmarshal(path, &sub); err != nil { |
144 | 141 | return err |
145 | 142 | } |
146 | 143 |
|
147 | 144 | transformed := make(map[string]any) |
148 | | - |
149 | 145 | for key, val := range sub { |
150 | 146 | newKey, newVal := transform(key, val) |
151 | 147 |
|
152 | 148 | transformed[newKey] = newVal |
153 | 149 | } |
| 150 | + |
| 151 | + config.Load(confmap.Provider(map[string]any{ |
| 152 | + path: map[string]any{}, |
| 153 | + }, "."), nil) |
154 | 154 |
|
155 | | - parts := strings.Split(path, ".") |
156 | | - if len(parts) < 1 { |
157 | | - return errors.New("invalid path: " + path) |
158 | | - } |
159 | | - |
160 | | - parentPath := strings.Join(parts[:len(parts)-1], ".") |
161 | | - lastKey := parts[len(parts)-1] |
162 | | - |
163 | | - var parent map[string]any |
164 | | - |
165 | | - if err := config.Unmarshal(parentPath, &parent); err != nil { |
166 | | - return err |
167 | | - } |
168 | | - |
169 | | - parent[lastKey] = transformed |
170 | | - |
171 | | - return config.Load(confmap.Provider(map[string]any{ |
172 | | - parentPath: parent, |
| 155 | + config.Load(confmap.Provider(map[string]any{ |
| 156 | + path: transformed, |
173 | 157 | }, "."), nil) |
174 | | -} |
175 | 158 |
|
| 159 | + return nil |
| 160 | +} |
176 | 161 |
|
177 | 162 | // Does the same thing as transformChildren() but does it for each Array Item inside of root and transforms subPath |
178 | 163 | func transformChildrenUnderArray(config *koanf.Koanf, root string, subPath string, transform func(key string, value any) (string, any)) error { |
179 | | - var items []map[string]any |
180 | | - |
181 | | - err := config.Unmarshal(root, &items) |
182 | | - |
183 | | - if err != nil { |
| 164 | + var array []map[string]any |
| 165 | + if err := config.Unmarshal(root, &array); err != nil { |
184 | 166 | return err |
185 | 167 | } |
186 | 168 |
|
187 | | - for i := range items { |
188 | | - p := root + "." + strconv.Itoa(i) + "." + subPath |
| 169 | + for i := range array { |
| 170 | + tmp := koanf.New(".") |
189 | 171 |
|
190 | | - log.Dev(p) |
| 172 | + tmp.Load(confmap.Provider(map[string]any{ |
| 173 | + "item": array[i], |
| 174 | + }, "."), nil) |
191 | 175 |
|
192 | | - err := transformChildren(config, p, transform) |
193 | | - |
194 | | - if err != nil { |
| 176 | + if err := transformChildren(tmp, "item." + subPath, transform); err != nil { |
195 | 177 | return err |
196 | 178 | } |
| 179 | + |
| 180 | + array[i] = tmp.All()["item"].(map[string]any) |
197 | 181 | } |
198 | 182 |
|
| 183 | + config.Load(confmap.Provider(map[string]any{ |
| 184 | + root: array, |
| 185 | + }, "."), nil) |
| 186 | + |
199 | 187 | return nil |
200 | 188 | } |
201 | 189 |
|
|
0 commit comments