Skip to content

Commit

Permalink
pr comments
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 16, 2023
1 parent 1985ae6 commit c11b367
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 38 deletions.
6 changes: 4 additions & 2 deletions example/custom-delims/composition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ spec:
input:
apiVersion: gotemplating.fn.crossplane.io/v1beta1
kind: GoTemplate
leftDelims: '[['
rightDelims: ']]'
config:
delims:
left: '[['
right: ']]'
source: Inline
inline:
template: |
Expand Down
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(in).Parse(tg.GetTemplates())
tmpl, err := GetNewTemplateWithFunctionMaps(in.Config).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
19 changes: 7 additions & 12 deletions function_maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ import (
"github.com/crossplane/crossplane-runtime/pkg/fieldpath"
)

var (
defaultLeftDelims = "{{"
defaultRightDelims = "}}"
)

var funcMaps = []template.FuncMap{
{
"randomChoice": func(choices ...string) string {
Expand Down Expand Up @@ -53,14 +48,14 @@ var funcMaps = []template.FuncMap{
},
}

func GetNewTemplateWithFunctionMaps(in *v1beta1.GoTemplate) *template.Template {
if in.LeftDelims == nil {
in.LeftDelims = &defaultLeftDelims
}
if in.RightDelims == nil {
in.RightDelims = &defaultRightDelims
func GetNewTemplateWithFunctionMaps(cfg *v1beta1.Config) *template.Template {
tpl := template.New("manifests")

if cfg.Delims != nil {
if cfg.Delims.Left != nil && cfg.Delims.Right != nil {
tpl = tpl.Delims(*cfg.Delims.Left, *cfg.Delims.Right)
}
}
tpl := template.New("manifests").Delims(*in.LeftDelims, *in.RightDelims)

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

// Template start characters
// +kubebuilder:default:="{{"
// Go Template Config
// +optional
LeftDelims *string `json:"leftDelims,omitempty"`
// Template end characters
// +kubebuilder:default:="}}"
// +optional
RightDelims *string `json:"rightDelims,omitempty"`
Config *Config `json:"config,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 All @@ -52,3 +46,20 @@ type TemplateSourceInline struct {
type TemplateSourceFileSystem struct {
DirPath string `json:"dirPath,omitempty"`
}

type Config struct {
// Template delimiters
// +optional
Delims *Delims `json:"delims,omitempty"`
}

type Delims struct {
// Template start characters
// +kubebuilder:default:="{{"
// +optional
Left *string `json:"left,omitempty"`
// Template end characters
// +kubebuilder:default:="}}"
// +optional
Right *string `json:"right,omitempty"`
}
54 changes: 47 additions & 7 deletions input/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 16 additions & 8 deletions package/input/gotemplating.fn.crossplane.io_gotemplates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ spec:
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
config:
description: Go Template Config
properties:
delims:
description: Template delimiters
properties:
left:
default: '{{'
description: Template start characters
type: string
right:
default: '}}'
description: Template end characters
type: string
type: object
type: object
fileSystem:
description: FileSystem is the folder path where the templates are located
properties:
Expand All @@ -43,16 +59,8 @@ 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:
default: '{{'
description: Template start characters
type: string
metadata:
type: object
rightDelims:
default: '}}'
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 c11b367

Please sign in to comment.