Skip to content

Commit

Permalink
Merge 141a604 into 7c06385
Browse files Browse the repository at this point in the history
  • Loading branch information
justinvp committed May 1, 2023
2 parents 7c06385 + 141a604 commit 872e94a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
secondMember = "hello"
}]
}
config "objectListConfigEmpty" "list(object({firstMember=number, secondMember=string}))" {
default = []
}
config "objectMapConfig" "map(object({firstMember=number, secondMember=string}))" {
default = {
hello = {
Expand All @@ -18,6 +21,9 @@
}
}
}
config "objectMapConfigEmpty" "map(object({firstMember=number, secondMember=string}))" {
default = {}
}
resource "usingSimpleObjectConfig" "simple:index:resource" {
inputOne = simpleObjectConfig.firstMember
}
Expand Down
18 changes: 18 additions & 0 deletions pkg/tf2pulumi/convert/testdata/object_config_rename/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ variable "object_list_config" {
}]
}

variable "object_list_config_empty" {
type = list(object({
first_member = number,
second_member = string
}))

default = []
}

variable "object_map_config" {
type = map(object({
first_member = number,
Expand All @@ -38,6 +47,15 @@ variable "object_map_config" {
}
}

variable "object_map_config_empty" {
type = map(object({
first_member = number,
second_member = string
}))

default = {}
}

resource "simple_resource" "using_simple_object_config" {
input_one = var.simple_object_config.first_member
}
Expand Down
15 changes: 10 additions & 5 deletions pkg/tf2pulumi/convert/tf.go
Original file line number Diff line number Diff line change
Expand Up @@ -1425,8 +1425,13 @@ func convertBody(sources map[string][]byte, scopes *scopes, fullyQualifiedPath s
// camelCaseObjectAttributes rewrites the attributes of objects to camelCase and returns the modified value.
// when the input is a list of objects or map of objects, those are modified recursively.
func camelCaseObjectAttributes(value cty.Value) cty.Value {
// if the value is null, return as is
if value.IsNull() {
return value
}

// handle type object({...})
if value.Type().IsObjectType() {
if value.Type().IsObjectType() && value.LengthInt() > 0 {
modifiedAttributes := map[string]cty.Value{}
for propertyKey, propertyValue := range value.AsValueMap() {
modifiedValue := camelCaseObjectAttributes(propertyValue)
Expand All @@ -1436,7 +1441,7 @@ func camelCaseObjectAttributes(value cty.Value) cty.Value {
}

// handle type list(...)
if value.Type().IsListType() {
if value.Type().IsListType() && value.LengthInt() > 0 {
modifiedValues := make([]cty.Value, value.LengthInt())
for index, element := range value.AsValueSlice() {
modifiedValues[index] = camelCaseObjectAttributes(element)
Expand All @@ -1447,7 +1452,7 @@ func camelCaseObjectAttributes(value cty.Value) cty.Value {

// handle set(...) and convert it to list(...)
// because we simplify sets to lists
if value.Type().IsSetType() {
if value.Type().IsSetType() && value.LengthInt() > 0 {
modifiedValues := make([]cty.Value, value.LengthInt())
for index, element := range value.AsValueSet().Values() {
modifiedValues[index] = camelCaseObjectAttributes(element)
Expand All @@ -1456,7 +1461,7 @@ func camelCaseObjectAttributes(value cty.Value) cty.Value {
return cty.ListVal(modifiedValues)
}

if value.Type().IsTupleType() {
if value.Type().IsTupleType() && value.LengthInt() > 0 {
tupleValues := make([]cty.Value, value.LengthInt())
for index, element := range value.AsValueSlice() {
tupleValues[index] = camelCaseObjectAttributes(element)
Expand All @@ -1466,7 +1471,7 @@ func camelCaseObjectAttributes(value cty.Value) cty.Value {
}

// handle type map(object({...}))
if value.Type().IsMapType() {
if value.Type().IsMapType() && value.LengthInt() > 0 {
modifiedAttributes := map[string]cty.Value{}
for propertyKey, propertyValue := range value.AsValueMap() {
modifiedValue := camelCaseObjectAttributes(propertyValue)
Expand Down

0 comments on commit 872e94a

Please sign in to comment.