Skip to content

Commit

Permalink
added ability to set custom template delims
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Chubatiuk <[email protected]>
  • Loading branch information
Andrew Chubatiuk committed Nov 3, 2023
1 parent 680f061 commit f582994
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ
return rsp, nil
}

tmpl, err := GetNewTemplateWithFunctionMaps().Parse(tg.GetTemplates())
tmpl, err := GetNewTemplateWithFunctionMaps(in).Parse(tg.GetTemplates())
if err != nil {
response.Fatal(rsp, errors.Wrap(err, "invalid function input: cannot parse the provided templates"))
return rsp, nil
Expand Down
11 changes: 9 additions & 2 deletions function_maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

sprig "github.com/Masterminds/sprig/v3"
"github.com/crossplane-contrib/function-go-templating/input/v1beta1"
)

var funcMaps = []template.FuncMap{
Expand All @@ -18,8 +19,14 @@ var funcMaps = []template.FuncMap{
},
}

func GetNewTemplateWithFunctionMaps() *template.Template {
tpl := template.New("manifests")
func GetNewTemplateWithFunctionMaps(in *v1beta1.GoTemplate) *template.Template {
if in.LeftDelims == "" {
in.LeftDelims = "{{"
}
if in.RightDelims == "" {
in.RightDelims = "}}"
}
tpl := template.New("manifests").Delims(in.LeftDelims, in.RightDelims)

for _, f := range funcMaps {
tpl.Funcs(f)
Expand Down
4 changes: 4 additions & 0 deletions input/v1beta1/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ type GoTemplate struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

// Template start characters
LeftDelims string `json:"leftDelims,omitempty"`
// Template end characters
RightDelims string `json:"rightDelims,omitempty"`
// Source specifies the different types of input sources that can be used with this function
Source TemplateSource `json:"source"`
// Inline is the inline form input of the templates
Expand Down
6 changes: 6 additions & 0 deletions package/input/gotemplating.fn.crossplane.io_gotemplates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,14 @@ spec:
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
leftDelims:
description: Template start characters
type: string
metadata:
type: object
rightDelims:
description: Template end characters
type: string
source:
description: Source specifies the different types of input sources that
can be used with this function
Expand Down

0 comments on commit f582994

Please sign in to comment.