Skip to content

Commit 30e2815

Browse files
committed
restructured transform logic
1 parent 73dfe29 commit 30e2815

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

utils/config/config.go

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package config
22

33
import (
4+
"errors"
45
"os"
56
"path/filepath"
6-
"strconv"
77
"strings"
88
"sync"
99

@@ -139,41 +139,33 @@ func normalizeKeys(config *koanf.Koanf) {
139139
// Transforms Children of path
140140
func transformChildren(config *koanf.Koanf, path string, transform func(key string, value any) (string, any)) error {
141141
var sub map[string]any
142-
143-
log.Dev(utils.ToJson(config.All()) + "\nPath:" + path)
144142

143+
if !config.Exists(path) {
144+
return errors.New("invalid path")
145+
}
146+
145147
err := config.Unmarshal(path, &sub)
146148

147149
if err != nil {
148150
return err
149151
}
150152

151-
log.Dev(utils.ToJson(sub))
152-
153153
transformed := make(map[string]any)
154154

155155
for key, val := range sub {
156156
newKey, newVal := transform(key, val)
157157

158158
transformed[newKey] = newVal
159-
160-
log.Dev(newKey)
161159
}
162-
163-
log.Dev(utils.ToJson(transformed))
164160

165161
config.Load(confmap.Provider(map[string]any{
166162
path: map[string]any{},
167163
}, "."), nil)
168164

169-
log.Dev("1:\n" + utils.ToJson(config.All()))
170-
171165
config.Load(confmap.Provider(map[string]any{
172166
path: transformed,
173167
}, "."), nil)
174168

175-
log.Dev("2:\n" + utils.ToJson(config.All()))
176-
177169
return nil
178170
}
179171

@@ -186,14 +178,34 @@ func transformChildrenUnderArray(config *koanf.Koanf, root string, subPath strin
186178
return err
187179
}
188180

189-
for i := range array {
190-
path := root + "." + strconv.Itoa(i) + "." + subPath
181+
transformed := []map[string]any{}
182+
183+
for _, data := range array {
184+
tmp := koanf.New(".")
185+
186+
tmp.Load(confmap.Provider(data, "."), nil)
187+
188+
log.Dev(utils.ToJson(tmp.All()))
191189

192-
if config.Exists(path) {
193-
transformChildren(config, path, transform)
190+
err := transformChildren(tmp, subPath, transform)
191+
192+
if err != nil {
193+
return err
194194
}
195+
196+
log.Dev(utils.ToJson(tmp.All()))
197+
198+
transformed = append(transformed, tmp.All())
195199
}
196200

201+
config.Load(confmap.Provider(map[string]any{
202+
root: map[string]any{},
203+
}, "."), nil)
204+
205+
config.Load(confmap.Provider(map[string]any{
206+
root: transformed,
207+
}, "."), nil)
208+
197209
return nil
198210
}
199211

0 commit comments

Comments
 (0)