Skip to content

Commit

Permalink
Add "replace" function and documentation
Browse files Browse the repository at this point in the history
This commit adds a `replace` 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.

The `strings.Replace` function in the Go standard library seems like it
would be better named `strings.ReplaceN`, so we wrap it to replace all
occurences of the string. If necessary, a gomplate function named
`replaceN` could be introduced calling `strings.Replace` directly.
  • Loading branch information
jen20 committed May 18, 2017
1 parent 8d5f38e commit 1bf38c3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
11 changes: 11 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)
- [`replace`](#replace)
- [`title`](#title)
- [`toLower`](#tolower)
- [`toUpper`](#toupper)
Expand Down Expand Up @@ -355,6 +356,16 @@ $ gomplate -i '{{ range splitN "foo:bar:baz" ":" 2 }}{{.}}{{end}}'
foo
bar:baz
```
#### `replace`

Replaces all occurrences of a given string with another.

##### Example

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

#### `title`

Expand Down
2 changes: 2 additions & 0 deletions gomplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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 +66,7 @@ func NewGomplate(data *Data, leftDelim, rightDelim string) *Gomplate {
"contains": strings.Contains,
"hasPrefix": strings.HasPrefix,
"hasSuffix": strings.HasSuffix,
"replace": stringfunc.Replace,
"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) Replace(s, old, new string) string {
return strings.Replace(s, old, new, -1)
}
15 changes: 15 additions & 0 deletions stringfunc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"testing"
"github.com/stretchr/testify/assert"
)

func TestReplace(t *testing.T) {
sf := &StringFunc{}

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

0 comments on commit 1bf38c3

Please sign in to comment.