Skip to content

Commit

Permalink
Templating function fromYaml (run-int-tests) (#1271)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertgraeff authored Nov 21, 2024
1 parent 02cd76d commit 1a4ec79
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ func LandscaperTplFuncMap(blueprint *blueprints.Blueprint,
"readFile": readFileFunc(blueprint.Fs),
"readDir": readDir(blueprint.Fs),

"toYaml": toYAML,
"toYaml": toYAML,
"fromYaml": fromYAML,

"parseOCIRef": lstmpl.ParseOCIReference,
"ociRefRepo": getOCIReferenceRepository,
Expand Down Expand Up @@ -125,6 +126,13 @@ func toYAML(v interface{}) string {
return strings.TrimSuffix(string(data), "\n")
}

// fromYAML takes a string, unmarshals it from yaml, and returns an interface.
func fromYAML(input string) (any, error) {
var output any
err := yaml.Unmarshal([]byte(input), &output)
return output, err
}

// getOCIReferenceVersion returns the version of a oci reference
func getOCIReferenceVersion(ref string) string {
return lstmpl.ParseOCIReference(ref)[1]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,18 @@ var _ = Describe("TemplateDeployExecutions", func() {
Expect(res).To(BeEquivalentTo("config:\n value: foo\n const: bar"))
})

It("should render a go template with a fromYaml function", func() {
bp := blueprints.New(nil, memoryfs.New())
tmpl := `{{ $yamlData := fromYaml .values.yamlString }}{{ $yamlData.foo }}`
t, err := gotemplate.NewTemplateExecution(bp, nil, nil, nil)
Expect(err).ToNot(HaveOccurred())
values := map[string]interface{}{
"values": map[string]interface{}{
"yamlString": `foo: bar`,
},
}
res, err := t.Execute(tmpl, values)
Expect(err).ToNot(HaveOccurred())
Expect(string(res)).To(Equal("bar"))
})
})

0 comments on commit 1a4ec79

Please sign in to comment.