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

feat: add yaml to/from functions #360

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

Conversation

blakepettersson
Copy link

In the same vein of #223, add YAML conversion functions. Fixes #358.

In the same vein of Masterminds#223, add YAML conversion functions. Fixes Masterminds#358.
defaults.go Outdated Show resolved Hide resolved
@yebolenko
Copy link

any update on this?

@alexbde
Copy link

alexbde commented Mar 21, 2023

@technosophos @mattfarina would you like to have a look at it? Seems to be a piece of cake 🍰 :)

@gabe565
Copy link

gabe565 commented Apr 17, 2023

I mistakenly opened a PR that did the same thing. I just closed it, but wanted to chime in to say I could really use these! Thanks for all the work you guys do 🙂

@@ -3,6 +3,7 @@ package sprig
import (
"bytes"
"encoding/json"
"gopkg.in/yaml.v3"
Copy link

@27149chen 27149chen May 16, 2023

Choose a reason for hiding this comment

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

it would be better to use "sigs.k8s.io/yaml" to supoort "omitempty" json tags

Choose a reason for hiding this comment

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

Better is a strong statement, just different.

Copy link

Choose a reason for hiding this comment

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

Could you elaborate why we would benefit from omitempty in this context?
We can't specify tags during the templating

Choose a reason for hiding this comment

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

AFAICT the Helm version of this doesn't specifically use omitempty either, so not sure whether that is really relevant. It does, however, make sense to use the same parser that Helm does, in case there are other differences in implementation.

(note that Helm uses a different yaml engine for toYamlPretty

@acelinkio
Copy link

Appreciate your work on this @blakepettersson, when you have a chance can you review the comments? Would love to see this integrated in! Awesome job!

Copy link

@olblak olblak left a comment

Choose a reason for hiding this comment

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

I would love to this pullrequest being merged and available in a release.
It has tests and documentation.

The code is trivial.l

}

// mustFromYaml decodes YAML into a structured value, returning errors.
func mustFromYaml(v string) (interface{}, error) {
Copy link

Choose a reason for hiding this comment

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

The use of 'must' typically implies that it will panic if the function fails. I understand this is the convention here, but it feels a bit weird.

Copy link
Author

Choose a reason for hiding this comment

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

Sure is, but as you said I'm just following the convention from elsewhere.

Copy link
Member

@mattfarina mattfarina left a comment

Choose a reason for hiding this comment

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

@blakepettersson Sorry for the slow response. This repo is maintained but we only pass by and act on occasion.

I would be interested in adding this functionality. But, the primary user of sprig is Helm and Helm already has two of the functions. We would want them to be compatible so that we could remove them from Helm and just use the sprig ones. You can find the Helm details at https://github.com/helm/helm/blob/main/pkg/engine/funcs.go.

Can you make them compatible?

@blakepettersson
Copy link
Author

@mattfarina in the case of toYaml it's pretty straightforward. My only concern is that this is inconsistent with how it's done with the existing tojson function.

Would it make sense to do the same with toJson?

In the case of fromYaml the same question arises; would it make sense to have an extra function in case a caller wants an Error map returned? Would it then make sense to have the same for the json unmarshling?

@zoni zoni mentioned this pull request Aug 26, 2023
@acelinkio
Copy link

@mattfarina , any chance you could review blacke's comments above? Would like to see this implemented as this would help a lot of downstream projects! Thanks!

@cornfeedhobo
Copy link

1 Year :(

42atomys added a commit to go-sprout/sprout that referenced this pull request May 16, 2024
## Description
We continue to backport features, bug fixes, and more from Sprig to
Sprout. This time, I'm focusing on the YAML functions part, used by Helm
and intended for use by others as well.

## Changes
- Add `fromYaml` function to convert yaml string into a go map
- Add `toYaml` function to convert any to a yaml string
- Add `mustFromYaml` function to convert and return error to the
template system when occurs
- Add `mustToYaml` function to convert and return error to the template
system when occurs

## Fixes Masterminds/sprig#358
Masterminds/sprig#360

## Checklist
- [x] I have read the **CONTRIBUTING.md** document.
- [x] My code follows the code style of this project.
- [x] I have added tests to cover my changes.
- [x] All new and existing tests passed.
- [ ] I have updated the documentation accordingly.
- [x] This change requires a change to the documentation on the website.

## Additional Information
The documentation for the encoding part of the library will be released
soon after this pull request on https://sprout.atom.codes

Initially implemented by @blakepettersson on sprig
@42atomys
Copy link

42atomys commented May 16, 2024

Hello everyone 👋,

I wanted to let you know that this issue has been addressed in the fork of this project at go-sprout/sprout. The fix has been implemented starting from version v0.4.0.

For those looking for a solution, I recommend checking out the latest releases of the fork. This should help address the issue discussed in this thread.

Thank you!

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.

Feature request: add toYaml/fromYaml functions