Skip to content

Commit 77fdfaf

Browse files
author
Rousseau Thibaut
committed
feat: Add "whereNot" function
1 parent 636e130 commit 77fdfaf

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ For example, this is a JSON version of an emitted RuntimeContainer struct:
373373
* *`trim $string`*: Removes whitespace from both sides of `$string`.
374374
* *`when $condition $trueValue $falseValue`*: Returns the `$trueValue` when the `$condition` is `true` and the `$falseValue` otherwise
375375
* *`where $items $fieldPath $value`*: Filters an array or slice 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. Returns an array of items having that value.
376+
* *`whereNot $items $fieldPath $value`*: Filters an array or slice 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. Returns an array of items **not** having that value.
376377
* *`whereExist $items $fieldPath`*: Like `where`, but returns only items where `$fieldPath` exists (is not nil).
377378
* *`whereNotExist $items $fieldPath`*: Like `where`, but returns only items where `$fieldPath` does not exist (is nil).
378379
* *`whereAny $items $fieldPath $sep $values`*: Like `where`, but the string value specified by `$fieldPath` is first split by `$sep` into a list of strings. The comparison value is a string slice with possible matches. Returns items which OR intersect these values.

template.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,13 @@ func where(entries interface{}, key string, cmp interface{}) (interface{}, error
156156
})
157157
}
158158

159+
// select entries where a key is not equal to a value
160+
func whereNot(entries interface{}, key string, cmp interface{}) (interface{}, error) {
161+
return generalizedWhere("whereNot", entries, key, func(value interface{}) bool {
162+
return !reflect.DeepEqual(value, cmp)
163+
})
164+
}
165+
159166
// selects entries where a key exists
160167
func whereExist(entries interface{}, key string) (interface{}, error) {
161168
return generalizedWhere("whereExist", entries, key, func(value interface{}) bool {
@@ -442,6 +449,7 @@ func newTemplate(name string) *template.Template {
442449
"trim": trim,
443450
"when": when,
444451
"where": where,
452+
"whereNot": whereNot,
445453
"whereExist": whereExist,
446454
"whereNotExist": whereNotExist,
447455
"whereAny": whereAny,

template_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,71 @@ func TestWhere(t *testing.T) {
351351
tests.run(t, "where")
352352
}
353353

354+
func TestWhereNot(t *testing.T) {
355+
containers := []*RuntimeContainer{
356+
&RuntimeContainer{
357+
Env: map[string]string{
358+
"VIRTUAL_HOST": "demo1.localhost",
359+
},
360+
ID: "1",
361+
Addresses: []Address{
362+
Address{
363+
IP: "172.16.42.1",
364+
Port: "80",
365+
Proto: "tcp",
366+
},
367+
},
368+
},
369+
&RuntimeContainer{
370+
Env: map[string]string{
371+
"VIRTUAL_HOST": "demo2.localhost",
372+
},
373+
ID: "2",
374+
Addresses: []Address{
375+
Address{
376+
IP: "172.16.42.1",
377+
Port: "9999",
378+
Proto: "tcp",
379+
},
380+
},
381+
},
382+
&RuntimeContainer{
383+
Env: map[string]string{
384+
"VIRTUAL_HOST": "demo3.localhost",
385+
},
386+
ID: "3",
387+
},
388+
&RuntimeContainer{
389+
Env: map[string]string{
390+
"VIRTUAL_HOST": "demo2.localhost",
391+
},
392+
ID: "4",
393+
},
394+
}
395+
396+
tests := templateTestList{
397+
{`{{whereNot . "Env.VIRTUAL_HOST" "demo1.localhost" | len}}`, containers, `3`},
398+
{`{{whereNot . "Env.VIRTUAL_HOST" "demo2.localhost" | len}}`, containers, `2`},
399+
{`{{whereNot . "Env.VIRTUAL_HOST" "demo3.localhost" | len}}`, containers, `3`},
400+
{`{{whereNot . "Env.NOEXIST" "demo3.localhost" | len}}`, containers, `4`},
401+
{`{{whereNot .Addresses "Port" "80" | len}}`, containers[0], `0`},
402+
{`{{whereNot .Addresses "Port" "80" | len}}`, containers[1], `1`},
403+
{
404+
`{{whereNot . "Value" 5 | len}}`,
405+
[]struct {
406+
Value int
407+
}{
408+
{Value: 5},
409+
{Value: 3},
410+
{Value: 5},
411+
},
412+
`1`,
413+
},
414+
}
415+
416+
tests.run(t, "whereNot")
417+
}
418+
354419
func TestWhereExist(t *testing.T) {
355420
containers := []*RuntimeContainer{
356421
&RuntimeContainer{

0 commit comments

Comments
 (0)