Skip to content

Commit

Permalink
fix logic for nested loops
Browse files Browse the repository at this point in the history
  • Loading branch information
Yusuf Kanchwala committed Oct 13, 2021
1 parent 11e5090 commit ac5cfed
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions pkg/iac-providers/terraform/commons/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,18 @@ func (c *converter) convertBody(body *hclsyntax.Body) (jsonObj, lineObj, error)
}

blockConfig := bcfg[block.Type].(jsonObj)
lineCfg := blcfg[block.Type].(lineObj)
if _, present := cfg[block.Type]; !present {
cfg[block.Type] = []jsonObj{blockConfig}
if lineCfg, ok := blcfg[block.Type].(lineObj); ok {
lcfg[block.Type] = []lineObj{lineCfg}
}

lcfg[block.Type] = []lineObj{lineCfg}
} else {
list := cfg[block.Type].([]jsonObj)
list = append(list, blockConfig)
cfg[block.Type] = list

if lineList, ok := lcfg[block.Type].([]lineObj); ok {
if lineCfg, ok := blcfg[block.Type].(lineObj); ok {
lineList = append(lineList, lineCfg)
}
lcfg[block.Type] = lineList
}
lineList := lcfg[block.Type].([]lineObj)
lineList = append(lineList, lineCfg)
lcfg[block.Type] = lineList
}
}

Expand All @@ -112,10 +107,26 @@ func (c *converter) convertBlock(block *hclsyntax.Block, cfg jsonObj, lcfg lineO
// TODO: better diagnostics
return fmt.Errorf("unable to convert Block to JSON: %v.%v", block.Type, strings.Join(block.Labels, "."))
}

if innerLineObj := lcfg[key]; exists {
lcfg, ok = innerLineObj.(lineObj)
if !ok {
return fmt.Errorf("unable to convert Block to JSON: %v.%v", block.Type, strings.Join(block.Labels, "."))
}
}

} else {
obj := make(jsonObj)
var (
obj = make(jsonObj)
lobj = make(lineObj)
)

cfg[key] = obj
cfg = obj

lcfg[key] = lobj
lcfg = lobj

}
key = label
}
Expand Down Expand Up @@ -145,7 +156,7 @@ func (c *converter) convertBlock(block *hclsyntax.Block, cfg jsonObj, lcfg lineO
return nil
}

func (c *converter) convertExpression(expr hclsyntax.Expression) (ret interface{}, line int, err error) {
func (c *converter) convertExpression(expr hclsyntax.Expression) (ret interface{}, line interface{}, err error) {
// assume it is hcl syntax (because, um, it is)
line = expr.StartRange().Start.Line
switch value := expr.(type) {
Expand All @@ -168,17 +179,18 @@ func (c *converter) convertExpression(expr hclsyntax.Expression) (ret interface{
return list, line, nil
case *hclsyntax.ObjectConsExpr:
m := make(jsonObj)
l := make(lineObj)
for _, item := range value.Items {
key, err := c.convertKey(item.KeyExpr)
if err != nil {
return nil, line, err
}
m[key], line, err = c.convertExpression(item.ValueExpr)
m[key], l[key], err = c.convertExpression(item.ValueExpr)
if err != nil {
return nil, line, err
}
}
return m, line, nil
return m, l, nil
default:
return c.wrapExpr(expr), line, nil
}
Expand Down

0 comments on commit ac5cfed

Please sign in to comment.