|
1 | 1 | package config |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
4 | 5 | "os" |
5 | 6 | "path/filepath" |
6 | 7 | "strconv" |
@@ -138,28 +139,41 @@ func normalizeKeys(config *koanf.Koanf) { |
138 | 139 | // Transforms Children of path |
139 | 140 | func transformChildren(config *koanf.Koanf, path string, transform func(key string, value any) (string, any)) error { |
140 | 141 | var sub map[string]any |
| 142 | + |
141 | 143 | if err := config.Unmarshal(path, &sub); err != nil { |
142 | 144 | return err |
143 | 145 | } |
144 | 146 |
|
145 | 147 | transformed := make(map[string]any) |
| 148 | + |
146 | 149 | for key, val := range sub { |
147 | 150 | newKey, newVal := transform(key, val) |
148 | 151 |
|
149 | 152 | transformed[newKey] = newVal |
150 | 153 | } |
151 | | - |
152 | | - config.Load(confmap.Provider(map[string]any{ |
153 | | - path: map[string]any{}, |
154 | | - }, "."), nil) |
155 | 154 |
|
156 | | - config.Load(confmap.Provider(map[string]any{ |
157 | | - path: transformed, |
158 | | - }, "."), nil) |
| 155 | + parts := strings.Split(path, ".") |
| 156 | + if len(parts) < 1 { |
| 157 | + return errors.New("invalid path: " + path) |
| 158 | + } |
159 | 159 |
|
160 | | - return nil |
| 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, |
| 173 | + }, "."), nil) |
161 | 174 | } |
162 | 175 |
|
| 176 | + |
163 | 177 | // Does the same thing as transformChildren() but does it for each Array Item inside of root and transforms subPath |
164 | 178 | func transformChildrenUnderArray(config *koanf.Koanf, root string, subPath string, transform func(key string, value any) (string, any)) error { |
165 | 179 | var items []map[string]any |
|
0 commit comments