Skip to content

Commit ce21a5f

Browse files
committed
Add hasPrefix(prefix,s) and hasSuffix(suffix,s)
1 parent e5efc78 commit ce21a5f

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ Within those templates, the object emitted by docker-gen will have [this structu
114114
* *`groupBy $containers $fieldPath`*: Groups an array of `RuntimeContainer` instances 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, which must be a string. Returns a map from the value of the field path expression to an array of containers having that value. Containers that do not have a value for the field path in question are omitted.
115115
* *`groupByKeys $containers $fieldPath`*: Returns the same as `groupBy` but only returns the keys of the map.
116116
* *`groupByMulti $containers $fieldPath $sep`*: Like `groupBy`, but the string value specified by `$fieldPath` is first split by `$sep` into a list of strings. A container whose `$fieldPath` value contains a list of strings will show up in the map output under each of those strings.
117+
* *`hasPrefix $prefix $string`*: Returns whether `$prefix` is a prefix of `$string`.
118+
* *`hasSuffix $suffix $string`*: Returns whether `$suffix` is a suffix of `$string`.
117119
* *`split $string $sep`*: Splits `$string` into a slice of substrings delimited by `$sep`. Alias for [`strings.Split`](http://golang.org/pkg/strings/#Split)
118120
* *`replace $string $old $new $count`*: Replaces up to `$count` occurences of `$old` with `$new` in `$string`. Alias for [`strings.Replace`](http://golang.org/pkg/strings/#Replace)
119121
* *`dict $key $value ...`*: Creates a map from a list of pairs. Each `$key` value must be a `string`, but the `$value` can be any type (or `nil`). Useful for passing more than one value as a pipeline context to subtemplates.

template.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ func groupByKeys(entries []*RuntimeContainer, key string) []string {
6565
return ret
6666
}
6767

68+
// hasPrefix returns whether a given string is a prefix of another string
69+
func hasPrefix(prefix, s string) bool {
70+
return strings.HasPrefix(s, prefix)
71+
}
72+
73+
// hasSuffix returns whether a given string is a suffix of another string
74+
func hasSuffix(suffix, s string) bool {
75+
return strings.HasSuffix(s, suffix)
76+
}
77+
6878
func contains(item map[string]string, key string) bool {
6979
if _, ok := item[key]; ok {
7080
return true
@@ -170,6 +180,8 @@ func generateFile(config Config, containers Context) bool {
170180
"groupBy": groupBy,
171181
"groupByMulti": groupByMulti,
172182
"groupByKeys": groupByKeys,
183+
"hasPrefix": hasPrefix,
184+
"hasSuffix": hasSuffix,
173185
"split": strings.Split,
174186
"replace": strings.Replace,
175187
"dict": dict,

template_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,22 @@ func TestGroupByMulti(t *testing.T) {
104104
}
105105
}
106106

107+
func TestHasPrefix(t *testing.T) {
108+
const prefix = "tcp://"
109+
const str = "tcp://127.0.0.1:2375"
110+
if !hasPrefix(prefix, str) {
111+
t.Fatalf("expected %s to have prefix %s", str, prefix)
112+
}
113+
}
114+
115+
func TestHasSuffix(t *testing.T) {
116+
const suffix = ".local"
117+
const str = "myhost.local"
118+
if !hasSuffix(suffix, str) {
119+
t.Fatalf("expected %s to have suffix %s", str, suffix)
120+
}
121+
}
122+
107123
func TestDict(t *testing.T) {
108124
containers := []*RuntimeContainer{
109125
&RuntimeContainer{

0 commit comments

Comments
 (0)