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

112 lines
3.2 KiB
Go
Raw Permalink Normal View History

// 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 (
"strings"
"github.com/hashicorp/hcl/v2"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
"github.com/zclconf/go-cty/cty"
)
// Traversable represents an entity that can be traversed by an HCL2 traverser.
type Traversable interface {
// Traverse attempts to traverse the receiver using the given traverser.
Traverse(t hcl.Traverser) (Traversable, hcl.Diagnostics)
}
// TypedTraversable is a Traversable that has an associated type.
type TypedTraversable interface {
Traversable
Type() Type
}
// ValueTraversable is a Traversable that has an associated value.
type ValueTraversable interface {
Traversable
Value(context *hcl.EvalContext) (cty.Value, hcl.Diagnostics)
}
// GetTraversableType returns the type of the given Traversable:
// - If the Traversable is a TypedTraversable, this returns t.Type()
// - If the Traversable is a Type, this returns t
// - Otherwise, this returns DynamicType
func GetTraversableType(t Traversable) Type {
switch t := t.(type) {
case TypedTraversable:
return t.Type()
case Type:
return t
default:
return DynamicType
}
}
// GetTraverserKey extracts the value and type of the key associated with the given traverser.
func GetTraverserKey(t hcl.Traverser) (cty.Value, Type) {
switch t := t.(type) {
case hcl.TraverseAttr:
return cty.StringVal(t.Name), StringType
case hcl.TraverseIndex:
if t.Key.Type().Equals(typeCapsule) {
return cty.DynamicVal, *(t.Key.EncapsulatedValue().(*Type))
}
return t.Key, ctyTypeToType(t.Key.Type(), false)
default:
contract.Failf("unexpected traverser of type %T (%v)", t, t.SourceRange())
return cty.DynamicVal, DynamicType
}
}
// bindTraversalParts computes the type for each element of the given traversal.
func bindTraversalParts(receiver Traversable, traversal hcl.Traversal,
all: Reformat with gofumpt Per team discussion, switching to gofumpt. [gofumpt][1] is an alternative, stricter alternative to gofmt. It addresses other stylistic concerns that gofmt doesn't yet cover. [1]: https://github.com/mvdan/gofumpt See the full list of [Added rules][2], but it includes: - Dropping empty lines around function bodies - Dropping unnecessary variable grouping when there's only one variable - Ensuring an empty line between multi-line functions - simplification (`-s` in gofmt) is always enabled - Ensuring multi-line function signatures end with `) {` on a separate line. [2]: https://github.com/mvdan/gofumpt#Added-rules gofumpt is stricter, but there's no lock-in. All gofumpt output is valid gofmt output, so if we decide we don't like it, it's easy to switch back without any code changes. gofumpt support is built into the tooling we use for development so this won't change development workflows. - golangci-lint includes a gofumpt check (enabled in this PR) - gopls, the LSP for Go, includes a gofumpt option (see [installation instrutions][3]) [3]: https://github.com/mvdan/gofumpt#installation This change was generated by running: ```bash gofumpt -w $(rg --files -g '*.go' | rg -v testdata | rg -v compilation_error) ``` The following files were manually tweaked afterwards: - pkg/cmd/pulumi/stack_change_secrets_provider.go: one of the lines overflowed and had comments in an inconvenient place - pkg/cmd/pulumi/destroy.go: `var x T = y` where `T` wasn't necessary - pkg/cmd/pulumi/policy_new.go: long line because of error message - pkg/backend/snapshot_test.go: long line trying to assign three variables in the same assignment I have included mention of gofumpt in the CONTRIBUTING.md.
2023-03-03 16:36:39 +00:00
allowMissingVariables bool,
) ([]Traversable, hcl.Diagnostics) {
parts := make([]Traversable, len(traversal)+1)
parts[0] = receiver
var diagnostics hcl.Diagnostics
for i, part := range traversal {
nextReceiver, partDiags := parts[i].Traverse(part)
// TODO(pdg): proper options for Traverse
if allowMissingVariables {
var diags hcl.Diagnostics
for _, d := range partDiags {
if !strings.HasPrefix(d.Summary, "undefined variable") {
diags = append(diags, d)
}
}
partDiags = diags
}
parts[i+1], diagnostics = nextReceiver, append(diagnostics, partDiags...)
}
switch parts[len(parts)-1].(type) {
case TypedTraversable, Type:
// OK
default:
// TODO(pdg): improve this diagnostic
diagnostics = append(diagnostics, undefinedVariable("",
traversal.SourceRange(), allowMissingVariables))
}
return parts, diagnostics
}