-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f4ce90b
commit b314715
Showing
8 changed files
with
147 additions
and
43 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
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
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
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
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
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
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,69 @@ | ||
package mino | ||
|
||
// addressIterator is an implementation of the iterator for addresses. | ||
// | ||
// - implements mino.AddressIterator | ||
type addressIterator struct { | ||
index int | ||
addrs []Address | ||
} | ||
|
||
// HasNext implements mino.AddressIterator. It returns true if there is an | ||
// address available. | ||
func (it *addressIterator) HasNext() bool { | ||
return it.index < len(it.addrs) | ||
} | ||
|
||
// GetNext implements mino.AddressIterator. It returns the address at the | ||
// current index and moves the iterator to the next address. | ||
func (it *addressIterator) GetNext() Address { | ||
if it.HasNext() { | ||
res := it.addrs[it.index] | ||
it.index++ | ||
return res | ||
} | ||
return nil | ||
} | ||
|
||
// roster is an implementation of the mino.Players interface. It provides helper | ||
// when known addresses need to be grouped into a roster for Mino calls. | ||
// | ||
// - implements mino.Players | ||
type roster struct { | ||
addrs []Address | ||
} | ||
|
||
// NewAddresses is a helper to instantiate a Players interface with only a few | ||
// addresses. | ||
func NewAddresses(addrs ...Address) Players { | ||
return roster{addrs: addrs} | ||
} | ||
|
||
// Take implements mino.Players. It returns a subset of the roster according to | ||
// the filter. | ||
func (r roster) Take(updaters ...FilterUpdater) Players { | ||
filter := &Filter{} | ||
for _, fn := range updaters { | ||
fn(filter) | ||
} | ||
|
||
addrs := make([]Address, len(filter.Indices)) | ||
for i, k := range filter.Indices { | ||
addrs[i] = r.addrs[k] | ||
} | ||
|
||
newRoster := roster{addrs: addrs} | ||
|
||
return newRoster | ||
} | ||
|
||
// AddressIterator implements mino.Players. It returns an iterator for the | ||
// roster. | ||
func (r roster) AddressIterator() AddressIterator { | ||
return &addressIterator{addrs: r.addrs} | ||
} | ||
|
||
// Len implements mino.Players. It returns the length of the roster. | ||
func (r roster) Len() int { | ||
return len(r.addrs) | ||
} |
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,54 @@ | ||
package mino | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAddressIterator_HasNext(t *testing.T) { | ||
addrs := []Address{fakeAddr{}, fakeAddr{}} | ||
|
||
iter := addressIterator{addrs: addrs} | ||
require.True(t, iter.HasNext()) | ||
|
||
iter.GetNext() | ||
iter.GetNext() | ||
require.False(t, iter.HasNext()) | ||
} | ||
|
||
func TestAddressIterator_GetNext(t *testing.T) { | ||
addrs := []Address{fakeAddr{}, fakeAddr{}} | ||
|
||
iter := addressIterator{addrs: addrs} | ||
require.NotNil(t, iter.GetNext()) | ||
require.NotNil(t, iter.GetNext()) | ||
require.Nil(t, iter.GetNext()) | ||
} | ||
|
||
func TestRoster_Take(t *testing.T) { | ||
addrs := NewAddresses(fakeAddr{}, fakeAddr{}, fakeAddr{}) | ||
|
||
addrs2 := addrs.Take(IndexFilter(0), IndexFilter(2)) | ||
require.Equal(t, 2, len(addrs2.(roster).addrs)) | ||
} | ||
|
||
func TestRoster_AddressIterator(t *testing.T) { | ||
addrs := NewAddresses(fakeAddr{}) | ||
|
||
iter := addrs.AddressIterator() | ||
require.NotNil(t, iter.GetNext()) | ||
require.Nil(t, iter.GetNext()) | ||
} | ||
|
||
func TestRoster_Len(t *testing.T) { | ||
addrs := NewAddresses(fakeAddr{}, fakeAddr{}) | ||
require.Equal(t, 2, addrs.Len()) | ||
} | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Utility functions | ||
|
||
type fakeAddr struct { | ||
Address | ||
} |