Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit 4b179b1

Browse files
author
David Chung
authored
Merge fixes from Release 0.3.0 (#383)
Signed-off-by: David Chung <[email protected]>
1 parent c18e3e1 commit 4b179b1

File tree

3 files changed

+49
-21
lines changed

3 files changed

+49
-21
lines changed

pkg/template/funcs.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ func UnixTime() interface{} {
7676
return time.Now().Unix()
7777
}
7878

79-
// Index returns the index of search in array. -1 if not found or array is not iterable. An optional true will
79+
// IndexOf returns the index of search in array. -1 if not found or array is not iterable. An optional true will
8080
// turn on strict type check while by default string representations are used to compare values.
81-
func Index(srch interface{}, array interface{}, strictOptional ...bool) int {
81+
func IndexOf(srch interface{}, array interface{}, strictOptional ...bool) int {
8282
strict := false
8383
if len(strictOptional) > 0 {
8484
strict = strictOptional[0]
@@ -153,6 +153,6 @@ func (t *Template) DefaultFuncs() map[string]interface{} {
153153
"lines": SplitLines,
154154
"to_json": ToJSON,
155155
"from_json": FromJSON,
156-
"index": Index,
156+
"index_of": IndexOf,
157157
}
158158
}

pkg/template/funcs_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -281,29 +281,29 @@ func TestMapEncodeDecode(t *testing.T) {
281281
require.Equal(t, expect, actual)
282282
}
283283

284-
func TestIndex(t *testing.T) {
285-
require.Equal(t, -1, Index("a", []string{"x", "y", "z"}))
286-
require.Equal(t, 1, Index("y", []string{"x", "y", "z"}))
287-
require.Equal(t, -1, Index(25, []string{"x", "y", "z"}))
288-
require.Equal(t, -1, Index(25, 26))
289-
require.Equal(t, 1, Index("y", []string{"x", "y", "z"}))
290-
require.Equal(t, 1, Index("y", []interface{}{"x", "y", "z"}))
291-
require.Equal(t, 1, Index(1, []interface{}{0, 1, 2}))
292-
require.Equal(t, 1, Index("1", []interface{}{0, 1, 2}))
293-
require.Equal(t, 1, Index(1, []interface{}{0, "1", 2}))
294-
require.Equal(t, -1, Index("1", []interface{}{0, 1, 2}, true)) // strict case type must match
295-
require.Equal(t, 1, Index("1", []interface{}{0, "1", 2}, true)) // strict case type must match
296-
require.Equal(t, -1, Index(1, []interface{}{0, "1", 2}, true)) // strict case type must match
284+
func TestIndexOf(t *testing.T) {
285+
require.Equal(t, -1, IndexOf("a", []string{"x", "y", "z"}))
286+
require.Equal(t, 1, IndexOf("y", []string{"x", "y", "z"}))
287+
require.Equal(t, -1, IndexOf(25, []string{"x", "y", "z"}))
288+
require.Equal(t, -1, IndexOf(25, 26))
289+
require.Equal(t, 1, IndexOf("y", []string{"x", "y", "z"}))
290+
require.Equal(t, 1, IndexOf("y", []interface{}{"x", "y", "z"}))
291+
require.Equal(t, 1, IndexOf(1, []interface{}{0, 1, 2}))
292+
require.Equal(t, 1, IndexOf("1", []interface{}{0, 1, 2}))
293+
require.Equal(t, 1, IndexOf(1, []interface{}{0, "1", 2}))
294+
require.Equal(t, -1, IndexOf("1", []interface{}{0, 1, 2}, true)) // strict case type must match
295+
require.Equal(t, 1, IndexOf("1", []interface{}{0, "1", 2}, true)) // strict case type must match
296+
require.Equal(t, -1, IndexOf(1, []interface{}{0, "1", 2}, true)) // strict case type must match
297297

298298
v := "1"
299-
require.Equal(t, 1, Index(&v, []interface{}{0, "1", 2}))
300-
require.Equal(t, 1, Index(&v, []interface{}{0, &v, 2}, true))
301-
require.Equal(t, 1, Index(&v, []interface{}{0, &v, 2}))
299+
require.Equal(t, 1, IndexOf(&v, []interface{}{0, "1", 2}))
300+
require.Equal(t, 1, IndexOf(&v, []interface{}{0, &v, 2}, true))
301+
require.Equal(t, 1, IndexOf(&v, []interface{}{0, &v, 2}))
302302

303303
a := "0"
304304
c := "2"
305-
require.Equal(t, 1, Index("1", []*string{&a, &v, &c}))
305+
require.Equal(t, 1, IndexOf("1", []*string{&a, &v, &c}))
306306

307307
// This doesn't work because the type information is gone and we have just an address
308-
require.Equal(t, -1, Index("1", []interface{}{0, &v, 2}))
308+
require.Equal(t, -1, IndexOf("1", []interface{}{0, &v, 2}))
309309
}

pkg/template/integration_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,31 @@ The message is {{str}}
158158
require.True(t, context.Bool)
159159
require.Equal(t, 23, context.invokes) // note this is private state not accessible in template
160160
}
161+
162+
func TestIndexIndexOf(t *testing.T) {
163+
164+
{
165+
tt, err := NewTemplate("str://{{ index . 1 }}", Options{})
166+
require.NoError(t, err)
167+
168+
view, err := tt.Render([]string{"a", "b", "c", "d"})
169+
require.NoError(t, err)
170+
require.Equal(t, "b", view)
171+
}
172+
{
173+
tt, err := NewTemplate(`str://{{ index_of "c" . }}`, Options{})
174+
require.NoError(t, err)
175+
176+
view, err := tt.Render([]string{"a", "b", "c", "d"})
177+
require.NoError(t, err)
178+
require.Equal(t, "2", view)
179+
}
180+
{
181+
tt, err := NewTemplate(`str://{{ index . 0 | cat "index-" | nospace }}`, Options{})
182+
require.NoError(t, err)
183+
184+
view, err := tt.Render([]string{"a", "b", "c", "d"})
185+
require.NoError(t, err)
186+
require.Equal(t, "index-a", view)
187+
}
188+
}

0 commit comments

Comments
 (0)