Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion of map to object #36256

Closed
aniketpant1 opened this issue Jan 2, 2025 · 4 comments
Closed

Conversion of map to object #36256

aniketpant1 opened this issue Jan 2, 2025 · 4 comments
Labels
documentation new new issue not yet triaged

Comments

@aniketpant1
Copy link

aniketpant1 commented Jan 2, 2025

Terraform Version

Terraform v1.10.1

Affected Pages

https://developer.hashicorp.com/terraform/language/expressions/type-constraints#conversion-of-complex-types

What is the docs issue?

Mentioning in the documentation map -> object map conversions can be lossy.

Proposal

It is a confusing as i haven't find any example on the internet and on the documentation where map to object conversion can be failed or lossy. Even though we should have a object function where it can convert map to object. If we have simple example on this . It would be easier for the users also.

References

No response

@aniketpant1 aniketpant1 added documentation new new issue not yet triaged labels Jan 2, 2025
@crw
Copy link
Contributor

crw commented Jan 2, 2025

Thanks for this request.

@aniketpant1
Copy link
Author

May i know the dates ? Or anyone who can help me by giving example in this thread

@liamcervante
Copy link
Member

Hi @aniketpant1, here's a potential example of a lossy conversion from map to object.

# ./main.tf

variable "map" {
    type = map(string)
    default = {
        a = "a"
        b = "b"
        c = "c"
    }
}

module "mod" {
    source = "./module"

    object = var.map
}

output "map" {
    value = var.map
}

output "object" {
    value = module.mod.object
}
# ./module/main.tf

variable "object" {
  type = object({
    a = string
    b = string
  })
}

output "object" {
  value = var.object
}

If you execute the above Terraform files, you can see from the outputs that the c value from the map has been automatically stripped from the returned object. This is the conversion being lossy - as values from the map have been silently removed in order to make the type match the type required by the object variable.

map = tomap({
  "a" = "a"
  "b" = "b"
  "c" = "c"
})
object = {
  "a" = "a"
  "b" = "b"
}

Hope this helps!

@aniketpant1
Copy link
Author

Thank You @liamcervante .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation new new issue not yet triaged
Projects
None yet
Development

No branches or pull requests

3 participants