-
Notifications
You must be signed in to change notification settings - Fork 16
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
Add support for using nested lists and maps #39
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 |
---|---|---|
|
@@ -12,6 +12,7 @@ import ( | |
"path" | ||
"fmt" | ||
"github.com/gruntwork-io/boilerplate/variables" | ||
"reflect" | ||
) | ||
|
||
const MaxRenderAttempts = 15 | ||
|
@@ -88,28 +89,29 @@ func renderVariables(variables map[string]interface{}, options *config.Boilerpla | |
// Variable values are allowed to use Go templating syntax (e.g. to reference other variables), so here, we render | ||
// those templates and return a new map of variables that are fully resolved. | ||
func renderVariable(variable interface{}, variables map[string]interface{}, options *config.BoilerplateOptions) (interface{}, error) { | ||
switch variableType := variable.(type) { | ||
case string: | ||
return renderTemplateRecursively(options.TemplateFolder, variableType, variables, options) | ||
case []string: | ||
values := []string{} | ||
for _, value := range variableType { | ||
rendered, err := renderTemplateRecursively(options.TemplateFolder, value, variables, options) | ||
valueType := reflect.ValueOf(variable) | ||
|
||
switch valueType.Kind() { | ||
case reflect.String: | ||
return renderTemplateRecursively(options.TemplateFolder, variable.(string), variables, options) | ||
case reflect.Slice: | ||
values := []interface{}{} | ||
for i := 0; i < valueType.Len(); i++ { | ||
rendered, err := renderVariable(valueType.Index(i).Interface(), variables, options) | ||
if err != nil { | ||
return nil, err | ||
return nil, err | ||
} | ||
values = append(values, rendered) | ||
} | ||
return values, nil | ||
case map[string]string: | ||
values := map[string]string{} | ||
for key, value := range variableType { | ||
renderedKey, err := renderTemplateRecursively(options.TemplateFolder, key, variables, options) | ||
case reflect.Map: | ||
values := map[interface{}]interface{}{} | ||
for _, key := range valueType.MapKeys() { | ||
renderedKey, err := renderVariable(key.Interface(), variables, options) | ||
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. Example 2 of Go being a shitty language. Here, type casting is required, because the incoming type genuinely could be anything, but even once I know the type is a |
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
renderedValue, err := renderTemplateRecursively(options.TemplateFolder, value, variables, options) | ||
renderedValue, err := renderVariable(valueType.MapIndex(key).Interface(), variables, options) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
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.
Today's example of Go being a shitty language: without generics, in order to support
map[x]y
for arbitraryx
andy
, I had to remove all types from the code and use reflection. That makes the code harder to read and more brittle. We've known how to do generics for several decades; there is no excuse for this.