2020-03-18 17:28:57 +01:00
|
|
|
// Copyright 2016-2020, Pulumi Corporation.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
2020-04-03 06:23:12 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2020-03-18 17:28:57 +01:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
|
|
|
"github.com/hashicorp/hcl/v2/hclsyntax"
|
2020-04-14 10:30:25 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/v2/codegen/hcl2/syntax"
|
2020-03-18 17:28:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Attribute represents an HCL2 attribute.
|
|
|
|
type Attribute struct {
|
|
|
|
// The syntax node for the attribute, if any.
|
|
|
|
Syntax *hclsyntax.Attribute
|
|
|
|
// The tokens for the attribute.
|
2020-04-03 05:01:14 +02:00
|
|
|
Tokens *syntax.AttributeTokens
|
2020-03-18 17:28:57 +01:00
|
|
|
|
|
|
|
// The attribute's name.
|
|
|
|
Name string
|
|
|
|
// The attribute's value.
|
|
|
|
Value Expression
|
|
|
|
}
|
|
|
|
|
|
|
|
// SyntaxNode returns the syntax node of the attribute, and will either return an *hclsyntax.Attribute or syntax.None.
|
|
|
|
func (a *Attribute) SyntaxNode() hclsyntax.Node {
|
|
|
|
return syntaxOrNone(a.Syntax)
|
|
|
|
}
|
|
|
|
|
2020-04-10 01:37:52 +02:00
|
|
|
func (a *Attribute) HasLeadingTrivia() bool {
|
2020-04-03 06:23:12 +02:00
|
|
|
return a.Tokens != nil
|
|
|
|
}
|
|
|
|
|
2020-04-10 01:37:52 +02:00
|
|
|
func (a *Attribute) HasTrailingTrivia() bool {
|
|
|
|
return a.Value.HasTrailingTrivia()
|
2020-04-03 06:23:12 +02:00
|
|
|
}
|
|
|
|
|
2020-07-07 22:46:26 +02:00
|
|
|
func (a *Attribute) GetLeadingTrivia() syntax.TriviaList {
|
|
|
|
return a.Tokens.GetName(a.Name).LeadingTrivia
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Attribute) GetTrailingTrivia() syntax.TriviaList {
|
|
|
|
return a.Value.GetTrailingTrivia()
|
|
|
|
}
|
|
|
|
|
2020-04-03 06:23:12 +02:00
|
|
|
func (a *Attribute) Format(f fmt.State, c rune) {
|
|
|
|
a.print(f, &printer{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Attribute) print(w io.Writer, p *printer) {
|
2020-04-14 04:11:56 +02:00
|
|
|
p.fprintf(w, "%v% v% v", a.Tokens.GetName(a.Name), a.Tokens.GetEquals(), a.Value)
|
2020-04-03 06:23:12 +02:00
|
|
|
}
|
|
|
|
|
2020-04-07 04:43:16 +02:00
|
|
|
func (a *Attribute) Type() Type {
|
|
|
|
return a.Value.Type()
|
|
|
|
}
|
|
|
|
|
2020-03-18 17:28:57 +01:00
|
|
|
func (*Attribute) isBodyItem() {}
|
|
|
|
|
|
|
|
// BindAttribute binds an HCL2 attribute using the given scope and token map.
|
2020-04-17 17:24:44 +02:00
|
|
|
func BindAttribute(attribute *hclsyntax.Attribute, scope *Scope, tokens syntax.TokenMap,
|
|
|
|
opts ...BindOption) (*Attribute, hcl.Diagnostics) {
|
|
|
|
|
|
|
|
value, diagnostics := BindExpression(attribute.Expr, scope, tokens, opts...)
|
2020-04-03 05:01:14 +02:00
|
|
|
attributeTokens, _ := tokens.ForNode(attribute).(*syntax.AttributeTokens)
|
2020-03-18 17:28:57 +01:00
|
|
|
return &Attribute{
|
|
|
|
Syntax: attribute,
|
|
|
|
Tokens: attributeTokens,
|
|
|
|
Name: attribute.Name,
|
|
|
|
Value: value,
|
|
|
|
}, diagnostics
|
|
|
|
}
|