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

     1  package parse2
     2  
     3  import "github.com/hashicorp/hcl/v2"
     4  
     5  type ImportBlock struct {
     6  	block   *hcl.Block
     7  	content *hcl.BodyContent
     8  	File    *String
     9  }
    10  
    11  var (
    12  	importFileAttributeSchema = hcl.AttributeSchema{Name: "file", Required: true}
    13  	importSchema              = &hcl.BodySchema{
    14  		Attributes: []hcl.AttributeSchema{
    15  			importFileAttributeSchema,
    16  		},
    17  	}
    18  )
    19  
    20  func (blk *ImportBlock) initAttributes(ctx *hcl.EvalContext) hcl.Diagnostics {
    21  	con, result := blk.block.Body.Content(importSchema)
    22  
    23  	if con == nil {
    24  		return result
    25  	}
    26  
    27  	blk.content = con
    28  
    29  	for _, attr := range con.Attributes {
    30  		if attr.Name == importFileAttributeSchema.Name {
    31  			if diag := blk.setFile(attr, ctx); diag.HasErrors() {
    32  				result = result.Extend(diag)
    33  			}
    34  		}
    35  	}
    36  
    37  	return result
    38  }
    39  
    40  func (blk *ImportBlock) setFile(attr *hcl.Attribute, ctx *hcl.EvalContext) (diag hcl.Diagnostics) {
    41  	blk.File, diag = newStringAttribute(attr, ctx)
    42  	return
    43  }