Skip to content
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

Enable template function splitToMap #1664

Merged
merged 1 commit into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/templating-language.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ provides the following functions:
- [sha256Hex](#sha256hex)
- [md5sum](#md5sum)
- [split](#split)
- [splitToMap](#splitToMap)
- [timestamp](#timestamp)
- [toJSON](#tojson)
- [toJSONPretty](#tojsonpretty)
Expand Down Expand Up @@ -1499,6 +1500,14 @@ This can be combined with chained and piped with other functions:
{{ key "foo" | toUpper | split "\n" | join "," }}
```

### `splitToMap`

Splits the given string on the provided separator and splits each resulting item into a key and value:

```golang
{{ "foo:bar\nbaz:bat\n" | splitToMap "\n" ":" }}
```

### `timestamp`

Returns the current timestamp as a string (UTC). If no arguments are given, the
Expand Down
20 changes: 20 additions & 0 deletions template/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,26 @@ func split(sep, s string) ([]string, error) {
return strings.Split(s, sep), nil
}

// splitToMap is a version of strings.Split which splits a second time for each
// item in the slice generated from the first split, building a map
func splitToMap(sep1, sep2, s string) (map[string]string, error) {
s = strings.TrimSpace(s)
if s == "" {
emptyMap := make(map[string]string)
return emptyMap, nil
}
s1 := strings.Split(s, sep1)
m:=make(map[string]string, len(s1))
for i := range s1 {
k,v,f:=strings.Cut(s1[i],sep2)
if !f {
continue
}
m[k]=v
}
return m, nil
}

// timestamp returns the current UNIX timestamp in UTC. If an argument is
// specified, it will be used to format the timestamp.
func timestamp(s ...string) (string, error) {
Expand Down
1 change: 1 addition & 0 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ func funcMap(i *funcMapInput) template.FuncMap {
"toUpper": toUpper,
"toYAML": toYAML,
"split": split,
"splitToMap": splitToMap,
"byMeta": byMeta,
"sockaddr": sockaddr,
"writeToFile": writeToFile,
Expand Down
11 changes: 11 additions & 0 deletions template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1685,6 +1685,17 @@ func TestTemplate_Execute(t *testing.T) {
"[a b c]",
false,
},
{
"helper_splitToMap",
&NewTemplateInput{
Contents: `{{ "a:x,b:y,c:z" | splitToMap "," ":" }}`,
},
&ExecuteInput{
Brain: NewBrain(),
},
"map[a:x b:y c:z]",
false,
},
{
"helper_timestamp",
&NewTemplateInput{
Expand Down