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

Feature / Allow command fields to implement IsZero() #327

Merged
merged 1 commit into from
Jul 13, 2021
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
16 changes: 15 additions & 1 deletion command_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ import (
"github.com/looplab/eventhorizon/uuid"
)

// IsZeroer is used to check if a type is zero-valued, and in that case
// is not allowed to be used in a command. See CheckCommand
type IsZeroer interface {
IsZero() bool
}

// CommandFieldError is returned by Dispatch when a field is incorrect.
type CommandFieldError struct {
Field string
Expand All @@ -47,7 +53,15 @@ func CheckCommand(cmd Command) error {
continue // Optional field.
}

if isZero(rv.Field(i)) {
var zero bool
switch foo := rv.Field(i).Interface().(type) {
case IsZeroer:
zero = foo.IsZero()
default:
zero = isZero(rv.Field(i))
}

if zero {
return CommandFieldError{field.Name}
}
}
Expand Down
41 changes: 41 additions & 0 deletions command_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,26 @@ func TestCheckCommand(t *testing.T) {
if err == nil || err.Error() != "missing field: StringArray" {
t.Error("there should be a missing field error:", err)
}

// IsZero, fail on zeroable int
err = CheckCommand(&TestCommandZeroableInt{
TestID: uuid.New(),
TestZeroableInt: 0,
TestInt: 0,
})
if err == nil || err.Error() != "missing field: TestZeroableInt"{
t.Error("there should be a missing field error:", err)
}

// IsZero, do not fail on plain int
err = CheckCommand(&TestCommandZeroableInt{
TestID: uuid.New(),
TestZeroableInt: 1,
TestInt: 0,
})
if err != nil {
t.Error("there should not be an error:", err)
}
}

type TestCommandFields struct {
Expand Down Expand Up @@ -288,3 +308,24 @@ var _ = Command(TestCommandArray{})
func (t TestCommandArray) AggregateID() uuid.UUID { return t.TestID }
func (t TestCommandArray) AggregateType() AggregateType { return AggregateType("Test") }
func (t TestCommandArray) CommandType() CommandType { return CommandType("TestCommandArray") }

type ZeroableInt int

type TestCommandZeroableInt struct {
TestID uuid.UUID
TestZeroableInt ZeroableInt
TestInt int
}

var _ = Command(TestCommandZeroableInt{})


func (t TestCommandZeroableInt) AggregateID() uuid.UUID { return t.TestID }
func (t TestCommandZeroableInt) AggregateType() AggregateType { return TestAggregateType }
func (t TestCommandZeroableInt) CommandType() CommandType {
return CommandType("TestCommandZeroableInt")
}

func (z ZeroableInt) IsZero () bool {
return z == 0
}