Skip to content

Commit 7b0dc47

Browse files
committed
refactor: replace docker-gen functions with sprig identical functions
(2/2)
1 parent 61e3cfd commit 7b0dc47

File tree

3 files changed

+9
-119
lines changed

3 files changed

+9
-119
lines changed

internal/template/functions.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@ import (
77
"reflect"
88
"regexp"
99
"strings"
10-
11-
"github.com/Masterminds/sprig/v3"
1210
)
1311

14-
var sprigFuncMap = sprig.TxtFuncMap()
15-
1612
func keys(input interface{}) (interface{}, error) {
1713
if input == nil {
1814
return nil, nil
@@ -81,18 +77,6 @@ func contains(input interface{}, key interface{}) bool {
8177
return false
8278
}
8379

84-
func hashSha1(input string) string {
85-
return sprigFuncMap["sha1sum"].(func(string) string)(input)
86-
}
87-
88-
func marshalJson(input interface{}) (string, error) {
89-
return sprigFuncMap["mustToJson"].(func(interface{}) (string, error))(input)
90-
}
91-
92-
func unmarshalJson(input string) (interface{}, error) {
93-
return sprigFuncMap["mustFromJson"].(func(string) (interface{}, error))(input)
94-
}
95-
9680
// arrayClosest find the longest matching substring in values
9781
// that matches input
9882
func arrayClosest(values []string, input string) string {
@@ -119,21 +103,6 @@ func dirList(path string) ([]string, error) {
119103
return names, nil
120104
}
121105

122-
// coalesce returns the first non nil argument
123-
func coalesce(input ...interface{}) interface{} {
124-
return sprigFuncMap["coalesce"].(func(...interface{}) interface{})(input...)
125-
}
126-
127-
// trimPrefix returns a string without the prefix, if present
128-
func trimPrefix(prefix, s string) string {
129-
return sprigFuncMap["trimPrefix"].(func(string, string) string)(prefix, s)
130-
}
131-
132-
// trimSuffix returns a string without the suffix, if present
133-
func trimSuffix(suffix, s string) string {
134-
return sprigFuncMap["trimSuffix"].(func(string, string) string)(suffix, s)
135-
}
136-
137106
// when returns the trueValue when the condition is true and the falseValue otherwise
138107
func when(condition bool, trueValue, falseValue interface{}) interface{} {
139108
if condition {

internal/template/functions_test.go

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package template
22

33
import (
4-
"bytes"
5-
"encoding/json"
64
"os"
75
"path"
86
"reflect"
97
"testing"
108

11-
"github.com/nginx-proxy/docker-gen/internal/context"
129
"github.com/stretchr/testify/assert"
1310
)
1411

@@ -130,73 +127,6 @@ func TestSplitN(t *testing.T) {
130127
tests.run(t)
131128
}
132129

133-
func TestTrimPrefix(t *testing.T) {
134-
const prefix = "tcp://"
135-
const str = "tcp://127.0.0.1:2375"
136-
const trimmed = "127.0.0.1:2375"
137-
got := trimPrefix(prefix, str)
138-
if got != trimmed {
139-
t.Fatalf("expected trimPrefix(%s,%s) to be %s, got %s", prefix, str, trimmed, got)
140-
}
141-
}
142-
143-
func TestTrimSuffix(t *testing.T) {
144-
const suffix = ".local"
145-
const str = "myhost.local"
146-
const trimmed = "myhost"
147-
got := trimSuffix(suffix, str)
148-
if got != trimmed {
149-
t.Fatalf("expected trimSuffix(%s,%s) to be %s, got %s", suffix, str, trimmed, got)
150-
}
151-
}
152-
153-
func TestSha1(t *testing.T) {
154-
sum := hashSha1("/path")
155-
if sum != "4f26609ad3f5185faaa9edf1e93aa131e2131352" {
156-
t.Fatal("Incorrect SHA1 sum")
157-
}
158-
}
159-
160-
func TestJson(t *testing.T) {
161-
containers := []*context.RuntimeContainer{
162-
{
163-
Env: map[string]string{
164-
"VIRTUAL_HOST": "demo1.localhost",
165-
},
166-
ID: "1",
167-
},
168-
{
169-
Env: map[string]string{
170-
"VIRTUAL_HOST": "demo1.localhost,demo3.localhost",
171-
},
172-
ID: "2",
173-
},
174-
{
175-
Env: map[string]string{
176-
"VIRTUAL_HOST": "demo2.localhost",
177-
},
178-
ID: "3",
179-
},
180-
}
181-
output, err := marshalJson(containers)
182-
if err != nil {
183-
t.Fatal(err)
184-
}
185-
186-
buf := bytes.NewBufferString(output)
187-
dec := json.NewDecoder(buf)
188-
if err != nil {
189-
t.Fatal(err)
190-
}
191-
var decoded []*context.RuntimeContainer
192-
if err := dec.Decode(&decoded); err != nil {
193-
t.Fatal(err)
194-
}
195-
if len(decoded) != len(containers) {
196-
t.Fatalf("Incorrect unmarshaled container count. Expected %d, got %d.", len(containers), len(decoded))
197-
}
198-
}
199-
200130
func TestParseJson(t *testing.T) {
201131
tests := templateTestList{
202132
{`{{parseJson .}}`, `null`, `<no value>`},
@@ -306,11 +236,3 @@ func TestDirList(t *testing.T) {
306236
filesList, _ = dirList("/wrong/path")
307237
assert.Equal(t, []string{}, filesList)
308238
}
309-
310-
func TestCoalesce(t *testing.T) {
311-
v := coalesce(nil, "second", "third")
312-
assert.Equal(t, "second", v, "Expected second value")
313-
314-
v = coalesce(nil, nil, nil)
315-
assert.Nil(t, v, "Expected nil value")
316-
}

internal/template/template.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ func newTemplate(name string) *template.Template {
5757
}
5858
return buf.String(), nil
5959
}
60-
tmpl.Funcs(sprig.TxtFuncMap()).Funcs(template.FuncMap{
60+
61+
sprigFuncMap := sprig.TxtFuncMap()
62+
63+
return tmpl.Funcs(sprigFuncMap).Funcs(template.FuncMap{
6164
"closest": arrayClosest,
6265
"comment": comment,
6366
"contains": contains,
@@ -86,8 +89,6 @@ func newTemplate(name string) *template.Template {
8689
"sortStringsDesc": sortStringsDesc,
8790
"sortObjectsByKeysAsc": sortObjectsByKeysAsc,
8891
"sortObjectsByKeysDesc": sortObjectsByKeysDesc,
89-
"trimPrefix": trimPrefix,
90-
"trimSuffix": trimSuffix,
9192
"toLower": strings.ToLower,
9293
"toUpper": strings.ToUpper,
9394
"when": when,
@@ -100,14 +101,12 @@ func newTemplate(name string) *template.Template {
100101
"whereLabelExists": whereLabelExists,
101102
"whereLabelDoesNotExist": whereLabelDoesNotExist,
102103
"whereLabelValueMatches": whereLabelValueMatches,
103-
}).Funcs(template.FuncMap{
104-
// docker-gen template function replaced by their Sprig clone
105-
"coalesce": coalesce,
106-
"json": marshalJson,
107-
"parseJson": unmarshalJson,
108-
"sha1": hashSha1,
104+
105+
// docker-gen template function aliased to their Sprig clone
106+
"json": sprigFuncMap["mustToJson"],
107+
"parseJson": sprigFuncMap["mustFromJson"],
108+
"sha1": sprigFuncMap["sha1sum"],
109109
})
110-
return tmpl
111110
}
112111

113112
func isBlank(str string) bool {

0 commit comments

Comments
 (0)