Skip to content

Commit

Permalink
Implement detailed rendering of Projection.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalloc committed Oct 17, 2024
1 parent 1259d9e commit 680bee2
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 4 deletions.
37 changes: 33 additions & 4 deletions config/projection.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"github.com/dogmatiq/dogma"
"github.com/dogmatiq/enginekit/config/internal/renderer"
"github.com/dogmatiq/enginekit/internal/typename"
"github.com/dogmatiq/enginekit/optional"
"github.com/dogmatiq/enginekit/protobuf/identitypb"
)
Expand Down Expand Up @@ -86,8 +87,31 @@ func (h *Projection) renderDescriptor(ren *renderer.Renderer) {
renderEntityDescriptor(ren, "projection", h.AsConfigured.Source)
}

func (h *Projection) renderDetails(*renderer.Renderer) {
panic("not implemented")
func (h *Projection) renderDetails(ren *renderer.Renderer) {
renderHandlerDetails(ren, h, h.AsConfigured.Source, h.AsConfigured.IsDisabled)

if p, ok := h.AsConfigured.DeliveryPolicy.TryGet(); ok {
// TODO: https://github.com/dogmatiq/enginekit/issues/55
if typeName, ok := p.TypeName.TryGet(); ok {
ren.IndentBullet()

switch typeName {
case typename.For[dogma.UnicastProjectionDeliveryPolicy]():
ren.Printf("unicast delivery policy")
case typename.For[dogma.BroadcastProjectionDeliveryPolicy]():
ren.Printf("broadcast delivery policy")
default:
ren.Printf("unrecognized delivery policy")

Check warning on line 104 in config/projection.go

View check run for this annotation

Codecov / codecov/patch

config/projection.go#L103-L104

Added lines #L103 - L104 were not covered by tests
}

if !p.Value.IsPresent() {
ren.Print(" (runtime type unavailable)")
}

ren.Indent()
ren.Print("\n")
}
}
}

func (h *Projection) identities() []*Identity {
Expand All @@ -107,12 +131,17 @@ func (h *Projection) clone() Component {

func (h *Projection) normalize(ctx *normalizationContext) {
normalizeValue(ctx, &h.AsConfigured.Source, &h.AsConfigured.Fidelity)
normalizeIdentities(ctx, h.AsConfigured.Identities)
normalize(ctx, h.AsConfigured.Routes...)

if !ctx.Options.Shallow {
normalizeIdentities(ctx, h.AsConfigured.Identities)
normalize(ctx, h.AsConfigured.Routes...)
}

if p, ok := h.AsConfigured.DeliveryPolicy.TryGet(); ok {
normalizeValue(ctx, &p, &h.AsConfigured.Fidelity)
h.AsConfigured.DeliveryPolicy = optional.Some(p)
} else {
h.AsConfigured.Fidelity |= Incomplete
}

reportRouteErrors(ctx, h, h.AsConfigured.Routes)
Expand Down
108 changes: 108 additions & 0 deletions config/projection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/dogmatiq/dogma"
. "github.com/dogmatiq/enginekit/config"
"github.com/dogmatiq/enginekit/config/internal/configbuilder"
"github.com/dogmatiq/enginekit/config/runtimeconfig"
. "github.com/dogmatiq/enginekit/enginetest/stubs"
. "github.com/dogmatiq/enginekit/internal/test"
Expand Down Expand Up @@ -243,6 +244,113 @@ func TestProjection_Interface(t *testing.T) {
)
}

func TestProjection_render(t *testing.T) {
cases := []renderTestCase{
{
Name: "complete",
ExpectDescriptor: `projection:ProjectionMessageHandlerStub`,
ExpectDetails: multiline(
`valid projection *github.com/dogmatiq/enginekit/enginetest/stubs.ProjectionMessageHandlerStub`,
` - valid identity name/19cb98d5-dd17-4daf-ae00-1b413b7b899a`,
` - valid handles-event route for github.com/dogmatiq/enginekit/enginetest/stubs.EventStub[github.com/dogmatiq/enginekit/enginetest/stubs.TypeA]`,
` - broadcast delivery policy`,
),
Component: runtimeconfig.FromProjection(&ProjectionMessageHandlerStub{
ConfigureFunc: func(c dogma.ProjectionConfigurer) {
c.Identity("name", "19cb98d5-dd17-4daf-ae00-1b413b7b899a")
c.Routes(
dogma.HandlesEvent[EventStub[TypeA]](),
)
c.DeliveryPolicy(
dogma.BroadcastProjectionDeliveryPolicy{
PrimaryFirst: true,
},
)
},
}),
},
{
Name: "disabled",
ExpectDescriptor: `projection:ProjectionMessageHandlerStub`,
ExpectDetails: multiline(
`disabled valid projection *github.com/dogmatiq/enginekit/enginetest/stubs.ProjectionMessageHandlerStub`,
` - valid identity name/19cb98d5-dd17-4daf-ae00-1b413b7b899a`,
` - valid handles-event route for github.com/dogmatiq/enginekit/enginetest/stubs.EventStub[github.com/dogmatiq/enginekit/enginetest/stubs.TypeA]`,
` - unicast delivery policy`,
),
Component: runtimeconfig.FromProjection(&ProjectionMessageHandlerStub{
ConfigureFunc: func(c dogma.ProjectionConfigurer) {
c.Identity("name", "19cb98d5-dd17-4daf-ae00-1b413b7b899a")
c.Routes(
dogma.HandlesEvent[EventStub[TypeA]](),
)
c.Disable()
},
}),
},
{
Name: "no runtime type information",
ExpectDescriptor: `projection:SomeProjection`,
ExpectDetails: multiline(
`valid projection pkg.SomeProjection (runtime type unavailable)`,
` - valid identity name/19cb98d5-dd17-4daf-ae00-1b413b7b899a`,
` - valid handles-event route for pkg.SomeEvent (runtime type unavailable)`,
` - broadcast delivery policy (runtime type unavailable)`,
),
Component: configbuilder.
Projection().
SetSourceTypeName("pkg.SomeProjection").
SetDisabled(false).
SetDeliveryPolicyTypeName("github.com/dogmatiq/dogma.BroadcastProjectionDeliveryPolicy").
BuildIdentity(func(b *configbuilder.IdentityBuilder) {
b.SetName("name")
b.SetKey("19cb98d5-dd17-4daf-ae00-1b413b7b899a")
}).
BuildRoute(func(b *configbuilder.RouteBuilder) {
b.SetRouteType(HandlesEventRouteType)
b.SetMessageTypeName("pkg.SomeEvent")
}).
Done(),
},
{
Name: "invalid",
ExpectDescriptor: `projection:ProjectionMessageHandlerStub`,
ExpectDetails: multiline(
`invalid projection *github.com/dogmatiq/enginekit/enginetest/stubs.ProjectionMessageHandlerStub`,
` - expected at least one "handles-event" route`,
` - valid identity name/19cb98d5-dd17-4daf-ae00-1b413b7b899a`,
` - unicast delivery policy`,
),
Component: runtimeconfig.FromProjection(&ProjectionMessageHandlerStub{
ConfigureFunc: func(c dogma.ProjectionConfigurer) {
c.Identity("name", "19cb98d5-dd17-4daf-ae00-1b413b7b899a")
},
}),
},
{
Name: "invalid sub-component",
ExpectDescriptor: `projection:ProjectionMessageHandlerStub`,
ExpectDetails: multiline(
`valid projection *github.com/dogmatiq/enginekit/enginetest/stubs.ProjectionMessageHandlerStub`,
` - invalid identity name/non-uuid`,
` - invalid key ("non-uuid"), expected an RFC 4122/9562 UUID`,
` - valid handles-event route for github.com/dogmatiq/enginekit/enginetest/stubs.EventStub[github.com/dogmatiq/enginekit/enginetest/stubs.TypeA]`,
` - unicast delivery policy`,
),
Component: runtimeconfig.FromProjection(&ProjectionMessageHandlerStub{
ConfigureFunc: func(c dogma.ProjectionConfigurer) {
c.Identity("name", "non-uuid")
c.Routes(
dogma.HandlesEvent[EventStub[TypeA]](),
)
},
}),
},
}

runRenderTests(t, cases)
}

func TestProjection_validation(t *testing.T) {
cases := []validationTestCase{
{
Expand Down

0 comments on commit 680bee2

Please sign in to comment.