Skip to content

Commit

Permalink
feat: merge template func should support variadic list of maps
Browse files Browse the repository at this point in the history
  • Loading branch information
pd93 committed Jan 11, 2024
1 parent dbc120c commit 22ff62f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
4 changes: 2 additions & 2 deletions docs/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -1292,8 +1292,8 @@ Task also adds the following functions:
based on a base path (first argument). The same as Go's
[filepath.Rel](https://pkg.go.dev/path/filepath#Rel).
- `merge`: Creates a new map that is a copy of the first map with the keys of
the second map merged into it. If there are duplicate keys, the value of the
second map is used.
each subsequent map merged into it. If there is a duplicate key, the value of
the last map with that key is used.
- `spew`: Returns the Go representation of a specific variable. Useful for
debugging. Uses the [davecgh/go-spew](https://github.com/davecgh/go-spew)
package.
Expand Down
20 changes: 13 additions & 7 deletions internal/templater/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,21 @@ func init() {
"relPath": func(basePath, targetPath string) (string, error) {
return filepath.Rel(basePath, targetPath)
},
"merge": func(a, b map[string]any) map[string]any {
m := make(map[string]any, len(a)+len(b))
for k, v := range a {
m[k] = v
"merge": func(base map[string]any, v ...map[string]any) map[string]any {
cap := len(v)
for _, m := range v {
cap += len(m)
}
for k, v := range b {
m[k] = v
result := make(map[string]any, cap)
for k, v := range base {
result[k] = v
}
return m
for _, m := range v {
for k, v := range m {
result[k] = v
}
}
return result
},
"spew": func(v any) string {
return spew.Sdump(v)
Expand Down

0 comments on commit 22ff62f

Please sign in to comment.