hcl2shim: Split out legacy subset

Due to some past confusion about the purpose of this package, it has grown
to include a confusing mix of currently-viable code and legacy support
code from the move to HCL 2. This has in turn caused confusion about which
parts of this package _should_ be used for new code.

To help clarify that distinction we'll move the legacy support code into
a package under the "legacy" directory, which is also where most of its
callers live.

There are unfortunately still some callers to these outside of the legacy
tree, but the vast majority are either old tests written before HCL 2
adoption or helper code used only by those tests. The one dubious exception
is the use in ResourceInstanceObjectSrc.Decode, which makes a best effort
to shim flatmap as a concession to the fact that not all state-loading
codepaths are able to run the provider state upgrade function that would
normally be responsible for the flatmap-to-JSON conversion, which is
explained in a new comment inline.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
This commit is contained in:
Martin Atkins
2025-07-09 11:26:31 -07:00
parent ee357fe530
commit 868dc2f01b
35 changed files with 62 additions and 32 deletions

View File

@@ -17,8 +17,8 @@ import (
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/aliyun/aliyun-tablestore-go-sdk/tablestore"
"github.com/opentofu/opentofu/internal/backend"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/encryption"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
)
// verify that we are doing ACC tests or the OSS tests specifically

View File

@@ -17,8 +17,8 @@ import (
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/hashicorp/aws-sdk-go-base/v2/mockdata"
"github.com/hashicorp/aws-sdk-go-base/v2/servicemocks"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/encryption"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
"github.com/opentofu/opentofu/internal/tfdiags"
)

View File

