Skip to content
This repository was archived by the owner on Jun 27, 2025. It is now read-only.

Conversation

@jsipprell
Copy link

When using levant with a common template but different vars files, it would be useful to be able to test for the existence of a key in a map. This PR adds an exists template function to test for key existence without failing due to the template missingkey option being set.

Example usage:

job "foo" {
[[ if exists . "constraint" ]]
  constraint {
    attribute = "${[[ .constraint.attribute ]]}"
    value = "[[ .constraint.value ]]"
  }
[[- end ]]

  ...
}

@jsipprell
Copy link
Author

jsipprell commented Feb 26, 2019

Addendum: I recognize this is possible by using the index function, but this may not be obvious or trivial to someone unfamiliar with go templating. (Plus, an index test wouldn't actually test for key existence, but rather whether the value is non-zero)

@jrasell
Copy link
Member

jrasell commented Feb 27, 2019

Hi @jsipprell thanks a lot for this addition, I really like it. I am curious if the name exists is too broad here, and potentially mapExists or similar might be better as the function is scoped to maps only? Also if you have the time, could you add a quick docs update to include this func also

@jrasell jrasell self-assigned this Feb 27, 2019
@jrasell jrasell self-requested a review February 27, 2019 08:08
@jsipprell
Copy link
Author

Sure, I can update the docs.

How about varExists? To me, that seems to map on to the idioms levant uses and indicates intuitively that one is testing for the existence of a variable (which would be the common usage).

@hashicorp-cla
Copy link

hashicorp-cla commented Jul 26, 2020

CLA assistant check

Thank you for your submission! We require that all contributors sign our Contributor License Agreement ("CLA") before we can accept the contribution. Read and sign the agreement

Learn more about why HashiCorp requires a CLA and what the CLA includes


0 out of 2 committers have signed the CLA.

  • Jesse Sipprell
  • jsipprell

Jesse Sipprell seems not to be a GitHub user.
You need a GitHub account to be able to sign the CLA.
If you have already a GitHub account, please add the email address used for this commit to your account.

Have you signed the CLA already but the status is still pending? Recheck it.

@angrycub
Copy link
Contributor

angrycub commented Nov 9, 2020

@jrasell Since the sprig functions are merged into levant, would this be a case that is now covered by sprig's hasKey function?

@jrasell jrasell added theme/template Relates to templating in general type/enhancement and removed enhancement labels Nov 27, 2020
Base automatically changed from master to main March 3, 2021 19:50
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

theme/template Relates to templating in general type/enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants