-
Notifications
You must be signed in to change notification settings - Fork 450
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package sprig | |
import ( | ||
"bytes" | ||
"encoding/json" | ||
"gopkg.in/yaml.v3" | ||
"math/rand" | ||
"reflect" | ||
"strings" | ||
|
@@ -153,6 +154,34 @@ func mustToRawJson(v interface{}) (string, error) { | |
return strings.TrimSuffix(buf.String(), "\n"), nil | ||
} | ||
|
||
// fromYaml decodes YAML into a structured value, ignoring errors. | ||
func fromYaml(v string) interface{} { | ||
output, _ := mustFromYaml(v) | ||
return output | ||
} | ||
|
||
// mustFromYaml decodes YAML into a structured value, returning errors. | ||
func mustFromYaml(v string) (interface{}, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
var output interface{} | ||
err := yaml.Unmarshal([]byte(v), &output) | ||
return output, err | ||
} | ||
|
||
// toYaml encodes an item into a YAML string | ||
func toYaml(v interface{}) string { | ||
output, _ := mustToYaml(v) | ||
return string(output) | ||
} | ||
|
||
// toYaml encodes an item into a YAML string, returning errors | ||
func mustToYaml(v interface{}) (string, error) { | ||
output, err := yaml.Marshal(v) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(output), nil | ||
} | ||
|
||
// ternary returns the first value if the last value is true, otherwise returns the second value. | ||
func ternary(vt interface{}, vf interface{}, v bool) interface{} { | ||
if v { | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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