-
Notifications
You must be signed in to change notification settings - Fork 17
Add lookup function #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,17 +8,22 @@ import ( | |
| "net/url" | ||
| "os" | ||
| "path/filepath" | ||
| "reflect" | ||
| "strings" | ||
| "text/template" | ||
|
|
||
| "github.com/Masterminds/sprig/v3" | ||
| "github.com/ghodss/yaml" | ||
| ) | ||
|
|
||
| var extraTemplateFuncs = template.FuncMap{ | ||
| "urlEncode": url.QueryEscape, | ||
| "toYaml": toYaml, | ||
| } | ||
| var ( | ||
| extraTemplateFuncs = template.FuncMap{ | ||
| "lookup": lookup, | ||
| "pathLookup": pathLookup, | ||
| "toYaml": toYaml, | ||
| "urlEncode": url.QueryEscape, | ||
| } | ||
| ) | ||
|
|
||
| // ApplyTemplate runs golang templating on all files in the provided path, | ||
| // replacing them in-place with their templated versions. | ||
|
|
@@ -214,6 +219,51 @@ func configMapEntriesGenerator( | |
| } | ||
| } | ||
|
|
||
| // lookup does a dot-separated path lookup on the input map. If a key on the path is | ||
| // not found, it returns nil. If the input or any of its children on the lookup path is not | ||
| // a map, it returns an error. | ||
| func lookup(input interface{}, path string) (interface{}, error) { | ||
| obj := reflect.ValueOf(input) | ||
| components := strings.Split(path, ".") | ||
|
|
||
| for i := 0; i < len(components); { | ||
| switch obj.Kind() { | ||
| case reflect.Map: | ||
| obj = obj.MapIndex(reflect.ValueOf(components[i])) | ||
| i++ | ||
| case reflect.Ptr, reflect.Interface: | ||
| if obj.IsNil() { | ||
| return nil, nil | ||
| } | ||
|
|
||
| // Get the thing being pointed to or interfaced, don't advance index | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm under the impression that we may have to check
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a check here. |
||
| obj = obj.Elem() | ||
| default: | ||
| if obj.IsValid() { | ||
| // An object was found, but it's not a map. Return an error. | ||
| return nil, fmt.Errorf( | ||
| "Tried to traverse a value that's not a map (kind=%s)", | ||
| obj.Kind(), | ||
| ) | ||
| } | ||
|
|
||
| // An intermediate key wasn't found | ||
| return nil, nil | ||
| } | ||
| } | ||
|
|
||
| if !obj.IsValid() { | ||
| // The last key wasn't found | ||
| return nil, nil | ||
| } | ||
| return obj.Interface(), nil | ||
| } | ||
|
|
||
| // pathLookup is the same as lookup, but with the arguments flipped. | ||
| func pathLookup(path string, input interface{}) (interface{}, error) { | ||
| return lookup(input, path) | ||
| } | ||
|
|
||
| func toYaml(input interface{}) (string, error) { | ||
| bytes, err := yaml.Marshal(input) | ||
| if err != nil { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package version | ||
|
|
||
| // Version stores the current kubeapply version. | ||
| const Version = "0.0.27" | ||
| const Version = "0.0.28" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like Sprig made the opinionated choice to use
map[string]interface{}only https://github.com/Masterminds/sprig/blob/39e4d5d0e0d566a256746e59749821155f209d11/dict.go#L8Do you know if we may ever run into cases where a struct is passed to this function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Map only is fine. I was trying to be super-flexible but probably unnecessary :-).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.