Skip to content

Commit

Permalink
Merge pull request #266 from per1234/fix-overlapping-prop-mapping
Browse files Browse the repository at this point in the history
Fix properties mapping utility function handling of overlapping keys
  • Loading branch information
per1234 committed Sep 1, 2021
2 parents 9494fa4 + d18887a commit 3b6da1c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
23 changes: 11 additions & 12 deletions internal/project/general/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,19 @@ In the event a full recursion of key levels is desired, set the levels argument
func PropertiesToMap(flatProperties *properties.Map, levels int) map[string]interface{} {
propertiesInterface := make(map[string]interface{})

var keys []string
if levels != 1 {
keys = flatProperties.FirstLevelKeys()
for _, key := range flatProperties.FirstLevelKeys() {
subTree := flatProperties.SubTree(key)
if subTree.Size() > 0 {
// This key contains a map.
propertiesInterface[key] = PropertiesToMap(subTree, levels-1)
} else {
// This key contains a string, no more recursion is possible.
propertiesInterface[key] = flatProperties.Get(key)
}
}
} else {
keys = flatProperties.Keys()
}

for _, key := range keys {
subTree := flatProperties.SubTree(key)
if subTree.Size() > 0 {
// This key contains a map.
propertiesInterface[key] = PropertiesToMap(subTree, levels-1)
} else {
// This key contains a string, no more recursion is possible.
for _, key := range flatProperties.Keys() {
propertiesInterface[key] = flatProperties.Get(key)
}
}
Expand Down
26 changes: 26 additions & 0 deletions internal/project/general/general_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func TestPropertiesToMap(t *testing.T) {
foo.bar=asdf
foo.baz=zxcv
bar.bat.bam=123
qux.a=x
qux.a.b=y
fuz.a.b=y
fuz.a=x
`)
propertiesInput, err := properties.LoadFromBytes(rawProperties)
require.Nil(t, err)
Expand All @@ -41,6 +45,10 @@ func TestPropertiesToMap(t *testing.T) {
"foo.bar": "asdf",
"foo.baz": "zxcv",
"bar.bat.bam": "123",
"qux.a": "x",
"qux.a.b": "y",
"fuz.a.b": "y",
"fuz.a": "x",
}

assert.True(t, reflect.DeepEqual(expectedMapOutput, PropertiesToMap(propertiesInput, 1)))
Expand All @@ -55,6 +63,14 @@ func TestPropertiesToMap(t *testing.T) {
"bar": map[string]interface{}{
"bat.bam": "123",
},
"qux": map[string]interface{}{
"a": "x",
"a.b": "y",
},
"fuz": map[string]interface{}{
"a.b": "y",
"a": "x",
},
}

assert.True(t, reflect.DeepEqual(expectedMapOutput, PropertiesToMap(propertiesInput, 2)))
Expand All @@ -71,6 +87,16 @@ func TestPropertiesToMap(t *testing.T) {
"bam": "123",
},
},
"qux": map[string]interface{}{
"a": map[string]interface{}{
"b": "y", // It is impossible to represent the complete "properties" data structure recursed to this depth.
},
},
"fuz": map[string]interface{}{
"a": map[string]interface{}{
"b": "y",
},
},
}

assert.True(t, reflect.DeepEqual(expectedMapOutput, PropertiesToMap(propertiesInput, 3)))
Expand Down

0 comments on commit 3b6da1c

Please sign in to comment.