Commit 2c980160
2c98016074f9Merged
Vigilans <[email protected]> ยท 11 months ago ยท 3 files ยท ~3 modified
Description
feat: add fromYamlArray and fromYamlAll template functions
- Implemented FromYamlArray to parse YAML array (single document) into []any.
- Implemented FromYamlAll to parse multiple YAML documents into []any.
Signed-off-by: Vigilans [email protected]
Details
- Commit
- 2c98016074f9
- Parent
- b5d1d9e9e583
pkg/template/extra.gomodifiedView file
@@ -40,6 +40,42 @@
4040
return m, nil4141
}4242
43+
// FromYAMLArray parses YAML array string into data.44+
// Used as custom template function.45+
func FromYamlArray(str string) ([]any, error) {46+
a := []any{}47+
48+
if err := yaml.Unmarshal([]byte(str), &a); err != nil {49+
return nil, fmt.Errorf("failed to unmarshal %s from YAML as array: %w", str, err)50+
}51+
52+
return a, nil53+
}54+
55+
// FromYamlAll parses multiple YAML documents separated by `---`.56+
// Returns an array of all documents.57+
// Used as custom template function.58+
func FromYamlAll(str string) ([]any, error) {59+
a := []any{}60+
61+
d := yaml.NewDecoder(strings.NewReader(str))62+
for {63+
var v any64+
err := d.Decode(&v)65+
if err != nil {66+
if err.Error() == "EOF" {67+
break68+
}69+
return nil, fmt.Errorf("failed to unmarshal YAML document: %w", err)70+
}71+
if v != nil {72+
a = append(a, v)73+
}74+
}75+
76+
return a, nil77+
}78+
4379
// Exec runs external binary and returns its standard output.4480
// Used as custom template function.4581
func Exec(command string, args []any, inputs ...string) (string, error) {pkg/template/extra_test.gomodifiedView file
Loading diffโฆ
pkg/template/sprig_templater.gomodifiedView file
Loading diffโฆ