generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #306 from dogmatiq/iface-handler-test
Add support for generic handlers.
- Loading branch information
Showing
3 changed files
with
183 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Interface as an entity configurer. | ||
|
||
This test ensures that the static analyzer can recognize the type of a handler | ||
when it is used in instantiating a generic handler. | ||
|
||
```go au:input | ||
package app | ||
|
||
import ( | ||
"context" | ||
. "github.com/dogmatiq/dogma" | ||
. "github.com/dogmatiq/enginekit/enginetest/stubs" | ||
) | ||
|
||
type GenericIntegration[T any, H IntegrationMessageHandler] struct { | ||
Handler H | ||
} | ||
|
||
func (i *GenericIntegration[T, H]) Configure(c IntegrationConfigurer) { | ||
i.Handler.Configure(c) | ||
} | ||
|
||
func (i *GenericIntegration[T, H]) HandleCommand( | ||
ctx context.Context, | ||
s IntegrationCommandScope, | ||
cmd Command, | ||
) error { | ||
return i.Handler.HandleCommand(ctx, s, cmd) | ||
} | ||
|
||
type integrationHandler struct {} | ||
|
||
func (integrationHandler) Configure(c IntegrationConfigurer) { | ||
c.Identity("<integration>", "abc7c329-c9da-4161-a8e2-6ab45be2dd83") | ||
|
||
routes := []IntegrationRoute{ | ||
HandlesCommand[CommandStub[TypeA]](), | ||
} | ||
|
||
c.Routes(routes...) | ||
} | ||
|
||
func (integrationHandler) HandleCommand( | ||
_ context.Context, | ||
_ IntegrationCommandScope, | ||
_ Command, | ||
) error { | ||
return nil | ||
} | ||
|
||
type InstantiatedIntegration = GenericIntegration[struct{}, integrationHandler] | ||
|
||
type App struct { | ||
Integration InstantiatedIntegration | ||
} | ||
|
||
func (a App) Configure(c ApplicationConfigurer) { | ||
c.Identity("<app>", "e522c782-48d2-4c47-a4c9-81e0d7cdeba0") | ||
c.RegisterIntegration(&a.Integration) | ||
} | ||
|
||
``` | ||
|
||
```au:output | ||
application <app> (e522c782-48d2-4c47-a4c9-81e0d7cdeba0) App | ||
- integration <integration> (abc7c329-c9da-4161-a8e2-6ab45be2dd83) *GenericIntegration[struct{}, integrationHandler] | ||
handles CommandStub[TypeA]? | ||
``` |
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,33 @@ | ||
# Interface as an entity configurer. | ||
|
||
This test ensures that the static analyzer does not behaves abnormally when it | ||
encounters an interface that handles configuration. In this case, the static | ||
analysis is not capable of gathering data about what particular entity is | ||
configured behind the interface. | ||
|
||
```go au:input | ||
package app | ||
|
||
import ( | ||
. "github.com/dogmatiq/dogma" | ||
) | ||
|
||
|
||
type Configurer interface { | ||
ApplyConfiguration(c ApplicationConfigurer) | ||
} | ||
|
||
type App struct { | ||
C Configurer | ||
} | ||
|
||
func (a App) Configure(c ApplicationConfigurer) { | ||
c.Identity("<app>", "7468a57f-20f0-4d11-9aad-48fcd553a908") | ||
a.C.ApplyConfiguration(c) | ||
} | ||
|
||
``` | ||
|
||
```au:output | ||
application <app> (7468a57f-20f0-4d11-9aad-48fcd553a908) App | ||
``` |