Skip to content

Commit 72520b3

Browse files
committed
Add utility function for parsing properties lists
Used as a convention for years, the integer terminal subproperty's use as an array syntax has now been formalized as a "list" type. Although the properties package provides a function to parse this data type, it still takes some code acrobatics to convert that into the map[string]interface{} type consumed by the JSON schema parser, which is a candidate for a function. Another "set" data type has also been added, but I don't have an immediate need for it, so will wait to add support.
1 parent b5950b7 commit 72520b3

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

internal/project/general/general.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,17 @@ func PropertiesToMap(flatProperties *properties.Map, levels int) map[string]inte
5151

5252
return propertiesInterface
5353
}
54+
55+
// PropertiesToList parses a property that has a list data type and returns it in the map[string]interface{} type
56+
// consumed by the JSON schema parser.
57+
func PropertiesToList(flatProperties *properties.Map, key string) map[string]interface{} {
58+
list := flatProperties.ExtractSubIndexLists(key)
59+
// Convert the slice to the required interface type
60+
listInterface := make([]interface{}, len(list))
61+
for i, v := range list {
62+
listInterface[i] = v
63+
}
64+
mapInterface := make(map[string]interface{})
65+
mapInterface[key] = listInterface
66+
return mapInterface
67+
}

internal/project/general/general_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,23 @@ func TestPropertiesToMap(t *testing.T) {
102102
assert.True(t, reflect.DeepEqual(expectedMapOutput, PropertiesToMap(propertiesInput, 3)))
103103
assert.True(t, reflect.DeepEqual(expectedMapOutput, PropertiesToMap(propertiesInput, 0)))
104104
}
105+
106+
func TestPropertiesToList(t *testing.T) {
107+
rawProperties := []byte(`
108+
hello=world
109+
foo.1=asdf
110+
foo.2=zxcv
111+
`)
112+
propertiesInput, err := properties.LoadFromBytes(rawProperties)
113+
require.Nil(t, err)
114+
115+
expectedMapOutput := map[string]interface{}{
116+
"hello": []interface{}{"world"},
117+
}
118+
assert.True(t, reflect.DeepEqual(expectedMapOutput, PropertiesToList(propertiesInput, "hello")))
119+
120+
expectedMapOutput = map[string]interface{}{
121+
"foo": []interface{}{"asdf", "zxcv"},
122+
}
123+
assert.True(t, reflect.DeepEqual(expectedMapOutput, PropertiesToList(propertiesInput, "foo")))
124+
}

0 commit comments

Comments
 (0)