Skip to content

Commit

Permalink
fix: also require camel case subscription names (#2342)
Browse files Browse the repository at this point in the history
This makes naming more consistent.
  • Loading branch information
stuartwdouglas authored Aug 14, 2024
1 parent d5666b9 commit da69372
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
8 changes: 4 additions & 4 deletions backend/controller/pubsub/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestPubSub(t *testing.T) {
WHERE
state = 'success'
AND origin = '%s'
`, dal.AsyncOriginPubSub{Subscription: schema.RefKey{Module: "subscriber", Name: "test_subscription"}}.String()),
`, dal.AsyncOriginPubSub{Subscription: schema.RefKey{Module: "subscriber", Name: "testSubscription"}}.String()),
events),
)
}
Expand Down Expand Up @@ -103,7 +103,7 @@ func TestRetry(t *testing.T) {
state = 'error'
AND catching = false
AND origin = '%s'
`, dal.AsyncOriginPubSub{Subscription: schema.RefKey{Module: "subscriber", Name: "doomed_subscription"}}.String()),
`, dal.AsyncOriginPubSub{Subscription: schema.RefKey{Module: "subscriber", Name: "doomedSubscription"}}.String()),
1+retriesPerCall),

// check that there is one failed attempt to catch (we purposely fail the first one)
Expand All @@ -116,7 +116,7 @@ func TestRetry(t *testing.T) {
AND error = 'call to verb subscriber.catch failed: catching error'
AND catching = true
AND origin = '%s'
`, dal.AsyncOriginPubSub{Subscription: schema.RefKey{Module: "subscriber", Name: "doomed_subscription"}}.String()),
`, dal.AsyncOriginPubSub{Subscription: schema.RefKey{Module: "subscriber", Name: "doomedSubscription"}}.String()),
1),

// check that there is one successful attempt to catch (we succeed the second one as long as we receive the correct error in the request)
Expand All @@ -129,7 +129,7 @@ func TestRetry(t *testing.T) {
AND error IS NULL
AND catching = true
AND origin = '%s'
`, dal.AsyncOriginPubSub{Subscription: schema.RefKey{Module: "subscriber", Name: "doomed_subscription"}}.String()),
`, dal.AsyncOriginPubSub{Subscription: schema.RefKey{Module: "subscriber", Name: "doomedSubscription"}}.String()),
1),
)
}
Expand Down
16 changes: 9 additions & 7 deletions backend/controller/pubsub/testdata/go/subscriber/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,32 @@ package subscriber
import (
"context"
"fmt"
"ftl/builtin"
"ftl/publisher"
"strings"
"time"

"github.com/TBD54566975/ftl/go-runtime/ftl" // Import the FTL SDK.
"ftl/builtin"
"ftl/publisher"

"github.com/alecthomas/atomic"

"github.com/TBD54566975/ftl/go-runtime/ftl" // Import the FTL SDK.
)

var _ = ftl.Subscription(publisher.TestTopic, "test_subscription")
var _ = ftl.Subscription(publisher.TestTopic, "testSubscription")

var catchCount atomic.Value[int]

//ftl:verb
//ftl:subscribe test_subscription
//ftl:subscribe testSubscription
func Consume(ctx context.Context, req publisher.PubSubEvent) error {
ftl.LoggerFromContext(ctx).Infof("Subscriber is consuming %v", req.Time)
return nil
}

var _ = ftl.Subscription(publisher.Topic2, "doomed_subscription")
var _ = ftl.Subscription(publisher.Topic2, "doomedSubscription")

//ftl:verb
//ftl:subscribe doomed_subscription
//ftl:subscribe doomedSubscription
//ftl:retry 2 1s 1s catch catch
func ConsumeButFailAndRetry(ctx context.Context, req publisher.PubSubEvent) error {
return fmt.Errorf("always error: event %v", req.Time)
Expand Down
8 changes: 7 additions & 1 deletion go-runtime/schema/subscription/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,15 @@ func Extract(pass *analysis.Pass, obj types.Object, node *ast.GenDecl, callExpr
return optional.None[*schema.Subscription]()
}

subName := common.ExtractStringLiteralArg(pass, callExpr, 1)
expSubName := strcase.ToLowerCamel(subName)
if subName != expSubName {
common.Errorf(pass, node, "unsupported subscription name %q, did you mean to use %q?", subName, expSubName)
return optional.None[*schema.Subscription]()
}
subscription := &schema.Subscription{
Pos: common.GoPosToSchemaPos(pass.Fset, callExpr.Pos()),
Name: common.ExtractStringLiteralArg(pass, callExpr, 1),
Name: subName,
Topic: topicRef,
}
common.ApplyMetadata[*schema.Subscription](pass, obj, func(md *common.ExtractedMetadata) {
Expand Down

0 comments on commit da69372

Please sign in to comment.