Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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