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

     1  package parse2
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/hcl/v2"
     7  )
     8  
     9  type RuleBlock struct {
    10  	block      *hcl.Block
    11  	content    *hcl.BodyContent
    12  	Target     *String
    13  	Command    *StringArray
    14  	attributes []attribute
    15  	scope      scope
    16  }
    17  
    18  var (
    19  	ruleTargetAttributeSchema  = hcl.AttributeSchema{Name: "target", Required: true}
    20  	ruleCommandAttributeSchema = hcl.AttributeSchema{Name: "command", Required: true}
    21  	ruleSchema                 = &hcl.BodySchema{
    22  		Attributes: []hcl.AttributeSchema{
    23  			ruleTargetAttributeSchema,
    24  			ruleCommandAttributeSchema,
    25  		},
    26  	}
    27  )
    28  
    29  func (blk *RuleBlock) initAttributes(gs scope) hcl.Diagnostics {
    30  	con, result := blk.block.Body.Content(ruleSchema)
    31  
    32  	if con == nil {
    33  		return result
    34  	}
    35  
    36  	blk.content = con
    37  
    38  	blk.attributes = make([]attribute, 0, len(con.Attributes))
    39  
    40  	blk.scope = &nestedScope{outer: gs}
    41  
    42  	local := fmt.Sprintf("rule.%v", blk.block.DefRange)
    43  
    44  	for _, attr := range con.Attributes {
    45  		switch attr.Name {
    46  		case ruleTargetAttributeSchema.Name:
    47  			blk.Target = &String{attribute: attr}
    48  			attr := attribute{
    49  				name:         fmt.Sprintf("%v.target", local),
    50  				set:          setDirect("target"),
    51  				fillable:     blk.Target,
    52  				scope:        blk.scope,
    53  				dependencies: getDependencies(local, blk.Target.attribute.Expr),
    54  			}
    55  			blk.attributes = append(blk.attributes, attr)
    56  		case ruleCommandAttributeSchema.Name:
    57  			blk.Command = &StringArray{attribute: attr}
    58  			attr := attribute{
    59  				name:         fmt.Sprintf("%v.command", local),
    60  				fillable:     blk.Command,
    61  				scope:        blk.scope,
    62  				dependencies: getDependencies(local, blk.Command.attribute.Expr),
    63  			}
    64  			blk.attributes = append(blk.attributes, attr)
    65  		}
    66  	}
    67  
    68  	return result
    69  }