From 63b8b506b96446ef657617d39b1d4c7ac229d0c3 Mon Sep 17 00:00:00 2001 From: Tarun Khandelwal Date: Thu, 12 Aug 2021 18:10:45 +0530 Subject: [PATCH] feat: add ability to ignore specific fields from walking while templating --- go.mod | 1 + go.sum | 1 + ktemplate/structtemplater.go | 8 ++++++++ 3 files changed, 10 insertions(+) diff --git a/go.mod b/go.mod index 490e6d4..14ef927 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/flanksource/commons v1.5.6 github.com/hairyhenderson/gomplate/v3 v3.6.0 github.com/mitchellh/mapstructure v1.3.3 + github.com/mitchellh/reflectwalk v1.0.0 github.com/pkg/errors v0.9.1 github.com/sergi/go-diff v1.0.0 github.com/sirupsen/logrus v1.7.0 diff --git a/go.sum b/go.sum index 218f156..53c1077 100644 --- a/go.sum +++ b/go.sum @@ -469,6 +469,7 @@ github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:F github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.3.3 h1:SzB1nHZ2Xi+17FP0zVQBHIZqvwRN9408fJO8h+eeNA8= github.com/mitchellh/mapstructure v1.3.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY= github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= github.com/moby/term v0.0.0-20200312100748-672ec06f55cd/go.mod h1:DdlQx2hp0Ss5/fLikoLlEeIYiATotOjgB//nb973jeo= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= diff --git a/ktemplate/structtemplater.go b/ktemplate/structtemplater.go index a4f6055..92e3f02 100644 --- a/ktemplate/structtemplater.go +++ b/ktemplate/structtemplater.go @@ -1,6 +1,7 @@ package ktemplate import ( + "github.com/mitchellh/reflectwalk" "k8s.io/client-go/kubernetes" "reflect" "strings" @@ -10,6 +11,8 @@ type StructTemplater struct { Values map[string]string Clientset *kubernetes.Clientset functions *Functions + // IgnoreFields from walking where key is field name and value is field type + IgnoreFields map[string]string } // this func is required to fulfil the reflectwalk.StructWalker interface @@ -18,6 +21,11 @@ func (w StructTemplater) Struct(reflect.Value) error { } func (w StructTemplater) StructField(f reflect.StructField, v reflect.Value) error { + for key, value := range w.IgnoreFields { + if key == f.Name && value == f.Type.String() { + return reflectwalk.SkipEntry + } + } if v.CanSet() && v.Kind() == reflect.String { v.SetString(w.Template(v.String())) }