Skip to content

Commit

Permalink
Added trim to available methods in template
Browse files Browse the repository at this point in the history
  • Loading branch information
Marius Gundersen authored and Marius Gundersen committed Oct 8, 2015
1 parent ecdad5c commit dc56c16
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ For example, this is a JSON version of an emitted RuntimeContainer struct:
* *`split $string $sep`*: Splits `$string` into a slice of substrings delimited by `$sep`. Alias for [`strings.Split`](http://golang.org/pkg/strings/#Split)
* *`trimPrefix $prefix $string`*: If `$prefix` is a prefix of `$string`, return `$string` with `$prefix` trimmed from the beginning. Otherwise, return `$string` unchanged.
* *`trimSuffix $suffix $string`*: If `$suffix` is a suffix of `$string`, return `$string` with `$suffix` trimmed from the end. Otherwise, return `$string` unchanged.
* *`trim $string`*: Removes whitespace from both sides of `$string`.
* *`where $items $fieldPath $value`*: Filters an array or slice based on the values of a field path expression `$fieldPath`. A field path expression is a dot-delimited list of map keys or struct member names specifying the path from container to a nested value. Returns an array of items having that value.
* *`whereExist $items $fieldPath`*: Like `where`, but returns only items where `$fieldPath` exists (is not nil).
* *`whereNotExist $items $fieldPath`*: Like `where`, but returns only items where `$fieldPath` does not exist (is nil).
Expand Down
9 changes: 7 additions & 2 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,16 @@ func coalesce(input ...interface{}) interface{} {
return nil
}

// trimPrefix returns whether a given string is a prefix of another string
func trim(s string) string {
return strings.TrimSpace(s)
}

// trimPrefix returns a string without the prefix, if present
func trimPrefix(prefix, s string) string {
return strings.TrimPrefix(s, prefix)
}

// trimSuffix returns whether a given string is a suffix of another string
// trimSuffix returns a string without the suffix, if present
func trimSuffix(suffix, s string) string {
return strings.TrimSuffix(s, suffix)
}
Expand Down Expand Up @@ -352,6 +356,7 @@ func newTemplate(name string) *template.Template {
"split": strings.Split,
"trimPrefix": trimPrefix,
"trimSuffix": trimSuffix,
"trim": trim,
"where": where,
"whereExist": whereExist,
"whereNotExist": whereNotExist,
Expand Down
9 changes: 9 additions & 0 deletions template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,15 @@ func TestTrimSuffix(t *testing.T) {
}
}

func TestTrim(t *testing.T) {
const str = " myhost.local "
const trimmed = "myhost.local"
got := trim(str)
if got != trimmed {
t.Fatalf("expected trim(%s) to be %s, got %s", str, trimmed, got)
}
}

func TestDict(t *testing.T) {
containers := []*RuntimeContainer{
&RuntimeContainer{
Expand Down
2 changes: 1 addition & 1 deletion templates/nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ server {
access_log /proc/self/fd/1;

location / {
proxy_pass http://{{ $host }};
proxy_pass http://{{ trim $host }};
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
Expand Down

0 comments on commit dc56c16

Please sign in to comment.