github.com/mineiros-io/terradoc@v0.0.9-0.20220711062319-018bd4ae81f5/internal/parsers/docparser/variables.go (about) 1 package docparser 2 3 import ( 4 "errors" 5 "fmt" 6 7 "github.com/hashicorp/hcl/v2" 8 "github.com/mineiros-io/terradoc/internal/entities" 9 "github.com/mineiros-io/terradoc/internal/parsers/hclparser" 10 "github.com/mineiros-io/terradoc/internal/schemas/docschema" 11 ) 12 13 func parseVariables(variableBlocks hcl.Blocks) (variables []entities.Variable, err error) { 14 for _, varBlk := range variableBlocks { 15 variable, err := parseVariable(varBlk) 16 if err != nil { 17 return nil, fmt.Errorf("parsing variable: %s", err) 18 } 19 20 variables = append(variables, variable) 21 } 22 23 return variables, nil 24 } 25 26 func parseVariable(variableBlock *hcl.Block) (entities.Variable, error) { 27 if len(variableBlock.Labels) != 1 { 28 return entities.Variable{}, errors.New("variable block does not have a name") 29 } 30 31 variableContent, diags := variableBlock.Body.Content(docschema.VariableSchema()) 32 if diags.HasErrors() { 33 return entities.Variable{}, fmt.Errorf("parsing variable: %v", diags.Errs()) 34 } 35 36 // variable blocks are required to have a label as defined in the schema 37 name := variableBlock.Labels[0] 38 variable, err := createVariableFromHCLAttributes(variableContent.Attributes, name) 39 if err != nil { 40 return entities.Variable{}, fmt.Errorf("parsing variable: %s", err) 41 } 42 43 // variables have only `attribute` blocks 44 attributes, err := parseVariableAttributes(variableContent.Blocks.OfType(attributeBlockName)) 45 if err != nil { 46 return entities.Variable{}, fmt.Errorf("parsing variable attributes: %s", err) 47 } 48 variable.Attributes = attributes 49 50 return variable, nil 51 } 52 53 func createVariableFromHCLAttributes(attrs hcl.Attributes, name string) (entities.Variable, error) { 54 var err error 55 56 variable := entities.Variable{Name: name} 57 58 variable.Description, err = hclparser.GetAttribute(attrs, descriptionAttributeName).String() 59 if err != nil { 60 return entities.Variable{}, err 61 } 62 63 variable.Default, err = hclparser.GetAttribute(attrs, defaultAttributeName).RawJSON() 64 if err != nil { 65 return entities.Variable{}, err 66 } 67 68 variable.Required, err = hclparser.GetAttribute(attrs, requiredAttributeName).Bool() 69 if err != nil { 70 return entities.Variable{}, err 71 } 72 73 variable.ForcesRecreation, err = hclparser.GetAttribute(attrs, forcesRecreationAttributeName).Bool() 74 if err != nil { 75 return entities.Variable{}, err 76 } 77 78 variable.ReadmeExample, err = hclparser.GetAttribute(attrs, readmeExampleAttributeName).String() 79 if err != nil { 80 return entities.Variable{}, err 81 } 82 83 // type definition 84 readmeType := hclparser.GetAttribute(attrs, readmeTypeAttributeName) 85 if readmeType == nil { 86 variable.Type, err = hclparser.GetAttribute(attrs, typeAttributeName).VarType() 87 } else { 88 variable.Type, err = readmeType.VarTypeFromString() 89 } 90 91 if err != nil { 92 return entities.Variable{}, err 93 } 94 95 return variable, nil 96 }