Skip to content

Commit e5bbd77

Browse files
committed
fix?
1 parent cf6b036 commit e5bbd77

File tree

1 file changed

+22
-34
lines changed

1 file changed

+22
-34
lines changed

utils/config/config.go

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

33
import (
4-
"errors"
54
"os"
65
"path/filepath"
7-
"strconv"
86
"strings"
97
"sync"
108

@@ -139,63 +137,53 @@ func normalizeKeys(config *koanf.Koanf) {
139137
// Transforms Children of path
140138
func transformChildren(config *koanf.Koanf, path string, transform func(key string, value any) (string, any)) error {
141139
var sub map[string]any
142-
143140
if err := config.Unmarshal(path, &sub); err != nil {
144141
return err
145142
}
146143

147144
transformed := make(map[string]any)
148-
149145
for key, val := range sub {
150146
newKey, newVal := transform(key, val)
151147

152148
transformed[newKey] = newVal
153149
}
150+
151+
config.Load(confmap.Provider(map[string]any{
152+
path: map[string]any{},
153+
}, "."), nil)
154154

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,
173157
}, "."), nil)
174-
}
175158

159+
return nil
160+
}
176161

177162
// Does the same thing as transformChildren() but does it for each Array Item inside of root and transforms subPath
178163
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 {
184166
return err
185167
}
186168

187-
for i := range items {
188-
p := root + "." + strconv.Itoa(i) + "." + subPath
169+
for i := range array {
170+
tmp := koanf.New(".")
189171

190-
log.Dev(p)
172+
tmp.Load(confmap.Provider(map[string]any{
173+
"item": array[i],
174+
}, "."), nil)
191175

192-
err := transformChildren(config, p, transform)
193-
194-
if err != nil {
176+
if err := transformChildren(tmp, "item." + subPath, transform); err != nil {
195177
return err
196178
}
179+
180+
array[i] = tmp.All()["item"].(map[string]any)
197181
}
198182

183+
config.Load(confmap.Provider(map[string]any{
184+
root: array,
185+
}, "."), nil)
186+
199187
return nil
200188
}
201189

0 commit comments

Comments
 (0)