Fix silently ignoring values assigned to map properties

Values assigned to map properties were silently ignored instead of
reported as an error.  Add a check when recursing into structs that
the value is a map.

Fixes: 177706602
Test: m nothing
Test: TestUnpackErrors
Change-Id: Ic56aeb1b9da6d5c86b6d98adae7bddb60c450404
This commit is contained in:
Colin Cross 2021-01-15 20:09:51 -08:00
parent f6ef155884
commit 3adb240964
2 changed files with 76 additions and 0 deletions

View file

@ -280,6 +280,14 @@ func (ctx *unpackContext) unpackToStruct(namePrefix string, structValue reflect.
}
if isStruct(fieldValue.Type()) {
if property.Value.Eval().Type() != parser.MapType {
ctx.addError(&UnpackError{
fmt.Errorf("can't assign %s value to map property %q",
property.Value.Type(), property.Name),
property.Value.Pos(),
})
continue
}
ctx.unpackToStruct(propertyName, fieldValue)
if len(ctx.errs) >= maxUnpackErrors {
return

View file

@ -894,6 +894,74 @@ func TestUnpackErrors(t *testing.T) {
`<input>:4:13: <-- previous definition here`,
},
},
{
name: "wrong type",
input: `
m {
int: "foo",
}
`,
output: []interface{}{
&struct {
Int *int64
}{},
},
errors: []string{
`<input>:3:11: can't assign string value to int64 property "int"`,
},
},
{
name: "wrong type for map",
input: `
m {
map: "foo",
}
`,
output: []interface{}{
&struct {
Map struct {
S string
}
}{},
},
errors: []string{
`<input>:3:11: can't assign string value to map property "map"`,
},
},
{
name: "wrong type for list",
input: `
m {
list: "foo",
}
`,
output: []interface{}{
&struct {
List []string
}{},
},
errors: []string{
`<input>:3:12: can't assign string value to list property "list"`,
},
},
{
name: "wrong type for list of maps",
input: `
m {
map_list: "foo",
}
`,
output: []interface{}{
&struct {
Map_list []struct {
S string
}
}{},
},
errors: []string{
`<input>:3:16: can't assign string value to list property "map_list"`,
},
},
}
for _, testCase := range testCases {