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

empty list comparison does not work if the list contains an object #23562

Open
shanxops opened this issue Dec 4, 2019 · 6 comments
Open

empty list comparison does not work if the list contains an object #23562

shanxops opened this issue Dec 4, 2019 · 6 comments

Comments

@shanxops
Copy link

shanxops commented Dec 4, 2019

Terraform Version

Terraform v0.12.17

Terraform Configuration Files

Thanks @dpiddockcmp for a simpler example. #23562 (comment)

variable "object" {
  type    = list(object({ a = string }))
  default = [] 
}

output "equal" {
  value = var.object == [] ? "empty" : "not empty"
} 

output "not_equal" {
  value = var.object != [] ? "not empty" : "empty"
}

Expected Behavior

Outputs:
equal = empty
not_equal = empty

Actual Behavior

Outputs:
equal = not empty
not_equal = not empty

Steps to Reproduce

terraform init
terraform apply -auto-approve

Solution

variable "object" {
  type    = list(object({ a = string }))
  default = []
}

output "equal" {
  value = length(var.object) == 0 ? "empty" : "not empty"
}

output "not_equal" {
  value = length(var.object) != 0 ? "not empty" : "empty"
}

References

terraform-aws-modules/terraform-aws-eks#606 (comment)

@shanxops shanxops changed the title inconsistent behaviour of if block based on data type inconsistent behaviour of yamlencode inside if block based on data type Dec 4, 2019
@dpiddockcmp
Copy link

Hi, this is more generic that the reporter implies. Empty list comparison does not work if the list contains an object:

variable "object" {
  type    = list(object({ a = string }))
  default = [] 
}

output "equal" {
  value = var.object == [] ? "empty" : "not empty"
} 

output "not_equal" {
  value = var.object != [] ? "not empty" : "empty"
}
Outputs:
equal = not empty
not_equal = not empty

@shanxops
Copy link
Author

shanxops commented Dec 6, 2019

ok, i was testing only yamlencode, let me change the title. Updating my example with @dpiddockcmp as the his generic example is simple and clear

@shanxops shanxops changed the title inconsistent behaviour of yamlencode inside if block based on data type empty list comparison does not work if the list contains an object Dec 6, 2019
@jbardin jbardin self-assigned this Dec 9, 2019
@jbardin
Copy link
Member

jbardin commented Dec 10, 2019

Hi @shanmugakarna,

Thanks for filing the issue. The comparison here is failing because the equality operator first compares types.

The type of var.object here (using the internal representation) is cty.List(cty.Object(map[string]cty.Type{"a":cty.String})), while the [] literal is of type cty.EmptyTuple, which do not compare as equal.

The preferred way to check for an empty set or series of any type would be to check its length, rather than compare it against a literal value

value = length(var.object) == 0 ? "empty" : "not empty"

There are however some cases where comparison against a literal value may be quite useful. In these cases we will need to decide on and implement a limited set of automatic type conversion rules for comparing equality.

Related: #21978

@jbardin jbardin removed their assignment Dec 10, 2019
@shanxops
Copy link
Author

Hi @jbardin

Thank you. I already have the length solution as part of the issue description. So, if this behaviour is expected, then we can close the issue.

@pieterza
Copy link

List comparisons are broken regardless of it being type object or not, see: #29187

@md5
Copy link

md5 commented Mar 22, 2022

@jbardin would it make sense to update the title of this issue since it is broader than the title implies? The title of #29187 seems to describe the problem better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants