mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
39e609d5fd
Previously we were using the experimental HCL 2 repository, but now we'll shift over to the v2 import path within the main HCL repository as part of actually releasing HCL 2.0 as stable. This is a mechanical search/replace to the new import paths. It also switches to the v2.0.0 release of HCL, which includes some new code that Terraform didn't previously have but should not change any behavior that matters for Terraform's purposes. For the moment the experimental HCL2 repository is still an indirect dependency via terraform-config-inspect, so it remains in our go.sum and vendor directories for the moment. Because terraform-config-inspect uses a much smaller subset of the HCL2 functionality, this does still manage to prune the vendor directory a little. A subsequent release of terraform-config-inspect should allow us to completely remove that old repository in a future commit.
56 lines
1.5 KiB
Go
56 lines
1.5 KiB
Go
package configs
|
|
|
|
import (
|
|
"github.com/hashicorp/hcl/v2"
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
|
"github.com/zclconf/go-cty/cty"
|
|
)
|
|
|
|
// Backend represents a "backend" block inside a "terraform" block in a module
|
|
// or file.
|
|
type Backend struct {
|
|
Type string
|
|
Config hcl.Body
|
|
|
|
TypeRange hcl.Range
|
|
DeclRange hcl.Range
|
|
}
|
|
|
|
func decodeBackendBlock(block *hcl.Block) (*Backend, hcl.Diagnostics) {
|
|
return &Backend{
|
|
Type: block.Labels[0],
|
|
TypeRange: block.LabelRanges[0],
|
|
Config: block.Body,
|
|
DeclRange: block.DefRange,
|
|
}, nil
|
|
}
|
|
|
|
// Hash produces a hash value for the reciever that covers the type and the
|
|
// portions of the config that conform to the given schema.
|
|
//
|
|
// If the config does not conform to the schema then the result is not
|
|
// meaningful for comparison since it will be based on an incomplete result.
|
|
//
|
|
// As an exception, required attributes in the schema are treated as optional
|
|
// for the purpose of hashing, so that an incomplete configuration can still
|
|
// be hashed. Other errors, such as extraneous attributes, have no such special
|
|
// case.
|
|
func (b *Backend) Hash(schema *configschema.Block) int {
|
|
// Don't fail if required attributes are not set. Instead, we'll just
|
|
// hash them as nulls.
|
|
schema = schema.NoneRequired()
|
|
spec := schema.DecoderSpec()
|
|
val, _ := hcldec.Decode(b.Config, spec, nil)
|
|
if val == cty.NilVal {
|
|
val = cty.UnknownVal(schema.ImpliedType())
|
|
}
|
|
|
|
toHash := cty.TupleVal([]cty.Value{
|
|
cty.StringVal(b.Type),
|
|
val,
|
|
})
|
|
|
|
return toHash.Hash()
|
|
}
|