github.com/beauknowssoftware/makehcl@v0.0.0-20200322000747-1b9bb1e1c008/internal/parse2/file.go (about)

     1  package parse2
     2  
     3  import (
     4  	"github.com/hashicorp/hcl/v2"
     5  )
     6  
     7  var (
     8  	importBlockHeaderSchema = hcl.BlockHeaderSchema{Type: "import"}
     9  	importStageSchema       = &hcl.BodySchema{
    10  		Blocks: []hcl.BlockHeaderSchema{
    11  			importBlockHeaderSchema,
    12  		},
    13  	}
    14  	ruleBlockHeaderSchema    = hcl.BlockHeaderSchema{Type: "rule"}
    15  	commandBlockHeaderSchema = hcl.BlockHeaderSchema{
    16  		Type:       "command",
    17  		LabelNames: []string{"name"},
    18  	}
    19  	varBlockHeaderSchema       = hcl.BlockHeaderSchema{Type: "var"}
    20  	defaultGoalAttributeSchema = hcl.AttributeSchema{Name: "default_goal"}
    21  	attributeStageSchema       = &hcl.BodySchema{
    22  		Attributes: []hcl.AttributeSchema{
    23  			defaultGoalAttributeSchema,
    24  		},
    25  		Blocks: []hcl.BlockHeaderSchema{
    26  			commandBlockHeaderSchema,
    27  			ruleBlockHeaderSchema,
    28  			varBlockHeaderSchema,
    29  		},
    30  	}
    31  )
    32  
    33  type File struct {
    34  	Name            string
    35  	DefaultGoal     *StringArray
    36  	hclFile         *hcl.File
    37  	unprocessedBody hcl.Body
    38  	content         *hcl.BodyContent
    39  	scope           scope
    40  	ImportBlocks    []*ImportBlock
    41  	RuleBlocks      []*RuleBlock
    42  	CommandBlocks   []*CommandBlock
    43  	varBlocks       []*varBlock
    44  	attributes      []attribute
    45  }
    46  
    47  func (f File) HasContents() bool {
    48  	return f.hclFile != nil
    49  }
    50  
    51  func (f *File) enumerateImportBlocks(ctx *hcl.EvalContext) (result hcl.Diagnostics) {
    52  	if f.unprocessedBody == nil {
    53  		return
    54  	}
    55  
    56  	f.content, f.unprocessedBody, result = f.unprocessedBody.PartialContent(importStageSchema)
    57  
    58  	if f.content == nil {
    59  		return
    60  	}
    61  
    62  	for _, blk := range f.content.Blocks {
    63  		if blk.Type != importBlockHeaderSchema.Type {
    64  			continue
    65  		}
    66  
    67  		iBlk := ImportBlock{block: blk}
    68  		f.ImportBlocks = append(f.ImportBlocks, &iBlk)
    69  
    70  		if diag := iBlk.initAttributes(ctx); diag.HasErrors() {
    71  			result = result.Extend(diag)
    72  		}
    73  	}
    74  
    75  	return
    76  }
    77  
    78  func (f *File) enumerateAttributes(gs scope) hcl.Diagnostics {
    79  	var result hcl.Diagnostics
    80  
    81  	if f.unprocessedBody == nil {
    82  		return result
    83  	}
    84  
    85  	f.content, f.unprocessedBody, result = f.unprocessedBody.PartialContent(attributeStageSchema)
    86  
    87  	if f.content == nil {
    88  		return result
    89  	}
    90  
    91  	f.scope = gs
    92  
    93  	for _, attr := range f.content.Attributes {
    94  		if attr.Name == defaultGoalAttributeSchema.Name {
    95  			f.DefaultGoal = &StringArray{attribute: attr}
    96  			attr := attribute{
    97  				name:         "default_goal",
    98  				fillable:     f.DefaultGoal,
    99  				scope:        f.scope,
   100  				dependencies: getDependencies("", f.DefaultGoal.attribute.Expr),
   101  			}
   102  			f.attributes = append(f.attributes, attr)
   103  		}
   104  	}
   105  
   106  	for _, blk := range f.content.Blocks {
   107  		switch blk.Type {
   108  		case commandBlockHeaderSchema.Type:
   109  			cBlk := CommandBlock{block: blk}
   110  			f.CommandBlocks = append(f.CommandBlocks, &cBlk)
   111  
   112  			if diag := cBlk.initAttributes(gs); diag.HasErrors() {
   113  				result = result.Extend(diag)
   114  			}
   115  
   116  			f.attributes = append(f.attributes, cBlk.attributes...)
   117  		case ruleBlockHeaderSchema.Type:
   118  			rBlk := RuleBlock{block: blk}
   119  			f.RuleBlocks = append(f.RuleBlocks, &rBlk)
   120  
   121  			if diag := rBlk.initAttributes(gs); diag.HasErrors() {
   122  				result = result.Extend(diag)
   123  			}
   124  
   125  			f.attributes = append(f.attributes, rBlk.attributes...)
   126  		case varBlockHeaderSchema.Type:
   127  			vBlk := varBlock{block: blk}
   128  			f.varBlocks = append(f.varBlocks, &vBlk)
   129  
   130  			if diag := vBlk.initAttributes(gs); diag.HasErrors() {
   131  				result = result.Extend(diag)
   132  			}
   133  
   134  			f.attributes = append(f.attributes, vBlk.attributes...)
   135  		}
   136  	}
   137  
   138  	return result
   139  }