summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/content/functions/general.md149
-rw-r--r--docs/content/functions/strings.md358
-rw-r--r--funcs.go14
-rw-r--r--funcs/strings.go100
-rw-r--r--funcs/strings_test.go (renamed from stringfunc_test.go)8
-rw-r--r--stringfunc.go10
6 files changed, 463 insertions, 176 deletions
diff --git a/docs/content/functions/general.md b/docs/content/functions/general.md
index 28f48009..c448948b 100644
--- a/docs/content/functions/general.md
+++ b/docs/content/functions/general.md
@@ -5,25 +5,6 @@ menu:
parent: functions
---
-## `contains`
-
-Contains reports whether the second string is contained within the first. Equivalent to
-[strings.Contains](https://golang.org/pkg/strings#Contains)
-
-### Example
-
-_`input.tmpl`:_
-```
-{{if contains .Env.FOO "f"}}yes{{else}}no{{end}}
-```
-
-```console
-$ FOO=foo gomplate < input.tmpl
-yes
-$ FOO=bar gomplate < input.tmpl
-no
-```
-
## `getenv`
Exposes the [os.Getenv](https://golang.org/pkg/os/#Getenv) function.
@@ -42,42 +23,6 @@ $ gomplate -i 'Hey, {{getenv "FIRSTNAME" "you"}}!'
Hey, you!
```
-## `hasPrefix`
-
-Tests whether the string begins with a certain substring. Equivalent to
-[strings.HasPrefix](https://golang.org/pkg/strings#HasPrefix)
-
-#### Example
-
-_`input.tmpl`:_
-```
-{{if hasPrefix .Env.URL "https"}}foo{{else}}bar{{end}}
-```
-
-```console
-$ URL=http://example.com gomplate < input.tmpl
-bar
-$ URL=https://example.com gomplate < input.tmpl
-foo
-```
-
-## `hasSuffix`
-
-Tests whether the string ends with a certain substring. Equivalent to
-[strings.HasSuffix](https://golang.org/pkg/strings#HasSuffix)
-
-#### Example
-
-_`input.tmpl`:_
-```
-{{.Env.URL}}{{if not (hasSuffix .Env.URL ":80")}}:80{{end}}
-```
-
-```console
-$ URL=http://example.com gomplate < input.tmpl
-http://example.com:80
-```
-
## `bool`
Converts a true-ish string to a boolean. Can be used to simplify conditional statements based on environment variables or other text input.
@@ -116,100 +61,6 @@ Hello, Lisa
Hello, Maggie
```
-## `split`
-
-Creates a slice by splitting a string on a given delimiter. Equivalent to
-[strings.Split](https://golang.org/pkg/strings#Split)
-
-#### Example
-
-```console
-$ gomplate -i '{{range split "Bart,Lisa,Maggie" ","}}Hello, {{.}}{{end}}'
-Hello, Bart
-Hello, Lisa
-Hello, Maggie
-```
-
-## `splitN`
-
-Creates a slice by splitting a string on a given delimiter. The count determines
-the number of substrings to return. Equivalent to [strings.SplitN](https://golang.org/pkg/strings#SplitN)
-
-#### Example
-
-```console
-$ 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`
-
-Convert to title-case. Equivalent to [strings.Title](https://golang.org/pkg/strings/#Title)
-
-#### Example
-
-```console
-$ echo '{{title "hello, world!"}}' | gomplate
-Hello, World!
-```
-
-## `toLower`
-
-Convert to lower-case. Equivalent to [strings.ToLower](https://golang.org/pkg/strings/#ToLower)
-
-#### Example
-
-```console
-$ echo '{{toLower "HELLO, WORLD!"}}' | gomplate
-hello, world!
-```
-
-## `toUpper`
-
-Convert to upper-case. Equivalent to [strings.ToUpper](https://golang.org/pkg/strings/#ToUpper)
-
-#### Example
-
-```console
-$ echo '{{toUpper "hello, world!"}}' | gomplate
-HELLO, WORLD!
-```
-
-## `trim`
-
-Trims a string by removing the given characters from the beginning and end of
-the string. Equivalent to [strings.Trim](https://golang.org/pkg/strings/#Trim)
-
-#### Example
-
-_`input.tmpl`:_
-```
-Hello, {{trim .Env.FOO " "}}!
-```
-
-```console
-$ FOO=" world " | gomplate < input.tmpl
-Hello, world!
-```
-
## `urlParse`
Parses a string as a URL for later use. Equivalent to [url.Parse](https://golang.org/pkg/net/url/#Parse)
diff --git a/docs/content/functions/strings.md b/docs/content/functions/strings.md
new file mode 100644
index 00000000..610638dc
--- /dev/null
+++ b/docs/content/functions/strings.md
@@ -0,0 +1,358 @@
+---
+title: strings functions
+menu:
+ main:
+ parent: functions
+---
+
+## `strings.Contains`
+
+Reports whether a substring is contained within a string.
+
+### Usage
+```go
+strings.Contains substr input
+```
+```go
+input | strings.Contains substr
+```
+
+### Example
+
+_`input.tmpl`:_
+```
+{{ if (.Env.FOO | strings.Contains "f") }}yes{{else}}no{{end}}
+```
+
+```console
+$ FOO=foo gomplate < input.tmpl
+yes
+$ FOO=bar gomplate < input.tmpl
+no
+```
+
+## `strings.HasPrefix`
+
+Tests whether a string begins with a certain prefix.
+
+### Usage
+```go
+strings.HasPrefix prefix input
+```
+```go
+input | strings.HasPrefix prefix
+```
+
+#### Example
+
+```console
+$ URL=http://example.com gomplate -i '{{if .Env.URL | strings.HasPrefix "https"}}foo{{else}}bar{{end}}'
+bar
+$ URL=https://example.com gomplate -i '{{if .Env.URL | strings.HasPrefix "https"}}foo{{else}}bar{{end}}'
+foo
+```
+
+## `strings.HasSuffix`
+
+Tests whether a string ends with a certain suffix.
+
+### Usage
+```go
+strings.HasSuffix suffix input
+```
+```go
+input | strings.HasSuffix suffix
+```
+
+### Examples
+
+_`input.tmpl`:_
+```
+{{.Env.URL}}{{if not (.Env.URL | strings.HasSuffix ":80")}}:80{{end}}
+```
+
+```console
+$ URL=http://example.com gomplate < input.tmpl
+http://example.com:80
+```
+
+## `strings.Split`
+
+Creates a slice by splitting a string on a given delimiter.
+
+### Usage
+```go
+strings.Split separator input
+```
+```go
+input | strings.Split separator
+```
+
+### Examples
+
+```console
+$ gomplate -i '{{range ("Bart,Lisa,Maggie" | strings.Split ",") }}Hello, {{.}}{{end}}'
+Hello, Bart
+Hello, Lisa
+Hello, Maggie
+```
+
+## `strings.SplitN`
+
+Creates a slice by splitting a string on a given delimiter. The count determines
+the number of substrings to return.
+
+### Usage
+```go
+strings.SplitN separator count input
+```
+```go
+input | strings.SplitN separator count
+```
+
+#### Example
+
+```console
+$ gomplate -i '{{ range ("foo:bar:baz" | strings.SplitN ":" 2) }}{{.}}{{end}}'
+foo
+bar:baz
+```
+
+## `strings.ReplaceAll`
+
+**Alias:** `replaceAll`
+
+Replaces all occurrences of a given string with another.
+
+### Usage
+```go
+strings.ReplaceAll old new input
+```
+```go
+input | strings.ReplaceAll old new
+```
+
+### Examples
+
+```console
+$ gomplate -i '{{ strings.ReplaceAll "." "-" "172.21.1.42" }}'
+172-21-1-42
+$ gomplate -i '{{ "172.21.1.42" | strings.ReplaceAll "." "-" }}'
+172-21-1-42
+```
+
+## `strings.Title`
+
+**Alias:** `title`
+
+Convert to title-case.
+
+### Usage
+```go
+strings.Title input
+```
+```go
+input | strings.Title
+```
+
+### Example
+
+```console
+$ gomplate -i '{{strings.Title "hello, world!"}}'
+Hello, World!
+```
+
+## `strings.ToLower`
+
+**Alias:** `toLower`
+
+Convert to lower-case.
+
+### Usage
+```go
+strings.ToLower input
+```
+```go
+input | strings.ToLower
+```
+
+#### Example
+
+```console
+$ echo '{{strings.ToLower "HELLO, WORLD!"}}' | gomplate
+hello, world!
+```
+
+## `strings.ToUpper`
+
+**Alias:** `toUpper`
+
+Convert to upper-case.
+
+### Usage
+```go
+strings.ToUpper input
+```
+```go
+input | strings.ToUpper
+```
+
+#### Example
+
+```console
+$ gomplate -i '{{strings.ToUpper "hello, world!"}}'
+HELLO, WORLD!
+```
+
+## `strings.Trim`
+
+Trims a string by removing the given characters from the beginning and end of
+the string.
+
+### Usage
+```go
+strings.Trim cutset input
+```
+```go
+input | strings.Trim cutset
+```
+
+#### Example
+
+```console
+$ gomplate -i '{{ "_-foo-_" | strings.Trim "_-" }}
+foo
+```
+
+## `strings.TrimSpace`
+
+**Alias:** `trimSpace`
+
+Trims a string by removing whitespace from the beginning and end of
+the string.
+
+### Usage
+```go
+strings.TrimSpace input
+```
+```go
+input | strings.TrimSpace
+```
+
+#### Example
+
+```console
+$ gomplate -i '{{ " \n\t foo" | strings.TrimSpace }}'
+foo
+```
+
+## `contains`
+
+**See [`strings.Contains](#strings-contains) for a pipeline-compatible version**
+
+Contains reports whether the second string is contained within the first. Equivalent to
+[strings.Contains](https://golang.org/pkg/strings#Contains)
+
+### Example
+
+_`input.tmpl`:_
+```
+{{if contains .Env.FOO "f"}}yes{{else}}no{{end}}
+```
+
+```console
+$ FOO=foo gomplate < input.tmpl
+yes
+$ FOO=bar gomplate < input.tmpl
+no
+```
+
+## `hasPrefix`
+
+**See [`strings.HasPrefix](#strings-hasprefix) for a pipeline-compatible version**
+
+Tests whether the string begins with a certain substring. Equivalent to
+[strings.HasPrefix](https://golang.org/pkg/strings#HasPrefix)
+
+#### Example
+
+_`input.tmpl`:_
+```
+{{if hasPrefix .Env.URL "https"}}foo{{else}}bar{{end}}
+```
+
+```console
+$ URL=http://example.com gomplate < input.tmpl
+bar
+$ URL=https://example.com gomplate < input.tmpl
+foo
+```
+
+## `hasSuffix`
+
+**See [`strings.HasSuffix](#strings-hassuffix) for a pipeline-compatible version**
+
+Tests whether the string ends with a certain substring. Equivalent to
+[strings.HasSuffix](https://golang.org/pkg/strings#HasSuffix)
+
+#### Example
+
+_`input.tmpl`:_
+```
+{{.Env.URL}}{{if not (hasSuffix .Env.URL ":80")}}:80{{end}}
+```
+
+```console
+$ URL=http://example.com gomplate < input.tmpl
+http://example.com:80
+```
+
+## `split`
+
+**See [`strings.Split](#strings-split) for a pipeline-compatible version**
+
+Creates a slice by splitting a string on a given delimiter. Equivalent to
+[strings.Split](https://golang.org/pkg/strings#Split)
+
+#### Example
+
+```console
+$ gomplate -i '{{range split "Bart,Lisa,Maggie" ","}}Hello, {{.}}{{end}}'
+Hello, Bart
+Hello, Lisa
+Hello, Maggie
+```
+
+## `splitN`
+
+**See [`strings.SplitN](#strings-splitn) for a pipeline-compatible version**
+
+Creates a slice by splitting a string on a given delimiter. The count determines
+the number of substrings to return. Equivalent to [strings.SplitN](https://golang.org/pkg/strings#SplitN)
+
+#### Example
+
+```console
+$ gomplate -i '{{ range splitN "foo:bar:baz" ":" 2 }}{{.}}{{end}}'
+foo
+bar:baz
+```
+
+## `trim`
+
+**See [`strings.Trim](#strings-trim) for a pipeline-compatible version**
+
+Trims a string by removing the given characters from the beginning and end of
+the string. Equivalent to [strings.Trim](https://golang.org/pkg/strings/#Trim)
+
+#### Example
+
+_`input.tmpl`:_
+```
+Hello, {{trim .Env.FOO " "}}!
+```
+
+```console
+$ FOO=" world " | gomplate < input.tmpl
+Hello, world!
+```
diff --git a/funcs.go b/funcs.go
index 7d38ef22..0a15b53e 100644
--- a/funcs.go
+++ b/funcs.go
@@ -2,7 +2,6 @@ package main
import (
"net/url"
- "strings"
"text/template"
"github.com/hairyhenderson/gomplate/funcs"
@@ -12,7 +11,6 @@ import (
func initFuncs(data *Data) template.FuncMap {
env := &Env{}
typeconv := &TypeConv{}
- stringfunc := &stringFunc{}
f := template.FuncMap{
"getenv": env.Getenv,
@@ -34,17 +32,6 @@ func initFuncs(data *Data) template.FuncMap {
"toYAML": typeconv.ToYAML,
"toTOML": typeconv.ToTOML,
"toCSV": typeconv.ToCSV,
- "contains": strings.Contains,
- "hasPrefix": strings.HasPrefix,
- "hasSuffix": strings.HasSuffix,
- "replaceAll": stringfunc.replaceAll,
- "split": strings.Split,
- "splitN": strings.SplitN,
- "title": strings.Title,
- "toUpper": strings.ToUpper,
- "toLower": strings.ToLower,
- "trim": strings.Trim,
- "trimSpace": strings.TrimSpace,
"urlParse": url.Parse,
"datasource": data.Datasource,
"ds": data.Datasource,
@@ -55,5 +42,6 @@ func initFuncs(data *Data) template.FuncMap {
funcs.AddBase64Funcs(f)
funcs.AddNetFuncs(f)
funcs.AddReFuncs(f)
+ funcs.AddStringFuncs(f)
return f
}
diff --git a/funcs/strings.go b/funcs/strings.go
new file mode 100644
index 00000000..b78fe40c
--- /dev/null
+++ b/funcs/strings.go
@@ -0,0 +1,100 @@
+package funcs
+
+// Namespace strings contains mostly wrappers of equivalently-named
+// functions in the standard library `strings` package, with
+// differences in argument order where it makes pipelining
+// in templates easier.
+
+import (
+ "sync"
+
+ "strings"
+)
+
+var (
+ strNS *StringFuncs
+ strNSInit sync.Once
+)
+
+// StrNS -
+func StrNS() *StringFuncs {
+ strNSInit.Do(func() { strNS = &StringFuncs{} })
+ return strNS
+}
+
+// AddStringFuncs -
+func AddStringFuncs(f map[string]interface{}) {
+ f["strings"] = StrNS
+
+ f["replaceAll"] = StrNS().ReplaceAll
+ f["title"] = StrNS().Title
+ f["toUpper"] = StrNS().ToUpper
+ f["toLower"] = StrNS().ToLower
+ f["trimSpace"] = StrNS().TrimSpace
+
+ // these are legacy aliases with non-pipelinable arg order
+ f["contains"] = strings.Contains
+ f["hasPrefix"] = strings.HasPrefix
+ f["hasSuffix"] = strings.HasSuffix
+ f["split"] = strings.Split
+ f["splitN"] = strings.SplitN
+ f["trim"] = strings.Trim
+}
+
+// StringFuncs -
+type StringFuncs struct{}
+
+// ReplaceAll -
+func (f *StringFuncs) ReplaceAll(old, new, s string) string {
+ return strings.Replace(s, old, new, -1)
+}
+
+// Contains -
+func (f *StringFuncs) Contains(substr, s string) bool {
+ return strings.Contains(s, substr)
+}
+
+// HasPrefix -
+func (f *StringFuncs) HasPrefix(prefix, s string) bool {
+ return strings.HasPrefix(s, prefix)
+}
+
+// HasSuffix -
+func (f *StringFuncs) HasSuffix(suffix, s string) bool {
+ return strings.HasSuffix(s, suffix)
+}
+
+// Split -
+func (f *StringFuncs) Split(sep, s string) []string {
+ return strings.Split(s, sep)
+}
+
+// SplitN -
+func (f *StringFuncs) SplitN(sep string, n int, s string) []string {
+ return strings.SplitN(s, sep, n)
+}
+
+// Trim -
+func (f *StringFuncs) Trim(cutset, s string) string {
+ return strings.Trim(s, cutset)
+}
+
+// Title -
+func (f *StringFuncs) Title(s string) string {
+ return strings.Title(s)
+}
+
+// ToUpper -
+func (f *StringFuncs) ToUpper(s string) string {
+ return strings.ToUpper(s)
+}
+
+// ToLower -
+func (f *StringFuncs) ToLower(s string) string {
+ return strings.ToLower(s)
+}
+
+// TrimSpace -
+func (f *StringFuncs) TrimSpace(s string) string {
+ return strings.TrimSpace(s)
+}
diff --git a/stringfunc_test.go b/funcs/strings_test.go
index 62d95dcb..69bfabd9 100644
--- a/stringfunc_test.go
+++ b/funcs/strings_test.go
@@ -1,4 +1,4 @@
-package main
+package funcs
import (
"testing"
@@ -7,10 +7,10 @@ import (
)
func TestReplaceAll(t *testing.T) {
- sf := &stringFunc{}
+ sf := &StringFuncs{}
assert.Equal(t, "Replaced",
- sf.replaceAll("Orig", "Replaced", "Orig"))
+ sf.ReplaceAll("Orig", "Replaced", "Orig"))
assert.Equal(t, "ReplacedReplaced",
- sf.replaceAll("Orig", "Replaced", "OrigOrig"))
+ sf.ReplaceAll("Orig", "Replaced", "OrigOrig"))
}
diff --git a/stringfunc.go b/stringfunc.go
deleted file mode 100644
index ff51b80a..00000000
--- a/stringfunc.go
+++ /dev/null
@@ -1,10 +0,0 @@
-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)
-}