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

or does not test possibly-undefined data values #836

Open
CJKay opened this issue May 18, 2023 · 3 comments
Open

or does not test possibly-undefined data values #836

CJKay opened this issue May 18, 2023 · 3 comments
Assignees
Labels
carvel accepted This issue should be considered for future work and that the triage process has been completed discussion This issue is not a bug or feature and a conversation is needed to find an appropriate resolution documentation This issue indicates a change to the docs should be considered

Comments

@CJKay
Copy link

CJKay commented May 18, 2023

What steps did you take:

I wrote this snippet into a new file xyz.yaml:

#@ load("@ytt:data", "data")

#@ foo = data.values.foo or "bar"

What happened:

When running ytt -f xyz.yaml, the following error is generated:

ytt: Error: 
- struct has no .foo field or method
    in <toplevel>
      template.yaml:3 | #@ foo = data.values.foo or "bar"

What did you expect:

I expected templating to complete successfully and generate an empty YAML file, per the FAQ:

How do I provide a default for a data value when it may not be defined?

When a value may be null, you can use or to specify a default.

#@ data.values.foo or "bar"

~ https://carvel.dev/ytt/docs/v0.38.0/faq/#how-do-i-provide-a-default-for-a-data-value-when-it-may-not-be-defined

Environment:

  • ytt version (use ytt --version): 0.45.1
  • OS (e.g. from /etc/os-release): macOS (via brew install)

Vote on this request

This is an invitation to the community to vote on issues, to help us prioritize our backlog. Use the "smiley face" up to the right of this comment to vote.

👍 "I would like to see this addressed as soon as possible"
👎 "There are other more important things to focus on right now"

We are also happy to receive and review Pull Requests if you want to help working on this issue.

@CJKay CJKay added bug This issue describes a defect or unexpected behavior carvel triage This issue has not yet been triaged for relevance labels May 18, 2023
@vmunishwar vmunishwar self-assigned this Jun 3, 2023
@vmunishwar vmunishwar removed the carvel triage This issue has not yet been triaged for relevance label Jun 3, 2023
@vmunishwar
Copy link
Contributor

vmunishwar commented Jun 5, 2023

@CJKay Thanks for reaching out.

This seems to be working as expected. When the data values file has 'foo' as empty or null, the foo gets value as bar in the output template. Please check few examples run on the ytt playground..
Could you please elaborate your use case if that does not fit with the use of 'or' as shown in the examples? Thanks

image image image

@vmunishwar vmunishwar added discussion This issue is not a bug or feature and a conversation is needed to find an appropriate resolution and removed bug This issue describes a defect or unexpected behavior labels Jun 5, 2023
@CJKay
Copy link
Author

CJKay commented Jun 6, 2023

Hi @vmunishwar, here is an example in the playground: https://carvel.dev/ytt/#gist:https://gist.github.com/CJKay/3a5944330d82c1e04a86d1afa5a4282c

In this example no foo data value has been provided. I therefore expected the value of the foo variable to evaluate to bar, but it instead triggers an error as the data value is not defined. It's not clear from the documentation that it is necessary to provide a definition for foo (if that is indeed the expectation), as the title for the relevant FAQ entry is:

How do I provide a default for a data value when it may not be defined?

Either there is a bug in the implementation (or must test that the left value is defined), or there is a bug in the documentation:

How do I provide a default for a data value when it may not be defined may be null?

@vmunishwar
Copy link
Contributor

vmunishwar commented Jun 8, 2023

Hi @CJKay
Looks like this is a documentation issue. Thanks for pointing it out. We will take your inputs and try to change the docs.

For now, just to be clear, when we load a data module using #@ load("@ytt:data","data"), the data values file is used to refer to the data values and data.values is a struct supplied by the @ytt:data module.
Though, we do not explicitly throw error if it is not present. So, the answer in FAQ docs `#@ data.values.foo or "bar"' refers to the use of 'or' keyword (assuming the data values file has foo as a key but not set with any default value). For complete examples please also refer to the ytt playground

Meanwhile, if you still need help kindly share your use case to use the default value for undefined data values.
Thanks again for reaching out.

@vmunishwar vmunishwar added documentation This issue indicates a change to the docs should be considered good first issue An issue that will be a good candidate for a new contributor carvel accepted This issue should be considered for future work and that the triage process has been completed and removed good first issue An issue that will be a good candidate for a new contributor labels Jun 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
carvel accepted This issue should be considered for future work and that the triage process has been completed discussion This issue is not a bug or feature and a conversation is needed to find an appropriate resolution documentation This issue indicates a change to the docs should be considered
Projects
Status: No status
Development

No branches or pull requests

2 participants