Skip to content

Commit

Permalink
Add validation scopes to message validation methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalloc committed Oct 3, 2024
1 parent 99f439a commit f85d6e1
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 16 deletions.
12 changes: 6 additions & 6 deletions enginetest/internal/action/behavior.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Fail(message string) []*Action {
}
}

func (x *Action_Fail) do(s Scope) error {
func (x *Action_Fail) do(Scope) error {
return errors.New(x.Fail)
}

Expand Down Expand Up @@ -58,7 +58,7 @@ func ExecuteCommand(c dogma.Command) []*Action {
}

func (x *Action_ExecuteCommand) do(s Scope) error {
s.(executor).ExecuteCommand(fromAny(x.ExecuteCommand))
s.(executor).ExecuteCommand(fromAny[dogma.Command](x.ExecuteCommand))
return nil
}

Expand All @@ -73,7 +73,7 @@ func RecordEvent(e dogma.Message) []*Action {
}

func (x *Action_RecordEvent) do(s Scope) error {
s.(recorder).RecordEvent(fromAny(x.RecordEvent))
s.(recorder).RecordEvent(fromAny[dogma.Event](x.RecordEvent))
return nil
}

Expand All @@ -94,7 +94,7 @@ func ScheduleTimeout(t dogma.Timeout, at time.Time) []*Action {

func (x *Action_ScheduleTimeout) do(s Scope) error {
s.(scheduler).ScheduleTimeout(
fromAny(x.ScheduleTimeout.Timeout),
fromAny[dogma.Timeout](x.ScheduleTimeout.Timeout),
x.ScheduleTimeout.At.AsTime(),
)
return nil
Expand Down Expand Up @@ -137,10 +137,10 @@ func toAny(m dogma.Message) *anypb.Any {
return x
}

func fromAny(m *anypb.Any) dogma.Message {
func fromAny[T dogma.Message](m *anypb.Any) T {
x, err := m.UnmarshalNew()
if err != nil {
panic(err)
}
return x.(dogma.Message)
return x.(T)
}
3 changes: 3 additions & 0 deletions enginetest/internal/action/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Package action provides a mechanism for triggering arbitrary actions within
// handlers by encoding the actions to perform within Dogma messages.
package action
8 changes: 4 additions & 4 deletions enginetest/internal/testapp/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (x *IntegrationCommandA) MessageDescription() string {
}

// Validate returns an error if the message is invalid.
func (x *IntegrationCommandA) Validate() error {
func (x *IntegrationCommandA) Validate(dogma.CommandValidationScope) error {
if x.IsInvalid {
return ErrInvalidIntegrationMessage
}
Expand All @@ -78,7 +78,7 @@ func (x *IntegrationCommandB) MessageDescription() string {
}

// Validate returns an error if the message is invalid.
func (x *IntegrationCommandB) Validate() error {
func (x *IntegrationCommandB) Validate(dogma.CommandValidationScope) error {
if x.IsInvalid {
return ErrInvalidIntegrationMessage
}
Expand All @@ -91,7 +91,7 @@ func (x *IntegrationEventA) MessageDescription() string {
}

// Validate returns an error if the message is invalid.
func (x *IntegrationEventA) Validate() error {
func (x *IntegrationEventA) Validate(dogma.EventValidationScope) error {
return nil
}

Expand All @@ -101,6 +101,6 @@ func (x *IntegrationEventB) MessageDescription() string {
}

// Validate returns an error if the message is invalid.
func (x *IntegrationEventB) Validate() error {
func (x *IntegrationEventB) Validate(dogma.EventValidationScope) error {
return nil
}
8 changes: 5 additions & 3 deletions enginetest/stubs/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package stubs
import (
"errors"
"fmt"

"github.com/dogmatiq/dogma"
)

// CommandStub is a test implementation of [dogma.Command].
Expand All @@ -26,7 +28,7 @@ func (s CommandStub[T]) MessageDescription() string {
}

// Validate returns a non-nil error if c.Invalid is not empty.
func (s CommandStub[T]) Validate() error {
func (s CommandStub[T]) Validate(dogma.CommandValidationScope) error {
if s.ValidationError != "" {
return errors.New(s.ValidationError)
}
Expand Down Expand Up @@ -54,7 +56,7 @@ func (s EventStub[T]) MessageDescription() string {
}

// Validate returns a non-nil error if c.Invalid is not empty.
func (s EventStub[T]) Validate() error {
func (s EventStub[T]) Validate(dogma.EventValidationScope) error {
if s.ValidationError != "" {
return errors.New(s.ValidationError)
}
Expand Down Expand Up @@ -82,7 +84,7 @@ func (s TimeoutStub[T]) MessageDescription() string {
}

// Validate returns a non-nil error if c.Invalid is not empty.
func (s TimeoutStub[T]) Validate() error {
func (s TimeoutStub[T]) Validate(dogma.TimeoutValidationScope) error {
if s.ValidationError != "" {
return errors.New(s.ValidationError)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.23

require (
github.com/dogmatiq/dapper v0.6.0
github.com/dogmatiq/dogma v0.14.3
github.com/dogmatiq/dogma v0.14.4-0.20240926234834-3c0cc27a2ca1
github.com/dogmatiq/primo v0.3.1
github.com/google/go-cmp v0.6.0
google.golang.org/grpc v1.67.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/dogmatiq/dapper v0.6.0 h1:hnWUsjnt3nUiC9hmkPvuxrnMd7fYNz1i+/GS3gOx0Xs=
github.com/dogmatiq/dapper v0.6.0/go.mod h1:ubRHWzt73s0MsPpGhWvnfW/Z/1YPnrkCsQv6CUOZVEw=
github.com/dogmatiq/dogma v0.14.3 h1:qwZqU1yqp80toUJcJBdFxLlh6xvlFd7jb7rycmriRUo=
github.com/dogmatiq/dogma v0.14.3/go.mod h1:9lyVA+6V2+E/exV0IrBOrkUiyFwIATEhv+b0vnB2umQ=
github.com/dogmatiq/dogma v0.14.4-0.20240926234834-3c0cc27a2ca1 h1:SKhtRnDs7CC3ZNMux7lYCxCFz5ZhmeXjvo7/uMNISLo=
github.com/dogmatiq/dogma v0.14.4-0.20240926234834-3c0cc27a2ca1/go.mod h1:9lyVA+6V2+E/exV0IrBOrkUiyFwIATEhv+b0vnB2umQ=
github.com/dogmatiq/jumble v0.1.0 h1:Cb3ExfxY+AoUP4G9/sOwoOdYX8o+kOLK8+dhXAry+QA=
github.com/dogmatiq/jumble v0.1.0/go.mod h1:FCGV2ImXu8zvThxhd4QLstiEdu74vbIVw9bFJSBcKr4=
github.com/dogmatiq/primo v0.3.1 h1:JSqiCh1ma9CbIVzPf8k1vhzQ2Zn/d/WupzElDoiYZw0=
Expand Down

0 comments on commit f85d6e1

Please sign in to comment.