diff --git a/internal/services/drivers/parser/terraform_parser.go b/internal/services/drivers/parser/terraform_parser.go index 6f3b024..1bf0508 100644 --- a/internal/services/drivers/parser/terraform_parser.go +++ b/internal/services/drivers/parser/terraform_parser.go @@ -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" ) @@ -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: diff --git a/internal/services/terraform.go b/internal/services/terraform.go index 36de321..608762e 100644 --- a/internal/services/terraform.go +++ b/internal/services/terraform.go @@ -16,7 +16,6 @@ package services import ( "fmt" - "regexp" "strings" logger "github.com/AppsFlyer/go-logger" @@ -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()) } } @@ -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 ?