Fix bug in buildPropertyMap in previous commit.

Property value should be evaluated before proeprty map is populated with
it. Added the test for this case.
This commit is contained in:
Sasha Smundak 2020-03-03 17:36:00 -08:00
parent 29fdcad56c
commit de4d9f94ac
2 changed files with 38 additions and 28 deletions

View file

@ -136,23 +136,22 @@ func (ctx *unpackContext) buildPropertyMap(prefix string, properties []*parser.P
} }
ctx.propertyMap[name] = &packedProperty{property, false} ctx.propertyMap[name] = &packedProperty{property, false}
if structProperty, ok := property.Value.(*parser.Map); ok { switch propValue := property.Value.Eval().(type) {
ctx.buildPropertyMap(name, structProperty.Properties) case *parser.Map:
} ctx.buildPropertyMap(name, propValue.Properties)
case *parser.List:
// If it is a list, unroll it unless its elements are of primitive type // If it is a list, unroll it unless its elements are of primitive type
// (no further mapping will be needed in that case, so we avoid cluttering // (no further mapping will be needed in that case, so we avoid cluttering
// the map). // the map).
listExpr, ok := property.Value.Eval().(*parser.List) if len(propValue.Values) == 0 {
if !ok || len(listExpr.Values) == 0 {
continue continue
} }
if t := listExpr.Values[0].Eval().Type(); t == parser.StringType || t == parser.Int64Type || t == parser.BoolType { if t := propValue.Values[0].Type(); t == parser.StringType || t == parser.Int64Type || t == parser.BoolType {
continue continue
} }
itemProperties := make([]*parser.Property, len(listExpr.Values), len(listExpr.Values)) itemProperties := make([]*parser.Property, len(propValue.Values), len(propValue.Values))
for i, expr := range listExpr.Values { for i, expr := range propValue.Values {
itemProperties[i] = &parser.Property{ itemProperties[i] = &parser.Property{
Name: property.Name + "[" + strconv.Itoa(i) + "]", Name: property.Name + "[" + strconv.Itoa(i) + "]",
NamePos: property.NamePos, NamePos: property.NamePos,
@ -164,6 +163,7 @@ func (ctx *unpackContext) buildPropertyMap(prefix string, properties []*parser.P
return false return false
} }
} }
}
return len(ctx.errs) == nOldErrors return len(ctx.errs) == nOldErrors
} }

View file

@ -496,10 +496,12 @@ var validUnpackTestCases = []struct {
list = ["abc"] list = ["abc"]
string = "def" string = "def"
list_with_variable = [string] list_with_variable = [string]
struct_value = { name: "foo" }
m { m {
s: string, s: string,
list: list, list: list,
list2: list_with_variable, list2: list_with_variable,
structattr: struct_value,
} }
`, `,
output: []interface{}{ output: []interface{}{
@ -507,10 +509,18 @@ var validUnpackTestCases = []struct {
S string S string
List []string List []string
List2 []string List2 []string
Structattr struct {
Name string
}
}{ }{
S: "def", S: "def",
List: []string{"abc"}, List: []string{"abc"},
List2: []string{"def"}, List2: []string{"def"},
Structattr: struct {
Name string
}{
Name: "foo",
},
}, },
}, },
}, },