mirror of https://github.com/helm/helm.git
Merge pull request #30553 from Zhanweelee/add_mustToYaml_support
build-test / build (push) Has been cancelled
Details
CodeQL / Analyze (go) (push) Has been cancelled
Details
golangci-lint / golangci-lint (push) Has been cancelled
Details
release / release (push) Has been cancelled
Details
release / canary-release (push) Has been cancelled
Details
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
Details
build-test / build (push) Has been cancelled
Details
CodeQL / Analyze (go) (push) Has been cancelled
Details
golangci-lint / golangci-lint (push) Has been cancelled
Details
release / release (push) Has been cancelled
Details
release / canary-release (push) Has been cancelled
Details
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
Details
feat: Add mustToYaml and mustToJson template functions
This commit is contained in:
commit
a1f8802de9
|
@ -51,10 +51,12 @@ func funcMap() template.FuncMap {
|
|||
"toToml": toTOML,
|
||||
"fromToml": fromTOML,
|
||||
"toYaml": toYAML,
|
||||
"mustToYaml": mustToYAML,
|
||||
"toYamlPretty": toYAMLPretty,
|
||||
"fromYaml": fromYAML,
|
||||
"fromYamlArray": fromYAMLArray,
|
||||
"toJson": toJSON,
|
||||
"mustToJson": mustToJSON,
|
||||
"fromJson": fromJSON,
|
||||
"fromJsonArray": fromJSONArray,
|
||||
|
||||
|
@ -91,6 +93,19 @@ func toYAML(v interface{}) string {
|
|||
return strings.TrimSuffix(string(data), "\n")
|
||||
}
|
||||
|
||||
// mustToYAML takes an interface, marshals it to yaml, and returns a string.
|
||||
// It will panic if there is an error.
|
||||
//
|
||||
// This is designed to be called from a template when need to ensure that the
|
||||
// output YAML is valid.
|
||||
func mustToYAML(v interface{}) string {
|
||||
data, err := yaml.Marshal(v)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return strings.TrimSuffix(string(data), "\n")
|
||||
}
|
||||
|
||||
func toYAMLPretty(v interface{}) string {
|
||||
var data bytes.Buffer
|
||||
encoder := goYaml.NewEncoder(&data)
|
||||
|
@ -176,6 +191,19 @@ func toJSON(v interface{}) string {
|
|||
return string(data)
|
||||
}
|
||||
|
||||
// mustToJSON takes an interface, marshals it to json, and returns a string.
|
||||
// It will panic if there is an error.
|
||||
//
|
||||
// This is designed to be called from a template when need to ensure that the
|
||||
// output JSON is valid.
|
||||
func mustToJSON(v interface{}) string {
|
||||
data, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
// fromJSON converts a JSON document into a map[string]interface{}.
|
||||
//
|
||||
// This is not a general-purpose JSON parser, and will not parse all valid
|
||||
|
|
|
@ -135,6 +135,43 @@ keyInElement1 = "valueInElement1"`,
|
|||
assert.NoError(t, err)
|
||||
assert.Equal(t, tt.expect, b.String(), tt.tpl)
|
||||
}
|
||||
|
||||
loopMap := map[string]interface{}{
|
||||
"foo": "bar",
|
||||
}
|
||||
loopMap["loop"] = []interface{}{loopMap}
|
||||
|
||||
mustFuncsTests := []struct {
|
||||
tpl string
|
||||
expect interface{}
|
||||
vars interface{}
|
||||
}{{
|
||||
tpl: `{{ mustToYaml . }}`,
|
||||
vars: loopMap,
|
||||
}, {
|
||||
tpl: `{{ mustToJson . }}`,
|
||||
vars: loopMap,
|
||||
}, {
|
||||
tpl: `{{ toYaml . }}`,
|
||||
expect: "", // should return empty string and swallow error
|
||||
vars: loopMap,
|
||||
}, {
|
||||
tpl: `{{ toJson . }}`,
|
||||
expect: "", // should return empty string and swallow error
|
||||
vars: loopMap,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range mustFuncsTests {
|
||||
var b strings.Builder
|
||||
err := template.Must(template.New("test").Funcs(funcMap()).Parse(tt.tpl)).Execute(&b, tt.vars)
|
||||
if tt.expect != nil {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tt.expect, b.String(), tt.tpl)
|
||||
} else {
|
||||
assert.Error(t, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This test to check a function provided by sprig is due to a change in a
|
||||
|
|
Loading…
Reference in New Issue