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

hcl2template: add alltrue & anytrue function #13237

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

mogrogan
Copy link
Contributor

@mogrogan mogrogan commented Dec 19, 2024

alltrue

Function that return true if all elements of the collection are true

image

manual tests

tested with packer console --config-type=hcl2

> alltrue([true, false])
false
> alltrue(["true", true])
true
> alltrue(["true", 123])
> Error: Invalid function argument

  on <console-input> line 1:
  (source code not available)

Invalid value for "list" parameter: element 1: bool required.

anytrue

Function that return true if at least 1 element is true

image

manual tests

tested with packer console --config-type=hcl2

> anytrue([true, false])
> true
>

> anytrue([true, true])
> true
>
>
> anytrue([false, false])
> false
>

> anytrue(["true"])
> true
> anytrue(["true", 123])
> Error: Invalid function argument

  on <console-input> line 1:
  (source code not available)

Invalid value for "list" parameter: element 1: bool required.

Closes #13073

@mogrogan mogrogan changed the title hcl2template: add alltrue function hcl2template: add alltrue & anytrue function Dec 19, 2024
@mogrogan mogrogan marked this pull request as ready for review December 19, 2024 20:16
@mogrogan mogrogan requested review from a team as code owners December 19, 2024 20:16
@mogrogan mogrogan requested a review from shrhim-des December 19, 2024 20:16
Copy link
Contributor

@lbajolet-hashicorp lbajolet-hashicorp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM, I left a couple comments mostly about the copyright/license header, and left one question on the implementation of any_true, but besides those we're good to go.

Pre-approving this one

hcl2template/function/alltrue.go Show resolved Hide resolved
if !v.IsKnown() {
return cty.UnknownVal(cty.Bool), nil
}
if v.IsNull() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we cannot declare the function as not accepting null/unknown? Though this might make sense to support them and return false immediately in that case so please feel free to push back on this comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that user of this function have good chance of being using terraform and I think we should keep consistency with the terraform project to reduce the risk of having confusing error.

}
}
if hasUnknown {
return cty.UnknownVal(cty.Bool), nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this behaviour was lifted from Terraform, but I find this somewhat debatable? Since the function is called AnyTrue, if one value is effectively true, I don't think we should care about one value being unknown in there

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you, when I took that from terraform I found it a bit weird but I think the consistency between packer and terraform is more important unfortunately.

add an hcl2 function that return true if all the value in a collection
are true, this function was derived from terraform codebase
this function add the hcl2 anytrue function which takes a collection and
return true if any of the element is true.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Please add support to alltrue function
2 participants