github.com/mineiros-io/terradoc@v0.0.9-0.20220711062319-018bd4ae81f5/internal/parsers/docparser/sections.go (about)

     1  package docparser
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/hcl/v2"
     7  	"github.com/mineiros-io/terradoc/internal/entities"
     8  	"github.com/mineiros-io/terradoc/internal/parsers/hclparser"
     9  	"github.com/mineiros-io/terradoc/internal/schemas/docschema"
    10  )
    11  
    12  const (
    13  	rootSectionLevel = 1
    14  )
    15  
    16  func parseSections(sectionBlocks hcl.Blocks) (sections []entities.Section, err error) {
    17  	for _, sectionBlock := range sectionBlocks {
    18  		section, err := parseSection(sectionBlock, rootSectionLevel) // initial level
    19  		if err != nil {
    20  			return nil, fmt.Errorf("parsing sections: %s", err)
    21  		}
    22  
    23  		sections = append(sections, section)
    24  	}
    25  
    26  	return sections, nil
    27  }
    28  
    29  func parseSection(sectionBlock *hcl.Block, level int) (entities.Section, error) {
    30  	sectionContent, diags := sectionBlock.Body.Content(docschema.SectionSchema())
    31  	if diags.HasErrors() {
    32  		return entities.Section{}, fmt.Errorf("parsing Terradoc section: %v", diags.Errs())
    33  	}
    34  
    35  	section, err := createSectionFromHCLAttributes(sectionContent.Attributes, level)
    36  	if err != nil {
    37  		return entities.Section{}, fmt.Errorf("parsing section: %s", err)
    38  	}
    39  
    40  	// parse `variable` blocks
    41  	variables, err := parseVariables(sectionContent.Blocks.OfType(variableBlockName))
    42  	if err != nil {
    43  		return entities.Section{}, fmt.Errorf("parsing section variable: %v", err)
    44  	}
    45  	section.Variables = variables
    46  
    47  	// parse `output` blocks
    48  	outputs, err := parseOutputs(sectionContent.Blocks.OfType(outputBlockName))
    49  	if err != nil {
    50  		return entities.Section{}, fmt.Errorf("parsing section variable: %v", err)
    51  	}
    52  	section.Outputs = outputs
    53  
    54  	subSectionLevel := level + 1
    55  	// parse `section` blocks
    56  	for _, subSectionBlk := range sectionContent.Blocks.OfType(sectionBlockName) {
    57  		subSection, err := parseSection(subSectionBlk, subSectionLevel)
    58  		if err != nil {
    59  			return entities.Section{}, fmt.Errorf("parsing subsection: %s", err)
    60  		}
    61  
    62  		section.SubSections = append(section.SubSections, subSection)
    63  	}
    64  
    65  	return section, nil
    66  }
    67  
    68  func createSectionFromHCLAttributes(attrs hcl.Attributes, level int) (entities.Section, error) {
    69  	var err error
    70  
    71  	section := entities.Section{Level: level}
    72  
    73  	section.Title, err = hclparser.GetAttribute(attrs, titleAttributeName).String()
    74  	if err != nil {
    75  		return entities.Section{}, err
    76  	}
    77  
    78  	section.Content, err = hclparser.GetAttribute(attrs, contentAttributeName).String()
    79  	if err != nil {
    80  		return entities.Section{}, err
    81  	}
    82  
    83  	section.TOC, err = hclparser.GetAttribute(attrs, tocAttributeName).Bool()
    84  	if err != nil {
    85  		return entities.Section{}, err
    86  	}
    87  
    88  	return section, nil
    89  }