Skip to content

Commit

Permalink
fix: fixed nested variables ignore on parsing (#41)
Browse files Browse the repository at this point in the history
* fix: fixed nested variables ignore on parsing

* fix: fixing lint

---------

Co-authored-by: osher.elmakaies <[email protected]>
  • Loading branch information
osherElm and osher.elmakaies authored May 22, 2023
1 parent 8caf30b commit 11ef984
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 31 deletions.
15 changes: 5 additions & 10 deletions internal/services/drivers/parser/terraform_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ package parser

import (
"errors"
"github.com/AppsFlyer/terra-crust/internal/services/drivers"
"os"
"path/filepath"
"strings"

"github.com/AppsFlyer/terra-crust/internal/services/drivers"

logger "github.com/AppsFlyer/go-logger"

"github.com/AppsFlyer/terra-crust/internal/types"

hcl "github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/zclconf/go-cty/cty"
)
Expand Down Expand Up @@ -120,13 +121,7 @@ func (p *TerraformParser) parseVariable(block *hclwrite.Block) *types.Variable {
for k, v := range body.Attributes() {
switch k {
case "type":
var typeTokens hclwrite.Tokens
for _, t := range v.Expr().BuildTokens(nil) {
if t.Type != hclsyntax.TokenNewline {
typeTokens = append(typeTokens, t)
}
}
variable.Type = typeTokens
variable.Type = v.Expr().BuildTokens(nil)
case "default":
variable.Default = v.Expr().BuildTokens(nil)
case HclBlockDescriptionField:
Expand Down
25 changes: 4 additions & 21 deletions internal/services/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package services

import (
"fmt"
"regexp"
"strings"

logger "github.com/AppsFlyer/go-logger"
Expand Down Expand Up @@ -79,26 +78,7 @@ func (t *Terraform) GenerateModuleVariableObject(modulesFilePath, destinationPat

for _, v := range m.Variables {
if v.Default != nil && string(v.Default.Bytes()) != emptyStringWrapped {
value := strings.ReplaceAll(string(v.Type.Bytes()), " ", emptyString)
if strings.Contains(string(v.Type.Bytes()), "object") && strings.Contains(string(v.Type.Bytes()), "list") {
pattern := `(\w+)\s*=\s*(\w+)`
regex := regexp.MustCompile(pattern)
matches := regex.FindAllStringSubmatch(string(v.Type.Bytes()), -1)
var sb strings.Builder

sb.WriteString("optional(list(object({")
for _, match := range matches {
key := match[1]
val := match[2]

sb.WriteString(fmt.Sprintf("%s=%s\n", key, val))
}

sb.WriteString("})))")

value = sb.String()
}
out[moduleName].ObjectTypeMapping[v.Name] = value
out[moduleName].ObjectTypeMapping[v.Name] = string(v.Type.Bytes())
out[moduleName].DefaultValues[v.Name] = string(v.Default.Bytes())
}
}
Expand Down Expand Up @@ -256,3 +236,6 @@ func (t *Terraform) parseModule(m *types.Module, moduleName string, out *templat
}
}
}

//is it possible to make a code that receive a generic string that could change but has a template of :
//"key = value key2 = value2...." and it repeats alot of time, The spaces between the "key" to the "=" could be none and could be alot , the space between value1 to key2 could be 1 and could be alot , it cannot be 0, is it possible to extract the key value into a map or any kind of data structure ?

0 comments on commit 11ef984

Please sign in to comment.