github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/codegen/hcl2/model/attribute.go (about)

     1  // Copyright 2016-2020, Pulumi Corporation.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package model
    16  
    17  import (
    18  	"fmt"
    19  	"io"
    20  
    21  	"github.com/hashicorp/hcl/v2"
    22  	"github.com/hashicorp/hcl/v2/hclsyntax"
    23  	"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/syntax"
    24  )
    25  
    26  // Attribute represents an HCL2 attribute.
    27  type Attribute struct {
    28  	// The syntax node for the attribute, if any.
    29  	Syntax *hclsyntax.Attribute
    30  	// The tokens for the attribute.
    31  	Tokens *syntax.AttributeTokens
    32  
    33  	// The attribute's name.
    34  	Name string
    35  	// The attribute's value.
    36  	Value Expression
    37  }
    38  
    39  // SyntaxNode returns the syntax node of the attribute, and will either return an *hclsyntax.Attribute or syntax.None.
    40  func (a *Attribute) SyntaxNode() hclsyntax.Node {
    41  	return syntaxOrNone(a.Syntax)
    42  }
    43  
    44  func (a *Attribute) HasLeadingTrivia() bool {
    45  	return a.Tokens != nil
    46  }
    47  
    48  func (a *Attribute) HasTrailingTrivia() bool {
    49  	return a.Value.HasTrailingTrivia()
    50  }
    51  
    52  func (a *Attribute) GetLeadingTrivia() syntax.TriviaList {
    53  	return a.Tokens.GetName(a.Name).LeadingTrivia
    54  }
    55  
    56  func (a *Attribute) GetTrailingTrivia() syntax.TriviaList {
    57  	return a.Value.GetTrailingTrivia()
    58  }
    59  
    60  func (a *Attribute) Format(f fmt.State, c rune) {
    61  	a.print(f, &printer{})
    62  }
    63  
    64  func (a *Attribute) print(w io.Writer, p *printer) {
    65  	p.fprintf(w, "%v% v% v", a.Tokens.GetName(a.Name), a.Tokens.GetEquals(), a.Value)
    66  }
    67  
    68  func (a *Attribute) Type() Type {
    69  	return a.Value.Type()
    70  }
    71  
    72  func (*Attribute) isBodyItem() {}
    73  
    74  // BindAttribute binds an HCL2 attribute using the given scope and token map.
    75  func BindAttribute(attribute *hclsyntax.Attribute, scope *Scope, tokens syntax.TokenMap,
    76  	opts ...BindOption) (*Attribute, hcl.Diagnostics) {
    77  
    78  	value, diagnostics := BindExpression(attribute.Expr, scope, tokens, opts...)
    79  	attributeTokens, _ := tokens.ForNode(attribute).(*syntax.AttributeTokens)
    80  	return &Attribute{
    81  		Syntax: attribute,
    82  		Tokens: attributeTokens,
    83  		Name:   attribute.Name,
    84  		Value:  value,
    85  	}, diagnostics
    86  }