Skip to content

Commit

Permalink
chore: Configure linter, fix linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
maxekman committed Oct 22, 2021
1 parent 1346591 commit c1c8f1f
Show file tree
Hide file tree
Showing 112 changed files with 942 additions and 76 deletions.
18 changes: 18 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,21 @@ linters:
- interfacer
- maligned
- scopelint

- gomnd # Ignore "magic numbers", used for event versions etc.
- godox # Allow TODOs.
- goconst # Not sure we want ta have consts for some dups.
- gochecknoglobals # We rely on globals for some type registrations.
- gochecknoinits # We rely on inits for some type registrations.
- paralleltest # Did not improve test times much (at all?).
- testpackage # Disabled for now as it requires substantial changes.
- tagliatelle # Undesired style advice.
- gofumpt # Not sure we want to run gofumpt.
- gci # Not sure we want to run gci.

- lll # Disabled until fixed.
- funlen # Disabled until fixed.
- dupl # Ignore duplicate code for now.
- gocognit # Disabled until fixed.
- nestif # Disabled until fixed.
- gocyclo # Disabled until fixed.
9 changes: 7 additions & 2 deletions aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func (e *AggregateStoreError) Error() string {
if e.AggregateType != "" {
at = string(e.AggregateType)
}

str += fmt.Sprintf(", %s(%s)", at, e.AggregateID)
}

Expand Down Expand Up @@ -149,24 +150,26 @@ func (e *AggregateError) Cause() error {
// An example would be:
// RegisterAggregate(func(id UUID) Aggregate { return &MyAggregate{id} })
func RegisterAggregate(factory func(uuid.UUID) Aggregate) {
// Check that the created aggregate matches the registered type.
// TODO: Explore the use of reflect/gob for creating concrete types without
// a factory func.

// Check that the created aggregate matches the registered type.
aggregate := factory(uuid.New())
if aggregate == nil {
panic("eventhorizon: created aggregate is nil")
}

aggregateType := aggregate.AggregateType()
if aggregateType == AggregateType("") {
panic("eventhorizon: attempt to register empty aggregate type")
}

aggregatesMu.Lock()
defer aggregatesMu.Unlock()

if _, ok := aggregates[aggregateType]; ok {
panic(fmt.Sprintf("eventhorizon: registering duplicate types for %q", aggregateType))
}

aggregates[aggregateType] = factory
}

Expand All @@ -175,9 +178,11 @@ func RegisterAggregate(factory func(uuid.UUID) Aggregate) {
func CreateAggregate(aggregateType AggregateType, id uuid.UUID) (Aggregate, error) {
aggregatesMu.RLock()
defer aggregatesMu.RUnlock()

if factory, ok := aggregates[aggregateType]; ok {
return factory(id), nil
}

return nil, ErrAggregateNotRegistered
}

Expand Down
2 changes: 2 additions & 0 deletions aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
func TestCreateAggregate(t *testing.T) {
id := uuid.New()
_, err := CreateAggregate(TestAggregateRegisterType, id)

if !errors.Is(err, ErrAggregateNotRegistered) {
t.Error("there should be a aggregate not registered error:", err)
}
Expand All @@ -41,6 +42,7 @@ func TestCreateAggregate(t *testing.T) {
if aggregate.AggregateType() != TestAggregateRegisterType {
t.Error("the aggregate type should be correct:", aggregate.AggregateType())
}

if aggregate.EntityID() != id {
t.Error("the ID should be correct:", aggregate.EntityID())
}
Expand Down
1 change: 1 addition & 0 deletions aggregatestore/events/aggregatebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,6 @@ func (a *AggregateBase) AppendEvent(t eh.EventType, data eh.EventData, timestamp
)
e := eh.NewEvent(t, data, timestamp, options...)
a.events = append(a.events, e)

return e
}
22 changes: 22 additions & 0 deletions aggregatestore/events/aggregatebase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,20 @@ import (

func TestNewAggregateBase(t *testing.T) {
id := uuid.New()

agg := NewAggregateBase(TestAggregateType, id)
if agg == nil {
t.Fatal("there should be an aggregate")
}

if agg.AggregateType() != TestAggregateType {
t.Error("the aggregate type should be correct: ", agg.AggregateType(), TestAggregateType)
}

if agg.EntityID() != id {
t.Error("the entity ID should be correct: ", agg.EntityID(), id)
}

if agg.AggregateVersion() != 0 {
t.Error("the version should be 0:", agg.AggregateVersion())
}
Expand All @@ -48,6 +52,7 @@ func TestAggregateVersion(t *testing.T) {
}

agg.SetAggregateVersion(51)

if agg.AggregateVersion() != 51 {
t.Error("the version should be 1:", agg.AggregateVersion())
}
Expand All @@ -58,31 +63,40 @@ func TestAggregateEvents(t *testing.T) {
agg := NewTestAggregate(id)
timestamp := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
event1 := agg.AppendEvent(TestAggregateEventType, &TestEventData{"event1"}, timestamp)

if event1.EventType() != TestAggregateEventType {
t.Error("the event type should be correct:", event1.EventType())
}

if !reflect.DeepEqual(event1.Data(), &TestEventData{"event1"}) {
t.Error("the data should be correct:", event1.Data())
}

if !event1.Timestamp().Equal(timestamp) {
t.Error("the timestamp should not be zero:", event1.Timestamp())
}

if event1.AggregateType() != TestAggregateType {
t.Error("the aggregate type should be correct:", event1.AggregateType())
}

if event1.AggregateID() != id {
t.Error("the aggregate ID should be correct:", event1.AggregateID())
}

if event1.Version() != 1 {
t.Error("the version should be 1:", event1.Version())
}

if event1.String() != "TestAggregateEvent("+id.String()+", v1)" {
t.Error("the string representation should be correct:", event1.String())
}

events := agg.events
if len(events) != 1 {
t.Fatal("there should be one event provided:", len(events))
}

if events[0] != event1 {
t.Error("the provided event should be correct:", events[0])
}
Expand All @@ -91,32 +105,39 @@ func TestAggregateEvents(t *testing.T) {
if event2.Version() != 2 {
t.Error("the version should be 2:", event2.Version())
}

events = agg.UncommittedEvents()
if len(events) != 2 {
t.Error("there should be two events provided:", len(events))
}

agg.ClearUncommittedEvents()

event3 := agg.AppendEvent(TestAggregateEventType, &TestEventData{"event1"}, timestamp)
if event3.Version() != 1 {
t.Error("the version should be 1 after clearing uncommitted events (without applying any):", event3.Version())
}

events = agg.UncommittedEvents()
if len(events) != 1 {
t.Error("there should be one new event provided:", len(events))
}

agg.ClearUncommittedEvents()

agg = NewTestAggregate(uuid.New())
event1 = agg.AppendEvent(TestAggregateEventType, &TestEventData{"event1"}, timestamp)
event2 = agg.AppendEvent(TestAggregateEventType, &TestEventData{"event2"}, timestamp)
events = agg.events

if len(events) != 2 {
t.Fatal("there should be 2 events provided:", len(events))
}

if events[0] != event1 {
t.Error("the first provided event should be correct:", events[0])
}

if events[1] != event2 {
t.Error("the second provided event should be correct:", events[0])
}
Expand Down Expand Up @@ -170,5 +191,6 @@ func (a *TestAggregate) HandleCommand(ctx context.Context, cmd eh.Command) error

func (a *TestAggregate) ApplyEvent(ctx context.Context, event eh.Event) error {
a.event = event

return nil
}
Loading

0 comments on commit c1c8f1f

Please sign in to comment.