diff --git a/internal/templatex/functions.go b/internal/templatex/functions.go index a395228..3e53279 100644 --- a/internal/templatex/functions.go +++ b/internal/templatex/functions.go @@ -24,19 +24,22 @@ import ( // template FuncMap generator func FuncMap() template.FuncMap { return template.FuncMap{ - "toYaml": toYaml, - "mustToYaml": toYaml, - "fromYaml": fromYaml, - "mustFromYaml": fromYaml, - "toJson": toJson, - "mustToJson": toJson, - "toPrettyJson": toPrettyJson, - "mustToPrettyJson": toPrettyJson, - "toRawJson": toRawJson, - "mustToRawJson": toRawJson, - "fromJson": fromJson, - "mustFromJson": fromJson, - "required": required, + "toYaml": toYaml, + "mustToYaml": toYaml, + "fromYaml": fromYaml, + "fromYamlArray": fromYamlArray, + "mustFromYaml": fromYaml, + "toJson": toJson, + "mustToJson": toJson, + "toPrettyJson": toPrettyJson, + "mustToPrettyJson": toPrettyJson, + "toRawJson": toRawJson, + "mustToRawJson": toRawJson, + "fromJson": fromJson, + "mustFromJson": fromJson, + "fromJsonArray": fromJsonArray, + "mustFromJsonArray": fromJsonArray, + "required": required, } } @@ -80,6 +83,14 @@ func fromYaml(data string) (any, error) { return res, nil } +func fromYamlArray(data string) ([]any, error) { + var res []any + if err := kyaml.Unmarshal([]byte(data), &res); err != nil { + return nil, err + } + return res, nil +} + func toJson(data any) (string, error) { raw, err := json.Marshal(data) if err != nil { @@ -115,6 +126,14 @@ func fromJson(data string) (any, error) { return res, nil } +func fromJsonArray(data string) ([]any, error) { + var res []any + if err := json.Unmarshal([]byte(data), &res); err != nil { + return nil, err + } + return res, nil +} + func required(warn string, data any) (any, error) { if data == nil { return data, errors.New(warn)