Skip to content

Commit

Permalink
Merge pull request #65 from flanksource/ignore-fields
Browse files Browse the repository at this point in the history
feat: add ability to ignore specific fields from walking while templa…
  • Loading branch information
moshloop authored Aug 12, 2021
2 parents 75c1fd5 + 63b8b50 commit f4a74c0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
8 changes: 8 additions & 0 deletions ktemplate/structtemplater.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ktemplate

import (
"github.com/mitchellh/reflectwalk"
"k8s.io/client-go/kubernetes"
"reflect"
"strings"
Expand All @@ -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
Expand All @@ -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()))
}
Expand Down

0 comments on commit f4a74c0

Please sign in to comment.