Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow to chain nil value decorator #431

Merged
merged 2 commits into from
Mar 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions app/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,29 @@ returns a Handler that will execute this whole stack.
)
*/
func ChainDecorators(chain ...weave.Decorator) Decorators {
chain = cutoffNil(chain)
return Decorators{}.Chain(chain...)
}

// Chain allows us to keep adding more Decorators to the chain
func (d Decorators) Chain(chain ...weave.Decorator) Decorators {
chain = cutoffNil(chain)
newChain := append(d.chain, chain...)
return Decorators{newChain}
}

// cutoffNil will in-place remove all all nil values from given slice.
func cutoffNil(ds []weave.Decorator) []weave.Decorator {
husio marked this conversation as resolved.
Show resolved Hide resolved
var cutoff int
for i := 0; i < len(ds); i++ {
ds[i-cutoff] = ds[i]
if ds[i] == nil {
cutoff++
}
}
return ds[:len(ds)-cutoff]
}

// WithHandler resolves the stack and returns a concrete Handler
// that will pass through the chain of decorators before calling
// the final Handler.
Expand Down
75 changes: 75 additions & 0 deletions app/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
"context"
"reflect"
"testing"

"github.com/iov-one/weave"
Expand Down Expand Up @@ -79,3 +80,77 @@ func (ph panicAtHeightDecorator) Deliver(ctx weave.Context, db weave.KVStore, tx
}
return next.Deliver(ctx, db, tx)
}

func TestChainNilDecorator(t *testing.T) {
husio marked this conversation as resolved.
Show resolved Hide resolved
stack := ChainDecorators(nil, &weavetest.Decorator{}, nil, nil)
if want, got := 1, len(stack.chain); want != got {
t.Fatalf("want %d decorator, got %d", want, got)
}

stack = stack.Chain(nil, &weavetest.Decorator{}, nil, nil)
if want, got := 2, len(stack.chain); want != got {
t.Fatalf("want %d decorators, got %d", want, got)
}
}

func TestCutoffNil(t *testing.T) {
// D is a custom implementation that allows instances to can be
// compared by the ID.
type D struct {
weave.Decorator
ID int
}

cases := map[string]struct {
input []weave.Decorator
want []weave.Decorator
}{
"nil input": {
input: nil,
want: nil,
},
"empty input": {
input: []weave.Decorator{},
want: []weave.Decorator{},
},
"only nil": {
input: []weave.Decorator{nil, nil},
want: []weave.Decorator{},
},
"single decorator": {
input: []weave.Decorator{&D{ID: 1}},
want: []weave.Decorator{&D{ID: 1}},
},
"order is preserved": {
input: []weave.Decorator{
nil, &D{ID: 1}, nil,
nil, &D{ID: 2}, nil,
nil, &D{ID: 3}, nil,
},
want: []weave.Decorator{
&D{ID: 1}, &D{ID: 2}, &D{ID: 3},
},
},
"surrounded by nil": {
input: []weave.Decorator{nil, &D{ID: 1}, nil},
want: []weave.Decorator{&D{ID: 1}},
},
"nil on left": {
input: []weave.Decorator{nil, nil, nil, &D{ID: 1}},
want: []weave.Decorator{&D{ID: 1}},
},
"nil on right": {
input: []weave.Decorator{&D{ID: 1}, nil, nil, nil},
want: []weave.Decorator{&D{ID: 1}},
},
}

for testName, tc := range cases {
t.Run(testName, func(t *testing.T) {
got := cutoffNil(tc.input)
if !reflect.DeepEqual(tc.want, got) {
t.Fatalf("unexpected result: %s", tc.want)
}
})
}
}