Skip to content

Commit

Permalink
Merge d7f1d5b into backport/liamcervante/35752/gently-bright-eagle
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Oct 14, 2024
2 parents 34ce5a4 + d7f1d5b commit 39a6471
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 8 deletions.
10 changes: 2 additions & 8 deletions internal/genconfig/generate_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ package genconfig
import (
"encoding/json"
"fmt"
"regexp"
"sort"
"strings"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/zclconf/go-cty/cty"
ctyjson "github.com/zclconf/go-cty/cty/json"
Expand All @@ -20,12 +20,6 @@ import (
"github.com/hashicorp/terraform/internal/tfdiags"
)

var (
// whitespace is a regular expression that matches one or more whitespace
// characters.
whitespace = regexp.MustCompile(`\s+`)
)

// GenerateResourceContents generates HCL configuration code for the provided
// resource and state value.
//
Expand Down Expand Up @@ -592,7 +586,7 @@ func ctyCollectionValues(val cty.Value) []cty.Value {
func hclEscapeString(str string) string {
// TODO: Replace this with more complete HCL logic instead of the simple
// go workaround.
if whitespace.MatchString(str) {
if !hclsyntax.ValidIdentifier(str) {
return fmt.Sprintf("%q", str)
}
return str
Expand Down
88 changes: 88 additions & 0 deletions internal/genconfig/generate_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,94 @@ resource "tfcoremock_sensitive_values" "values" {
value = "underscores"
}
}
}`,
},
"simple_map_with_periods_in_keys": {
schema: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"map": {
Type: cty.Map(cty.String),
Optional: true,
},
},
},
addr: addrs.AbsResourceInstance{
Module: addrs.RootModuleInstance,
Resource: addrs.ResourceInstance{
Resource: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "testing_resource",
Name: "resource",
},
Key: addrs.NoKey,
},
},
provider: addrs.LocalProviderConfig{
LocalName: "testing",
},
value: cty.ObjectVal(map[string]cty.Value{
"map": cty.MapVal(map[string]cty.Value{
"key.with.periods": cty.StringVal("periods"),
"key_with_underscores": cty.StringVal("underscores"),
}),
}),
expected: `resource "testing_resource" "resource" {
map = {
"key.with.periods" = "periods"
key_with_underscores = "underscores"
}
}`,
},
"nested_map_with_periods_in_keys": {
schema: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"map": {
NestedType: &configschema.Object{
Attributes: map[string]*configschema.Attribute{
"value": {
Type: cty.String,
Optional: true,
},
},
Nesting: configschema.NestingMap,
},
Optional: true,
},
},
},
addr: addrs.AbsResourceInstance{
Module: addrs.RootModuleInstance,
Resource: addrs.ResourceInstance{
Resource: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "testing_resource",
Name: "resource",
},
Key: addrs.NoKey,
},
},
provider: addrs.LocalProviderConfig{
LocalName: "testing",
},
value: cty.ObjectVal(map[string]cty.Value{
"map": cty.MapVal(map[string]cty.Value{
"key.with.periods": cty.ObjectVal(map[string]cty.Value{
"value": cty.StringVal("periods"),
}),
"key_with_underscores": cty.ObjectVal(map[string]cty.Value{
"value": cty.StringVal("underscores"),
}),
}),
}),
expected: `resource "testing_resource" "resource" {
map = {
"key.with.periods" = {
value = "periods"
}
key_with_underscores = {
value = "underscores"
}
}
}`,
},
}
Expand Down

0 comments on commit 39a6471

Please sign in to comment.