Commit 8e508e5c by gotjosh Committed by Torkel Ödegaard

Provisioning: Handle empty nested keys on YAML provisioning datasources (#19547)

* Fix: Handle empty nested keys on YAML provisioning datasources

As we provision a datasource via a YAML file, we attempt to transform the
file into sensible Go types that the provisioning code can use.

While this happens, there is a chance some of the keys nested within
the YAML array are empty.

This fix allows the YAML parser to handle empty keys by null checking
the return of `reflect.TypeOf` which according to the documentation:

> TypeOf returns the reflection Type that represents the dynamic type of i. If i is a nil interface value, TypeOf returns nil.

Can return nil.

* Add tests
parent ac11352e
...@@ -155,7 +155,13 @@ func (val *StringMapValue) Value() map[string]string { ...@@ -155,7 +155,13 @@ func (val *StringMapValue) Value() map[string]string {
// slices and the actual interpolation is done on all simple string values in the structure. It returns a copy of any // slices and the actual interpolation is done on all simple string values in the structure. It returns a copy of any
// map or slice value instead of modifying them in place. // map or slice value instead of modifying them in place.
func tranformInterface(i interface{}) interface{} { func tranformInterface(i interface{}) interface{} {
switch reflect.TypeOf(i).Kind() { typeOf := reflect.TypeOf(i)
if typeOf == nil {
return nil
}
switch typeOf.Kind() {
case reflect.Slice: case reflect.Slice:
return transformSlice(i.([]interface{})) return transformSlice(i.([]interface{}))
case reflect.Map: case reflect.Map:
......
...@@ -131,6 +131,8 @@ func TestValues(t *testing.T) { ...@@ -131,6 +131,8 @@ func TestValues(t *testing.T) {
- two - two
- three: - three:
inside: $STRING inside: $STRING
- six:
empty:
four: four:
nested: nested:
onemore: $INT onemore: $INT
...@@ -146,11 +148,18 @@ func TestValues(t *testing.T) { ...@@ -146,11 +148,18 @@ func TestValues(t *testing.T) {
"one": 1, "one": 1,
"two": "test", "two": "test",
"three": []interface{}{ "three": []interface{}{
1, "two", anyMap{ 1,
"two",
anyMap{
"three": anyMap{ "three": anyMap{
"inside": "test", "inside": "test",
}, },
}, },
anyMap{
"six": anyMap{
"empty": interface{}(nil),
},
},
}, },
"four": anyMap{ "four": anyMap{
"nested": anyMap{ "nested": anyMap{
...@@ -166,11 +175,18 @@ func TestValues(t *testing.T) { ...@@ -166,11 +175,18 @@ func TestValues(t *testing.T) {
"one": 1, "one": 1,
"two": "$STRING", "two": "$STRING",
"three": []interface{}{ "three": []interface{}{
1, "two", anyMap{ 1,
"two",
anyMap{
"three": anyMap{ "three": anyMap{
"inside": "$STRING", "inside": "$STRING",
}, },
}, },
anyMap{
"six": anyMap{
"empty": interface{}(nil),
},
},
}, },
"four": anyMap{ "four": anyMap{
"nested": anyMap{ "nested": anyMap{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment