Skip to content

Commit

Permalink
Improve "speculative" detection of dynamically constructed variadics.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalloc committed Nov 5, 2024
1 parent 13f27ea commit 04e17c4
Show file tree
Hide file tree
Showing 6 changed files with 320 additions and 123 deletions.
5 changes: 5 additions & 0 deletions config/staticconfig/entity.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package staticconfig

import (
"fmt"
"go/types"
"os"
"runtime"

"github.com/dogmatiq/enginekit/config"
Expand Down Expand Up @@ -79,6 +81,9 @@ func analyzeEntity[
builder.TypeName(typename.OfStatic(t))
configure := ctx.LookupMethod(t, "Configure")

fmt.Println("===========================================")
configure.WriteTo(os.Stderr)

ectx := &entityContext[T, E, B]{
context: ctx,
EntityType: t,
Expand Down
44 changes: 0 additions & 44 deletions config/staticconfig/testdata/handler-with-appended-routes.md

This file was deleted.

83 changes: 83 additions & 0 deletions config/staticconfig/testdata/handler-with-speculative-routes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Handler with speculative routes

This test verifies that the static analyzer correctly identifies routes as
"speculative" under various complex conditions.

Some of these scenarios could be improved, potentially avoiding false positives
for the "speculative" flag. In general, however, it is preferred that the
analyzer errs on the side of caution and marks routes as "speculative" when it
is unsure.

```au:output au:group=matrix
valid application github.com/dogmatiq/enginekit/config/staticconfig.App (value unavailable)
- valid identity app/e4183527-c234-42d5-8709-3dc8b9d5caa4
- valid integration github.com/dogmatiq/enginekit/config/staticconfig.Integration (value unavailable)
- valid identity handler/5752bb84-0b65-4a7f-b2fa-bfb77a53a97f
- valid speculative handles-command route for github.com/dogmatiq/enginekit/enginetest/stubs.CommandStub[github.com/dogmatiq/enginekit/enginetest/stubs.TypeA] (type unavailable)
- valid speculative records-event route for github.com/dogmatiq/enginekit/enginetest/stubs.EventStub[github.com/dogmatiq/enginekit/enginetest/stubs.TypeA] (type unavailable)
```

## Random index

```go au:input au:group=matrix
package app

import "context"
import "math/rand"
import "github.com/dogmatiq/dogma"
import "github.com/dogmatiq/enginekit/enginetest/stubs"

type Integration struct{}

func (Integration) Configure(c dogma.IntegrationConfigurer) {
c.Identity("handler", "5752bb84-0b65-4a7f-b2fa-bfb77a53a97f")

routes := make([]dogma.IntegrationRoute, 0)

routes[0] = dogma.HandlesCommand[stubs.CommandStub[stubs.TypeA]]()
routes[rand.Int()] = dogma.RecordsEvent[stubs.EventStub[stubs.TypeA]]()

c.Routes(routes...)
}

func (Integration) HandleCommand(context.Context, dogma.IntegrationCommandScope, dogma.Command) error { return nil }

type App struct{}

func (App) Configure(c dogma.ApplicationConfigurer) {
c.Identity("app", "e4183527-c234-42d5-8709-3dc8b9d5caa4")
c.RegisterIntegration(Integration{})
}
```

## Colliding indices

```go au:input au:group=matrix
package app

import "context"
import "github.com/dogmatiq/dogma"
import "github.com/dogmatiq/enginekit/enginetest/stubs"

type Integration struct{}

func (Integration) Configure(c dogma.IntegrationConfigurer) {
c.Identity("handler", "5752bb84-0b65-4a7f-b2fa-bfb77a53a97f")

routes := make([]dogma.IntegrationRoute, 0)

routes[0] = dogma.HandlesCommand[stubs.CommandStub[stubs.TypeA]]()
routes[0] = dogma.RecordsEvent[stubs.EventStub[stubs.TypeA]]()

c.Routes(routes...)
}

func (Integration) HandleCommand(context.Context, dogma.IntegrationCommandScope, dogma.Command) error { return nil }

type App struct{}

func (App) Configure(c dogma.ApplicationConfigurer) {
c.Identity("app", "e4183527-c234-42d5-8709-3dc8b9d5caa4")
c.RegisterIntegration(Integration{})
}
```
111 changes: 111 additions & 0 deletions config/staticconfig/testdata/handler-with-variadic-routes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Handler with variadic routes

This test verifies that the static analyzer correctly identifies routes that are
configured via a slice which is used as the variadic parameter to the `Routes()`
method.

```au:output au:group=matrix
valid application github.com/dogmatiq/enginekit/config/staticconfig.App (value unavailable)
- valid identity app/e4183527-c234-42d5-8709-3dc8b9d5caa4
- valid integration github.com/dogmatiq/enginekit/config/staticconfig.Integration (value unavailable)
- valid identity handler/5752bb84-0b65-4a7f-b2fa-bfb77a53a97f
- valid handles-command route for github.com/dogmatiq/enginekit/enginetest/stubs.CommandStub[github.com/dogmatiq/enginekit/enginetest/stubs.TypeA] (type unavailable)
- valid records-event route for github.com/dogmatiq/enginekit/enginetest/stubs.EventStub[github.com/dogmatiq/enginekit/enginetest/stubs.TypeA] (type unavailable)
```

## Appended

```go au:input au:group=matrix
package app

import "context"
import "github.com/dogmatiq/dogma"
import "github.com/dogmatiq/enginekit/enginetest/stubs"

type Integration struct{}

func (Integration) Configure(c dogma.IntegrationConfigurer) {
c.Identity("handler", "5752bb84-0b65-4a7f-b2fa-bfb77a53a97f")

var routes []dogma.IntegrationRoute

routes = append(
routes,
dogma.HandlesCommand[stubs.CommandStub[stubs.TypeA]](),
dogma.RecordsEvent[stubs.EventStub[stubs.TypeA]](),
)

c.Routes(routes...)
}

func (Integration) HandleCommand(context.Context, dogma.IntegrationCommandScope, dogma.Command) error { return nil }

type App struct{}

func (App) Configure(c dogma.ApplicationConfigurer) {
c.Identity("app", "e4183527-c234-42d5-8709-3dc8b9d5caa4")
c.RegisterIntegration(Integration{})
}
```

## Assigned to index

```go au:input au:group=matrix
package app

import "context"
import "github.com/dogmatiq/dogma"
import "github.com/dogmatiq/enginekit/enginetest/stubs"

type Integration struct{}

func (Integration) Configure(c dogma.IntegrationConfigurer) {
c.Identity("handler", "5752bb84-0b65-4a7f-b2fa-bfb77a53a97f")

routes := make([]dogma.IntegrationRoute, 1)
routes[0] = dogma.HandlesCommand[stubs.CommandStub[stubs.TypeA]]()
routes[1] = dogma.RecordsEvent[stubs.EventStub[stubs.TypeA]]()

c.Routes(routes...)
}

func (Integration) HandleCommand(context.Context, dogma.IntegrationCommandScope, dogma.Command) error { return nil }

type App struct{}

func (App) Configure(c dogma.ApplicationConfigurer) {
c.Identity("app", "e4183527-c234-42d5-8709-3dc8b9d5caa4")
c.RegisterIntegration(Integration{})
}
```

## Assigned to index of sub-slice

```go au:input au:group=matrix
package app

import "context"
import "github.com/dogmatiq/dogma"
import "github.com/dogmatiq/enginekit/enginetest/stubs"

type Integration struct{}

func (Integration) Configure(c dogma.IntegrationConfigurer) {
c.Identity("handler", "5752bb84-0b65-4a7f-b2fa-bfb77a53a97f")

routes := make([]dogma.IntegrationRoute, 1)
routes[:1][0] = dogma.HandlesCommand[stubs.CommandStub[stubs.TypeA]]()
routes[1:][0] = dogma.RecordsEvent[stubs.EventStub[stubs.TypeA]]()

c.Routes(routes...)
}

func (Integration) HandleCommand(context.Context, dogma.IntegrationCommandScope, dogma.Command) error { return nil }

type App struct{}

func (App) Configure(c dogma.ApplicationConfigurer) {
c.Identity("app", "e4183527-c234-42d5-8709-3dc8b9d5caa4")
c.RegisterIntegration(Integration{})
}
```
Loading

0 comments on commit 04e17c4

Please sign in to comment.