Skip to content

Commit dc56c16

Browse files
Marius GundersenMarius Gundersen
Marius Gundersen
authored and
Marius Gundersen
committed
Added trim to available methods in template
1 parent ecdad5c commit dc56c16

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ For example, this is a JSON version of an emitted RuntimeContainer struct:
229229
* *`split $string $sep`*: Splits `$string` into a slice of substrings delimited by `$sep`. Alias for [`strings.Split`](http://golang.org/pkg/strings/#Split)
230230
* *`trimPrefix $prefix $string`*: If `$prefix` is a prefix of `$string`, return `$string` with `$prefix` trimmed from the beginning. Otherwise, return `$string` unchanged.
231231
* *`trimSuffix $suffix $string`*: If `$suffix` is a suffix of `$string`, return `$string` with `$suffix` trimmed from the end. Otherwise, return `$string` unchanged.
232+
* *`trim $string`*: Removes whitespace from both sides of `$string`.
232233
* *`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.
233234
* *`whereExist $items $fieldPath`*: Like `where`, but returns only items where `$fieldPath` exists (is not nil).
234235
* *`whereNotExist $items $fieldPath`*: Like `where`, but returns only items where `$fieldPath` does not exist (is nil).

template.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,16 @@ func coalesce(input ...interface{}) interface{} {
317317
return nil
318318
}
319319

320-
// trimPrefix returns whether a given string is a prefix of another string
320+
func trim(s string) string {
321+
return strings.TrimSpace(s)
322+
}
323+
324+
// trimPrefix returns a string without the prefix, if present
321325
func trimPrefix(prefix, s string) string {
322326
return strings.TrimPrefix(s, prefix)
323327
}
324328

325-
// trimSuffix returns whether a given string is a suffix of another string
329+
// trimSuffix returns a string without the suffix, if present
326330
func trimSuffix(suffix, s string) string {
327331
return strings.TrimSuffix(s, suffix)
328332
}
@@ -352,6 +356,7 @@ func newTemplate(name string) *template.Template {
352356
"split": strings.Split,
353357
"trimPrefix": trimPrefix,
354358
"trimSuffix": trimSuffix,
359+
"trim": trim,
355360
"where": where,
356361
"whereExist": whereExist,
357362
"whereNotExist": whereNotExist,

template_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,15 @@ func TestTrimSuffix(t *testing.T) {
488488
}
489489
}
490490

491+
func TestTrim(t *testing.T) {
492+
const str = " myhost.local "
493+
const trimmed = "myhost.local"
494+
got := trim(str)
495+
if got != trimmed {
496+
t.Fatalf("expected trim(%s) to be %s, got %s", str, trimmed, got)
497+
}
498+
}
499+
491500
func TestDict(t *testing.T) {
492501
containers := []*RuntimeContainer{
493502
&RuntimeContainer{

templates/nginx.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ server {
4949
access_log /proc/self/fd/1;
5050

5151
location / {
52-
proxy_pass http://{{ $host }};
52+
proxy_pass http://{{ trim $host }};
5353
proxy_set_header Host $http_host;
5454
proxy_set_header X-Real-IP $remote_addr;
5555
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

0 commit comments

Comments
 (0)