Skip to content

Commit

Permalink
ability to add props to the root mem map (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtintes authored Sep 22, 2024
1 parent a82438b commit 4fd553a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions cli/actions/memoryMap.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func applyMappings(flatFile map[string]interface{}, mappings []Mapping, masterMe
if strings.HasPrefix(key, mapping.InPath+".") || key == mapping.InPath {
keySuffix := key[len(mapping.InPath):]
newKey := mapping.ToPath + keySuffix
newKey = strings.TrimPrefix(newKey, ".")
for memKey := range masterMemoryMap {
if strings.HasPrefix(memKey, newKey+".") || strings.HasPrefix(newKey, memKey+".") {
traces = append(traces, Trace{key: memKey, oldValue: masterMemoryMap[memKey], changeType: "delete", file: filePath})
Expand Down
37 changes: 37 additions & 0 deletions cli/actions/memoryMap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,3 +449,40 @@ func TestReadMemoryMap_Traces(t *testing.T) {
assert.Equal(t, 1, len(*traces))

}

func TestReadMemoryMap_PropToRoot(t *testing.T) {

AppFs = afero.NewMemMapFs()

jsonData := `{
"key1": {
"key2": {
"key3": "value"}}
}`

_ = afero.WriteFile(AppFs, "test.json", []byte(jsonData), 0644)

config := ConfigurationMap{
Configs: []Config{
{
Path: "test.json",
Mappings: []Mapping{
{
InPath: "key1.key2",
ToPath: "",
},
},
ApplyFile: "after",
},
},
}

expectedMemoryMap := map[string]interface{}{
"key3": "value",
}

memoryMap, _, err := ReadMemoryMap(&config)

assert.Nil(t, err)
assert.Equal(t, fmt.Sprint(expectedMemoryMap), fmt.Sprint(memoryMap))
}

0 comments on commit 4fd553a

Please sign in to comment.