Skip to content

Commit cf6b036

Browse files
committed
update transform logic
1 parent cada417 commit cf6b036

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

utils/config/config.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"errors"
45
"os"
56
"path/filepath"
67
"strconv"
@@ -138,28 +139,41 @@ func normalizeKeys(config *koanf.Koanf) {
138139
// Transforms Children of path
139140
func transformChildren(config *koanf.Koanf, path string, transform func(key string, value any) (string, any)) error {
140141
var sub map[string]any
142+
141143
if err := config.Unmarshal(path, &sub); err != nil {
142144
return err
143145
}
144146

145147
transformed := make(map[string]any)
148+
146149
for key, val := range sub {
147150
newKey, newVal := transform(key, val)
148151

149152
transformed[newKey] = newVal
150153
}
151-
152-
config.Load(confmap.Provider(map[string]any{
153-
path: map[string]any{},
154-
}, "."), nil)
155154

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+
}
159159

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)
161174
}
162175

176+
163177
// Does the same thing as transformChildren() but does it for each Array Item inside of root and transforms subPath
164178
func transformChildrenUnderArray(config *koanf.Koanf, root string, subPath string, transform func(key string, value any) (string, any)) error {
165179
var items []map[string]any

0 commit comments

Comments
 (0)