-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add filter middleware for simpler logic
- Loading branch information
Showing
3 changed files
with
136 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// Copyright (c) 2018 Dean Jackson <[email protected]> | ||
// | ||
// MIT Licence. See http://opensource.org/licenses/MIT | ||
// | ||
// Created on 2018-01-27 | ||
// | ||
|
||
package main | ||
|
||
// Filterer passes selectively passes through strings | ||
type Filterer func(in <-chan string) <-chan string | ||
|
||
// Filter is a chain of Filterers | ||
type Filter struct { | ||
Funcs []Filterer | ||
} | ||
|
||
// Use adds a Filterer to the stack | ||
func (f *Filter) Use(fn Filterer) { | ||
f.Funcs = append(f.Funcs, fn) | ||
} | ||
|
||
// Apply runs the filter on a channel. | ||
func (f *Filter) Apply(in <-chan string) <-chan string { | ||
|
||
var out = make(<-chan string) | ||
|
||
// Make stack of handlers | ||
out = f.Funcs[len(f.Funcs)-1](in) | ||
for i := len(f.Funcs) - 2; i >= 0; i-- { | ||
out = f.Funcs[i](out) | ||
} | ||
|
||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// | ||
// Copyright (c) 2018 Dean Jackson <[email protected]> | ||
// | ||
// MIT Licence. See http://opensource.org/licenses/MIT | ||
// | ||
// Created on 2018-01-27 | ||
// | ||
|
||
package main | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func TestFilter(t *testing.T) { | ||
data := []struct { | ||
in, out []string | ||
}{ | ||
{[]string{""}, []string{}}, | ||
{[]string{"file", "file.txt"}, []string{"file.txt"}}, | ||
{[]string{"file.txt", "file.pdf"}, []string{"file.txt", "file.pdf"}}, | ||
{[]string{"file.mp4", "file.pdf"}, []string{"file.pdf"}}, | ||
} | ||
|
||
for _, td := range data { | ||
|
||
var in = make(chan string) | ||
|
||
// Generate input | ||
go func(c chan string, data []string) { | ||
|
||
for _, s := range data { | ||
if s == "" { | ||
continue | ||
} | ||
x := filepath.Ext(s) | ||
if x == ".mp4" || x == "" { | ||
continue | ||
} | ||
c <- s | ||
} | ||
close(in) | ||
}(in, td.in) | ||
|
||
f := Filter{} | ||
f.Use(func(in <-chan string) <-chan string { | ||
var out = make(chan string) | ||
|
||
go func() { | ||
defer close(out) | ||
|
||
for s := range in { | ||
out <- s | ||
} | ||
|
||
}() | ||
|
||
return out | ||
}) | ||
|
||
out := f.Apply(in) | ||
res := []string{} | ||
for s := range out { | ||
res = append(res, s) | ||
} | ||
|
||
if !strSlicesEqual(res, td.out) { | ||
t.Errorf("Bad Filter. Expected=%#v, Got=%#v", td.out, res) | ||
} | ||
} | ||
|
||
} | ||
|
||
func strSlicesEqual(s1, s2 []string) bool { | ||
if len(s1) != len(s2) { | ||
return false | ||
} | ||
|
||
for i, s := range s1 { | ||
if s != s2[i] { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters