summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Nugent <james@jen20.com>2017-05-17 20:48:12 -0400
committerDave Henderson <dhenderson@gmail.com>2017-05-18 18:10:19 -0400
commit8218e45cbcc756333bee6b6245ef5a4c2aaa273f (patch)
tree2348f35d33fd05a5f318854d6d447455f33bf727
parent8d5f38e2b0bab1117e82ddc6558ae5844e283496 (diff)
Add "replaceAll" function and documentation
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.
-rw-r--r--README.md18
-rw-r--r--gomplate.go3
-rw-r--r--stringfunc.go10
-rw-r--r--stringfunc_test.go16
4 files changed, 46 insertions, 1 deletions
diff --git a/README.md b/README.md
index 92b8085f..cb60f2c6 100644
--- a/README.md
+++ b/README.md
@@ -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)
@@ -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`
diff --git a/gomplate.go b/gomplate.go
index 28e623ab..c2490e81 100644
--- a/gomplate.go
+++ b/gomplate.go
@@ -4,7 +4,6 @@ import (
"io"
"log"
"net/url"
-
"strings"
"text/template"
@@ -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{
@@ -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,
diff --git a/stringfunc.go b/stringfunc.go
new file mode 100644
index 00000000..ff51b80a
--- /dev/null
+++ b/stringfunc.go
@@ -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)
+}
diff --git a/stringfunc_test.go b/stringfunc_test.go
new file mode 100644
index 00000000..62d95dcb
--- /dev/null
+++ b/stringfunc_test.go
@@ -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"))
+}