Skip to content

Commit

Permalink
Merge pull request #38 from dogmatiq/handler-set-ranging
Browse files Browse the repository at this point in the history
Add ranging methods to handler sets.
jmalloc authored Jan 29, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents c36948c + d9f6701 commit f075cfe
Showing 3 changed files with 521 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -16,6 +16,11 @@ The format is based on [Keep a Changelog], and this project adheres to
- **[BC]** Rename `message.NameCollection.Each()` to `Range()` for consistency with the Go standard library
- **[BC]** Rename `message.TypeCollection.Each()` to `Range()` for consistency with the Go standard library

### Added

- Add `HandlerSet.Range[Aggregates|Processes|Integrations|Projections]()`
- Add `RichHandlerSet.Range[Aggregates|Processes|Integrations|Projections]()`

## [0.2.2] - 2020-01-29

### Added
144 changes: 144 additions & 0 deletions handlerset.go
Original file line number Diff line number Diff line change
@@ -147,6 +147,78 @@ func (s HandlerSet) AcceptVisitor(ctx context.Context, v Visitor) error {
return nil
}

// RangeAggregates invokes fn once for each aggregate handler in the set.
//
// Iteration stops when fn returns false or once fn has been invoked for all
// aggregate handlers in the set.
//
// It returns true if fn returned true for all aggregate handlers.
func (s HandlerSet) RangeAggregates(fn func(Aggregate) bool) bool {
for _, h := range s {
if h.HandlerType() == AggregateHandlerType {
if !fn(h) {
return false
}
}
}

return true
}

// RangeProcesses invokes fn once for each process handler in the set.
//
// Iteration stops when fn returns false or once fn has been invoked for all
// process handlers in the set.
//
// It returns true if fn returned true for all process handlers.
func (s HandlerSet) RangeProcesses(fn func(Process) bool) bool {
for _, h := range s {
if h.HandlerType() == ProcessHandlerType {
if !fn(h) {
return false
}
}
}

return true
}

// RangeIntegrations invokes fn once for each integration handler in the set.
//
// Iteration stops when fn returns false or once fn has been invoked for all
// integration handlers in the set.
//
// It returns true if fn returned true for all integration handlers.
func (s HandlerSet) RangeIntegrations(fn func(Integration) bool) bool {
for _, h := range s {
if h.HandlerType() == IntegrationHandlerType {
if !fn(h) {
return false
}
}
}

return true
}

// RangeProjections invokes fn once for each projection handler in the set.
//
// Iteration stops when fn returns false or once fn has been invoked for all
// projection handlers in the set.
//
// It returns true if fn returned true for all projection handlers.
func (s HandlerSet) RangeProjections(fn func(Projection) bool) bool {
for _, h := range s {
if h.HandlerType() == ProjectionHandlerType {
if !fn(h) {
return false
}
}
}

return true
}

// RichHandlerSet is a collection of rich handlers.
type RichHandlerSet map[Identity]RichHandler

@@ -313,3 +385,75 @@ func (s RichHandlerSet) AcceptRichVisitor(ctx context.Context, v RichVisitor) er

return nil
}

// RangeAggregates invokes fn once for each aggregate handler in the set.
//
// Iteration stops when fn returns false or once fn has been invoked for all
// aggregate handlers in the set.
//
// It returns true if fn returned true for all aggregate handlers.
func (s RichHandlerSet) RangeAggregates(fn func(RichAggregate) bool) bool {
for _, h := range s {
if x, ok := h.(RichAggregate); ok {
if !fn(x) {
return false
}
}
}

return true
}

// RangeProcesses invokes fn once for each process handler in the set.
//
// Iteration stops when fn returns false or once fn has been invoked for all
// process handlers in the set.
//
// It returns true if fn returned true for all process handlers.
func (s RichHandlerSet) RangeProcesses(fn func(RichProcess) bool) bool {
for _, h := range s {
if x, ok := h.(RichProcess); ok {
if !fn(x) {
return false
}
}
}

return true
}

// RangeIntegrations invokes fn once for each integration handler in the set.
//
// Iteration stops when fn returns false or once fn has been invoked for all
// integration handlers in the set.
//
// It returns true if fn returned true for all integration handlers.
func (s RichHandlerSet) RangeIntegrations(fn func(RichIntegration) bool) bool {
for _, h := range s {
if x, ok := h.(RichIntegration); ok {
if !fn(x) {
return false
}
}
}

return true
}

// RangeProjections invokes fn once for each projection handler in the set.
//
// Iteration stops when fn returns false or once fn has been invoked for all
// projection handlers in the set.
//
// It returns true if fn returned true for all projection handlers.
func (s RichHandlerSet) RangeProjections(fn func(RichProjection) bool) bool {
for _, h := range s {
if x, ok := h.(RichProjection); ok {
if !fn(x) {
return false
}
}
}

return true
}
Loading

0 comments on commit f075cfe

Please sign in to comment.