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

fix: kindIs function documentation #1653

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

TerryHowe
Copy link
Contributor

The kindIs function does not work as documented because the underlying library used for parsing stores number values as float64

Closes: #1532

@@ -2016,10 +2016,10 @@ The above would return `string`. For simple tests (like in `if` blocks), the
`kindIs` function will let you verify that a value is a particular kind:

```
kindIs "int" 123
kindIs "float64" 123
Copy link
Contributor

Choose a reason for hiding this comment

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

This is more complicated than it seems. Even if I go back to the issue this is about.

The following will return true in a template.

kindIs "int" 123

See https://go.dev/play/p/kUCfwEQzeoh for an example that shows exactly this.

The problem in the original one is that 123 in the values.yaml file is parsed by the k8s yaml parser. The spec has data types for both integers and floating point numbers. But, the Kubernetes YAML parser turns any number into a float64. The following is a comment from that package:

// - This decodes any number (although it is an integer) into a float64 if the type of obj is unknown, e.g. *map[string]interface{}, *interface{}, or *[]interface{}. This means integers above +/- 2^53 will lose precision when round-tripping. Make a JSONOpt that calls d.UseNumber() to avoid this.

If you use an integer right in the template it will return true. If you use a number passed from the values.yaml file it will need to be a float64.

Is there a way we can make this more clear?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated PR.

@andrask
Copy link

andrask commented Dec 16, 2024

Instead of highlighting one example that's inconsistent with the expectations, I think it would be better to add details about how such errors can be debugged. Or even better, warn to not use the kindIs at all, and instead throw errors when a conversion / assertion fails. Failing fast with any kind of configuration errors is the best strategy anyway.

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

Successfully merging this pull request may close these issues.

kindIs returns false when documentation says it should return true
3 participants