Skip to content

Commit

Permalink
add template functions fromYamlArray and fromJsonArrray (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbarbian-sap authored Sep 3, 2024
1 parent d6e590a commit 408c8b2
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions internal/templatex/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 408c8b2

Please sign in to comment.