Skip to content

Commit

Permalink
Add "replaceAll" function and documentation
Browse files Browse the repository at this point in the history
This commit adds a `replaceAll` function which takes three arguments - an
original string, the substring to replace, and the string with which to
replace it. This is of particular use when generating node names from IP
addresses where the node name may not contain "." characters.
  • Loading branch information
jen20 committed May 18, 2017
1 parent 8d5f38e commit b6739fe
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Gomplate is an alternative that will let you process templates which also includ
- [`slice`](#slice)
- [`split`](#split)
- [`splitN`](#splitn)
- [`replaceAll`](#replaceAll)
- [`title`](#title)
- [`toLower`](#tolower)
- [`toUpper`](#toupper)
Expand Down Expand Up @@ -355,6 +356,23 @@ $ gomplate -i '{{ range splitN "foo:bar:baz" ":" 2 }}{{.}}{{end}}'
foo
bar:baz
```
#### `replaceAll`

Replaces all occurrences of a given string with another.

##### Example

```console
$ gomplate -i '{{ replaceAll "." "-" "172.21.1.42" }}'
172-21-1-42
```

##### Example (with pipeline)

```console
$ gomplate -i '{{ "172.21.1.42" | replaceAll "." "-" }}'
172-21-1-42
```

#### `title`

Expand Down
3 changes: 2 additions & 1 deletion gomplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"io"
"log"
"net/url"

"strings"
"text/template"

Expand Down Expand Up @@ -39,6 +38,7 @@ func (g *Gomplate) RunTemplate(text string, out io.Writer) {
func NewGomplate(data *Data, leftDelim, rightDelim string) *Gomplate {
env := &Env{}
typeconv := &TypeConv{}
stringfunc := &stringFunc{}
ec2meta := aws.NewEc2Meta()
ec2info := aws.NewEc2Info()
return &Gomplate{
Expand All @@ -65,6 +65,7 @@ func NewGomplate(data *Data, leftDelim, rightDelim string) *Gomplate {
"contains": strings.Contains,
"hasPrefix": strings.HasPrefix,
"hasSuffix": strings.HasSuffix,
"replaceAll": stringfunc.replaceAll,
"split": strings.Split,
"splitN": strings.SplitN,
"title": strings.Title,
Expand Down
10 changes: 10 additions & 0 deletions stringfunc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import "strings"

// stringFunc - string manipulation function wrappers
type stringFunc struct{}

func (t stringFunc) replaceAll(old, new, s string) string {
return strings.Replace(s, old, new, -1)
}
16 changes: 16 additions & 0 deletions stringfunc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestReplaceAll(t *testing.T) {
sf := &stringFunc{}

assert.Equal(t, "Replaced",
sf.replaceAll("Orig", "Replaced", "Orig"))
assert.Equal(t, "ReplacedReplaced",
sf.replaceAll("Orig", "Replaced", "OrigOrig"))
}

0 comments on commit b6739fe

Please sign in to comment.