pulumi/pkg/codegen/hcl2/model/block.go

58 lines
1.7 KiB
Go

// 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 (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/pulumi/pulumi/pkg/v2/codegen/hcl2/syntax"
)
// Block represents an HCL2 block.
type Block struct {
// The syntax node for the block, if any.
Syntax *hclsyntax.Block
// The tokens for the block.
Tokens syntax.BlockTokens
// The block's type.
Type string
// The block's labels.
Labels []string
// The block's body.
Body *Body
}
// SyntaxNode returns the syntax node of the block, and will either return an *hclsyntax.Block or syntax.None.
func (b *Block) SyntaxNode() hclsyntax.Node {
return syntaxOrNone(b.Syntax)
}
func (*Block) isBodyItem() {}
// BindBlock binds an HCL2 block using the given scopes and token map.
func BindBlock(block *hclsyntax.Block, scopes Scopes, tokens syntax.TokenMap) (*Block, hcl.Diagnostics) {
body, diagnostics := BindBody(block.Body, scopes, tokens)
blockTokens, _ := tokens.ForNode(block).(syntax.BlockTokens)
return &Block{
Syntax: block,
Tokens: blockTokens,
Type: block.Type,
Labels: block.Labels,
Body: body,
}, diagnostics
}