@@ -30,8 +30,8 @@ import (
"github.com/opentofu/opentofu/internal/backend"
"github.com/opentofu/opentofu/internal/configs/configschema"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/encryption"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
"github.com/opentofu/opentofu/internal/states"
"github.com/opentofu/opentofu/internal/states/remote"
"github.com/opentofu/opentofu/internal/tfdiags"

View File

@@ -17,7 +17,7 @@ import (
"github.com/opentofu/opentofu/internal/addrs"
"github.com/opentofu/opentofu/internal/configs"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
"github.com/opentofu/opentofu/internal/states"
"github.com/opentofu/opentofu/internal/states/statemgr"
"github.com/opentofu/opentofu/internal/tfdiags"

View File

@@ -21,7 +21,7 @@ import (
ctyjson "github.com/zclconf/go-cty/cty/json"
"github.com/opentofu/opentofu/internal/command/cliconfig/svcauthconfig"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
pluginDiscovery "github.com/opentofu/opentofu/internal/plugin/discovery"
"github.com/opentofu/opentofu/internal/replacefile"
)

View File

@@ -0,0 +1,18 @@
// Copyright (c) The OpenTofu Authors
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2023 HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
// Package hcl2shim contains a small number of "shimming" utilities that the
// other packages under internal/legacy use to adapt from HCL 2 concepts to
// legacy concepts.
//
// It's unfortunately also used in support of some very old tests in non-legacy
// packages that were originally written against the legacy packages, which are
// preserved in their current form for now to ensure that they keep testing what
// they were originally intended to test, but will hopefully be gradually
// modernized over time as they get updated for other reasons.
//
// Nothing in this package should be used in new code. It's here only to keep
// the other legacy code working.
package hcl2shim

View File

@@ -13,7 +13,7 @@ import (
"github.com/zclconf/go-cty/cty"
"github.com/opentofu/opentofu/internal/configs/configschema"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/tofu"
ctyconvert "github.com/zclconf/go-cty/cty/convert"
)

View File

@@ -11,7 +11,7 @@ import (
"reflect"
"testing"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/helper/hashcode"
"github.com/opentofu/opentofu/internal/legacy/tofu"
)

View File

@@ -12,7 +12,7 @@ import (
"testing"
"github.com/davecgh/go-spew/spew"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/tofu"
)

View File

@@ -14,7 +14,7 @@ import (
"time"
"github.com/google/go-cmp/cmp"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/tofu"
"github.com/zclconf/go-cty/cty"

View File

@@ -11,7 +11,7 @@ import (
"time"
"github.com/mitchellh/copystructure"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/tofu"
)

View File

@@ -29,7 +29,7 @@ import (
"github.com/go-viper/mapstructure/v2"
"github.com/mitchellh/copystructure"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/tofu"
)

View File

@@ -15,7 +15,7 @@ import (
"strings"
"testing"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/helper/hashcode"
"github.com/opentofu/opentofu/internal/legacy/tofu"
)

View File

@@ -12,7 +12,7 @@ import (
ctyjson "github.com/zclconf/go-cty/cty/json"
"github.com/opentofu/opentofu/internal/configs/configschema"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/tofu"
)

View File

@@ -17,7 +17,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/opentofu/opentofu/internal/configs/configschema"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/helper/hashcode"
"github.com/opentofu/opentofu/internal/legacy/tofu"
"github.com/opentofu/opentofu/internal/providers"

View File

@@ -19,7 +19,7 @@ import (
"github.com/opentofu/opentofu/internal/addrs"
"github.com/opentofu/opentofu/internal/configs/configschema"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
"github.com/zclconf/go-cty/cty"
"github.com/mitchellh/copystructure"

View File

@@ -18,7 +18,7 @@ import (
"github.com/opentofu/opentofu/internal/addrs"
"github.com/opentofu/opentofu/internal/configs/configschema"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
)
// Resource is a legacy way to identify a particular resource instance.

View File

@@ -10,11 +10,11 @@ import (
"reflect"
"testing"
"github.com/opentofu/opentofu/internal/configs/configschema"
"github.com/mitchellh/reflectwalk"
"github.com/zclconf/go-cty/cty"
"github.com/mitchellh/reflectwalk"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/configs/configschema"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
)
func TestResourceConfigGet(t *testing.T) {

View File

@@ -30,7 +30,7 @@ import (
"github.com/opentofu/opentofu/internal/addrs"
"github.com/opentofu/opentofu/internal/configs"
"github.com/opentofu/opentofu/internal/configs/configschema"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
"github.com/opentofu/opentofu/internal/plans"
"github.com/opentofu/opentofu/internal/tfdiags"
tfversion "github.com/opentofu/opentofu/version"

View File

@@ -17,7 +17,7 @@ import (
"github.com/davecgh/go-spew/spew"
"github.com/opentofu/opentofu/internal/addrs"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
)
func TestStateValidate(t *testing.T) {

View File

@@ -16,7 +16,7 @@ import (
"go.uber.org/mock/gomock"
"github.com/opentofu/opentofu/internal/addrs"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
mockproto "github.com/opentofu/opentofu/internal/plugin/mock_proto"
"github.com/opentofu/opentofu/internal/providers"
"github.com/opentofu/opentofu/internal/tfdiags"

View File

@@ -11,7 +11,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
"github.com/opentofu/opentofu/internal/provisioners"
proto "github.com/opentofu/opentofu/internal/tfplugin5"
"github.com/zclconf/go-cty/cty"

View File

@@ -17,7 +17,7 @@ import (
"go.uber.org/mock/gomock"
"github.com/opentofu/opentofu/internal/addrs"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
mockproto "github.com/opentofu/opentofu/internal/plugin6/mock_proto"
"github.com/opentofu/opentofu/internal/providers"
"github.com/opentofu/opentofu/internal/tfdiags"

View File

@@ -10,7 +10,7 @@ import (
ctyjson "github.com/zclconf/go-cty/cty/json"
"github.com/opentofu/opentofu/internal/addrs"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
)
// ResourceInstanceObjectSrc is a not-fully-decoded version of
@@ -86,6 +86,14 @@ func (os *ResourceInstanceObjectSrc) Decode(ty cty.Type) (*ResourceInstanceObjec
var err error
if os.AttrsFlat != nil {
// Legacy mode. We'll do our best to unpick this from the flatmap.
//
// Note that we can only get here in unusual cases like when running
// "tofu show" or "tofu console" against a very old state snapshot
// created with Terraform v0.11 or earlier; in the normal plan/apply
// path we use the provider function "UpgradeResourceState" to ask
// the _provider_ to translate from flatmap to JSON, which can therefore
// give better results because the provider can have awareness of its
// own legacy encoding quirks.
val, err = hcl2shim.HCL2ValueFromFlatmap(os.AttrsFlat, ty)
if err != nil {
return nil, err

View File

@@ -11,7 +11,7 @@ import (
ctyjson "github.com/zclconf/go-cty/cty/json"
"github.com/opentofu/opentofu/internal/addrs"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
)
// String returns a rather-odd string representation of the entire state.
@@ -215,6 +215,10 @@ func (ms *Module) testString() string {
for _, k := range ks {
v := ms.OutputValues[k]
// This uses the legacy configuration mapping because the string
// representation of state is primarily used by very old tests
// that were originally written in the era where this was the
// main config representation.
lv := hcl2shim.ConfigValueFromHCL2(v.Value)
switch vTyped := lv.(type) {
case string:

View File

@@ -30,8 +30,8 @@ import (
"github.com/opentofu/opentofu/internal/addrs"
"github.com/opentofu/opentofu/internal/configs"
"github.com/opentofu/opentofu/internal/configs/configschema"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/lang/marks"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
"github.com/opentofu/opentofu/internal/plans"
"github.com/opentofu/opentofu/internal/providers"
"github.com/opentofu/opentofu/internal/provisioners"

View File

@@ -25,8 +25,8 @@ import (
"github.com/opentofu/opentofu/internal/addrs"
"github.com/opentofu/opentofu/internal/configs/configschema"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/lang/marks"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
"github.com/opentofu/opentofu/internal/plans"
"github.com/opentofu/opentofu/internal/providers"
"github.com/opentofu/opentofu/internal/provisioners"

View File

@@ -18,7 +18,7 @@ import (
"github.com/opentofu/opentofu/internal/addrs"
"github.com/opentofu/opentofu/internal/configs/configschema"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
"github.com/opentofu/opentofu/internal/plans"
"github.com/opentofu/opentofu/internal/providers"
"github.com/opentofu/opentofu/internal/states"

View File

@@ -25,8 +25,8 @@ import (
"github.com/opentofu/opentofu/internal/configs"
"github.com/opentofu/opentofu/internal/configs/configload"
"github.com/opentofu/opentofu/internal/configs/configschema"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/encryption"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
"github.com/opentofu/opentofu/internal/plans"
"github.com/opentofu/opentofu/internal/plans/planfile"
"github.com/opentofu/opentofu/internal/providers"

View File

@@ -16,7 +16,7 @@ import (
"github.com/opentofu/opentofu/internal/addrs"
"github.com/opentofu/opentofu/internal/configs"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
"github.com/opentofu/opentofu/internal/states"
)

View File

@@ -14,7 +14,7 @@ import (
ctyjson "github.com/zclconf/go-cty/cty/json"
"github.com/zclconf/go-cty/cty/msgpack"
"github.com/opentofu/opentofu/internal/configs/hcl2shim"
"github.com/opentofu/opentofu/internal/legacy/hcl2shim"
"github.com/opentofu/opentofu/internal/providers"
"github.com/opentofu/opentofu/internal/tracing"
)