Skip to content

Commit 6f9f222

Browse files
committed
Adding Prepend to HanderList
Prepend will allow for prepending to a given handler list adding flexbility to the handler list. Signed-off-by: xibz <[email protected]>
1 parent 75ee9e9 commit 6f9f222

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

handlers.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,13 @@ type HandlerList struct {
251251
list []Handler
252252
}
253253

254+
// Prepend will prepend a new set of handlers to the handler list
255+
func (l HandlerList) Prepend(handlers ...Handler) HandlerList {
256+
l.list = append(handlers, l.list...)
257+
258+
return l
259+
}
260+
254261
// Append will append a new handler to the handler list.
255262
func (l HandlerList) Append(handlers ...Handler) HandlerList {
256263
l.list = append(l.list, handlers...)

handlers_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,31 @@ func TestHandlerListAppend(t *testing.T) {
3838
}
3939
}
4040

41+
func TestHandlerListPrepend(t *testing.T) {
42+
h := HandlerList{}
43+
h.Prepend(Handler{Name: "foo"})
44+
45+
if size := h.Len(); size != 0 {
46+
t.Errorf("expected length to be '0', but received '%d'", size)
47+
}
48+
49+
expectedNames := []string{
50+
"foo",
51+
"bar",
52+
"baz",
53+
}
54+
55+
for _, name := range expectedNames {
56+
h = h.Prepend(Handler{Name: name})
57+
}
58+
59+
for i, name := range expectedNames {
60+
if e, a := name, h.list[len(h.list)-i-1].Name; e != a {
61+
t.Errorf("expected %q, but received %q", e, a)
62+
}
63+
}
64+
}
65+
4166
func TestHandlerListRemove(t *testing.T) {
4267
h := HandlerList{}.Append(
4368
Handler{

0 commit comments

Comments
 (0)