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

Adds a toMap function which converts a ServiceTags element into a map. #410

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,19 @@ Takes the argument as a string and converts it to lowercase.

See Go's [strings.ToLower()](http://golang.org/pkg/strings/#ToLower) for more information.

##### 'toMap'
Takes the argument as a ServiceTags and converts any tag which are of the style `key=value` into a `map`.

Given the following tags in a service entry: ``path=/test lbset=group1``

```liquid
{{range service "www"}}
{{$x := .Tags | toMap}}
ProxyPass {{$x.path}} http://{{$x.lbset}}{{$x.path}}
ProxyPassReverse {{$x.path}} http://{{$x.lbset}}{{$x.path}}
{{end}}
```

##### `toTitle`
Takes the argument as a string and converts it to titlecase.

Expand Down
1 change: 1 addition & 0 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func funcMap(brain *Brain, used, missing map[string]dep.Dependency) template.Fun
"replaceAll": replaceAll,
"timestamp": timestamp,
"toLower": toLower,
"toMap": toMap,
"toJSON": toJSON,
"toJSONPretty": toJSONPretty,
"toTitle": toTitle,
Expand Down
15 changes: 15 additions & 0 deletions template_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -810,3 +810,18 @@ func addDependency(m map[string]dep.Dependency, d dep.Dependency) {
m[d.HashCode()] = d
}
}

// toMap converts ServiceTags into a map. It loops through the tags
// and any tag that has an equal sign in it is indexed in the map.
func toMap(entries dep.ServiceTags) (map[string]string, error) {
m := make(map[string]string)
for i := 0; i < len(entries); i++ {
if strings.Contains(entries[i], "=") {
item := strings.Split(entries[i], "=")
m[item[0]] = item[1]
}
}
return m, nil
}