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

Add tests for jsonnet functions and fix parseYAML. #195

Merged
merged 5 commits into from
Feb 6, 2020

Conversation

mplzik
Copy link
Contributor

@mplzik mplzik commented Feb 6, 2020

  • funcs.go was missing tests for the jsonnet native functions, this PR
    adds some basic tests exercising some generic classses of arguments

  • parseYAML seems to have been doing one extra step of marshalling the
    YAML documents into JSON, which it probably shouldn't do and this PR
    removes the functionality.

W.r.t. second point, the current implementation looks like
manifestJsonFrom Yaml or similar -- is there any use case for that that
would be worth adding it?

- funcs.go was missing tests for the jsonnet native functions, this PR
adds some basic tests exercising some generic classses of arguments

- `parseYAML` seems to have been doing one extra step of marshalling the
YAML documents into JSON, which it probably shouldn't do and this PR
removes the functionality.

W.r.t. second point, the current implementation looks like
manifestJsonFrom Yaml or similar -- is there any use case for that that
would be worth adding it?

Signed-off-by: Milan Plzik <milan.plzik@grafana.com>
@mplzik
Copy link
Contributor Author

mplzik commented Feb 6, 2020

This should fix #194 .

@mplzik mplzik marked this pull request as ready for review February 6, 2020 11:36
@mplzik mplzik requested a review from sh0rez February 6, 2020 11:36
return nil, err
}
ret = append(ret, jsonDoc)
ret = append(ret, doc)

Choose a reason for hiding this comment

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

Wouldn't this result in problems further down the line? Jsonnet expects the result of a native function to comply with the standard Go representation, that is:

bool, for JSON booleans
float64, for JSON numbers
string, for JSON strings
[]interface{}, for JSON arrays
map[string]interface{}, for JSON objects
nil for JSON null

(source: https://golang.org/pkg/encoding/json/#Unmarshal)

Copy link
Member

Choose a reason for hiding this comment

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

yeah we alreaday noticed this

mplzik and others added 4 commits February 6, 2020 13:12
Signed-off-by: Milan Plzik <milan.plzik@grafana.com>
This normalizes YAML data to a form compatible with JSON -- that is,
`int` gets turned into `float64` etc.

Note: some cases this code still fails with:
```
evaluating jsonnet: unexpected end of JSON input
```

Interestingly enough, the same file (CRDs from cert-manager) fails when
used from a jsonnet library for cert-manager, but succeeds when loaded
from a dummy libsonnet file.

Signed-off-by: Milan Plzik <milan.plzik@grafana.com>
The `err` variable returned by `jsonnet.EvaluateFile` call was silently
ignored, making tanka fail with mysterious

```
evaluating jsonnet: unexpected end of JSON input
```

This commit makes the code properly handle errors. Thanks to @sh0rez for
a quick pointer.

Signed-off-by: Milan Plzik <milan.plzik@grafana.com>
Copy link
Member

@sh0rez sh0rez left a comment

Choose a reason for hiding this comment

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

LGTM

@sh0rez sh0rez merged commit bf763e1 into master Feb 6, 2020
@sh0rez sh0rez deleted the mplzik/fix-yaml-loading branch February 6, 2020 18:00
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.

3 participants