Skip to content

Commit

Permalink
be prescriptive about topic names; require lower_snake
Browse files Browse the repository at this point in the history
  • Loading branch information
safeer committed Jul 17, 2024
1 parent 7dff76d commit c965c39
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 11 deletions.
4 changes: 2 additions & 2 deletions backend/controller/pubsub/testdata/go/publisher/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ func PublishOne(ctx context.Context) error {
}

//ftl:export
var Topic2 = ftl.Topic[PubSubEvent]("topic2")
var Topic2 = ftl.Topic[PubSubEvent]("topic_2")

//ftl:verb
func PublishOneToTopic2(ctx context.Context) error {
logger := ftl.LoggerFromContext(ctx)
t := time.Now()
logger.Infof("Publishing to topic2 %v", t)
logger.Infof("Publishing to topic_2 %v", t)
return Topic2.Publish(ctx, PubSubEvent{Time: t})
}
4 changes: 2 additions & 2 deletions go-runtime/compile/testdata/pubsub/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ var Payins = ftl.Topic[PayinEvent]("payins")

var _ = ftl.Subscription(PublicBroadcast, "broadcastSubscription")

// publicBroadcast is a topic that broadcasts payin events to the public.
// public_broadcast is a topic that broadcasts payin events to the public.
// out of order with subscription registration to test ordering doesn't matter.
//
//ftl:export
var PublicBroadcast = ftl.Topic[PayinEvent]("publicBroadcast")
var PublicBroadcast = ftl.Topic[PayinEvent]("public_broadcast")

//ftl:verb export
func Broadcast(ctx context.Context) error {
Expand Down
7 changes: 2 additions & 5 deletions go-runtime/schema/subscription/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package subscription
import (
"go/ast"
"go/types"
"strings"

"github.com/TBD54566975/ftl/backend/schema"
"github.com/TBD54566975/ftl/backend/schema/strcase"
"github.com/TBD54566975/ftl/go-runtime/schema/common"
"github.com/TBD54566975/golang-tools/go/analysis"
"github.com/TBD54566975/golang-tools/go/analysis/passes/inspect"
Expand Down Expand Up @@ -91,10 +91,7 @@ func extractSubscription(pass *analysis.Pass, obj types.Object, node *ast.CallEx
common.Errorf(pass, node, "subscription registration must have a topic")
return optional.None[*schema.Subscription]()
}
name := strings.ToLower(string(varName[0]))
if len(varName) > 1 {
name += varName[1:]
}
name := strcase.ToLowerSnake(varName)
topicRef = &schema.Ref{
Module: moduleIdent.Name,
Name: name,
Expand Down
5 changes: 5 additions & 0 deletions go-runtime/schema/topic/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ func extractTopic(pass *analysis.Pass, node *ast.GenDecl, callExpr *ast.CallExpr
}

topicName := common.ExtractStringLiteralArg(pass, callExpr, 0)
expTopicName := strcase.ToLowerSnake(topicName)
if topicName != expTopicName {
common.Errorf(pass, node, "unsupported topic name %q, did you mean to use %q?", topicName, expTopicName)
return optional.None[*schema.Topic]()
}

if len(node.Specs) > 0 {
if t, ok := node.Specs[0].(*ast.ValueSpec); ok {
Expand Down
2 changes: 1 addition & 1 deletion lsp/hoveritems.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lsp/markdown/completion/pubSubTopic.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Declare a PubSub topic.

```go
var invoicesTopic = ftl.Topic[Invoice]("invoices")
var Invoices = ftl.Topic[Invoice]("invoices")
```

See https://tbd54566975.github.io/ftl/docs/reference/pubsub/
Expand Down

0 comments on commit c965c39

Please sign in to comment.