Skip to content

Commit

Permalink
use 'strippedCamel' str conversion for topic var names
Browse files Browse the repository at this point in the history
  • Loading branch information
safeer committed Jul 12, 2024
1 parent 1c58c8e commit ae7f5c5
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 6 deletions.
6 changes: 3 additions & 3 deletions backend/controller/pubsub/testdata/go/publisher/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

//ftl:export
var Test_topic = ftl.Topic[PubSubEvent]("test_topic")
var TestTopic = ftl.Topic[PubSubEvent]("test_topic")

type PubSubEvent struct {
Time time.Time
Expand All @@ -21,7 +21,7 @@ func PublishTen(ctx context.Context) error {
for i := 0; i < 10; i++ {
t := time.Now()
logger.Infof("Publishing %v", t)
err := Test_topic.Publish(ctx, PubSubEvent{Time: t})
err := TestTopic.Publish(ctx, PubSubEvent{Time: t})
if err != nil {
return err
}
Expand All @@ -34,7 +34,7 @@ func PublishOne(ctx context.Context) error {
logger := ftl.LoggerFromContext(ctx)
t := time.Now()
logger.Infof("Publishing %v", t)
return Test_topic.Publish(ctx, PubSubEvent{Time: t})
return TestTopic.Publish(ctx, PubSubEvent{Time: t})
}

//ftl:export
Expand Down
12 changes: 12 additions & 0 deletions backend/schema/strcase/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ func ToUpperCamel(s string) string {
return strings.Join(parts, "")
}

func ToUpperStrippedCamel(s string) string {
parts := split(s)
out := make([]string, 0, len(parts)*2)
for i := range parts {
if parts[i] == "-" || parts[i] == "_" {
continue
}
out = append(out, title(parts[i]))
}
return strings.Join(out, "")
}

func ToLowerSnake(s string) string {
parts := split(s)
out := make([]string, 0, len(parts)*2)
Expand Down
28 changes: 28 additions & 0 deletions backend/schema/strcase/case_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,34 @@ func TestUpperCamelCase(t *testing.T) {
}
}

func TestUpperStrippedCamel(t *testing.T) {
for _, tt := range []struct {
input string
expected string
}{
{"lowercase", "Lowercase"},
{"Class", "Class"},
{"MyClass", "MyClass"},
{"MyC", "MyC"},
{"HTML", "Html"},
{"PDFLoader", "PdfLoader"},
{"AString", "AString"},
{"SimpleXMLParser", "SimpleXmlParser"},
{"vimRPCPlugin", "VimRpcPlugin"},
{"GL11Version", "Gl11Version"},
{"99Bottles", "99Bottles"},
{"May5", "May5"},
{"BFG9000", "Bfg9000"},
{"BöseÜberraschung", "BöseÜberraschung"},
{"snake_case", "SnakeCase"},
{"snake_Case_Caps", "SnakeCaseCaps"},
{"kebab-numbers99", "KebabNumbers99"},
} {
actual := ToUpperStrippedCamel(tt.input)
assert.Equal(t, tt.expected, actual, "UpperStrippedCamel(%q) = %v; want %v", tt.input, actual, tt.expected)
}
}

func TestLowerSnake(t *testing.T) {
for _, tt := range []struct {
input string
Expand Down
1 change: 1 addition & 0 deletions cmd/ftl/cmd_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ var scaffoldFuncs = template.FuncMap{
"screamingSnake": strcase.ToUpperSnake,
"camel": strcase.ToUpperCamel,
"lowerCamel": strcase.ToLowerCamel,
"strippedCamel": strcase.ToUpperStrippedCamel,
"kebab": strcase.ToLowerKebab,
"screamingKebab": strcase.ToUpperKebab,
"upper": strings.ToUpper,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var _ = context.Background
//
{{- end}}
{{- if is "Topic" .}}
var {{.Name|title}} = ftl.Topic[{{type $.Module .Event}}]("{{.Name}}")
var {{.Name|strippedCamel}} = ftl.Topic[{{type $.Module .Event}}]("{{.Name}}")
{{- else if and (is "Enum" .) .IsValueEnum}}
{{- $enumName := .Name}}
//ftl:enum
Expand Down
18 changes: 16 additions & 2 deletions go-runtime/schema/topic/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"go/types"

"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 @@ -47,7 +48,7 @@ func Extract(pass *analysis.Pass) (interface{}, error) {
return common.NewExtractorResult(pass), nil
}

// expects: _ = ftl.Topic[EventType]("name_literal")
// expects: var NameLiteral = ftl.Topic[EventType]("name_literal")
func extractTopic(pass *analysis.Pass, node *ast.GenDecl, callExpr *ast.CallExpr, obj types.Object) optional.Option[*schema.Topic] {
indexExpr, ok := callExpr.Fun.(*ast.IndexExpr)
if !ok {
Expand All @@ -60,9 +61,22 @@ func extractTopic(pass *analysis.Pass, node *ast.GenDecl, callExpr *ast.CallExpr
return optional.None[*schema.Topic]()
}

topicName := common.ExtractStringLiteralArg(pass, callExpr, 0)

if len(node.Specs) > 0 {
if t, ok := node.Specs[0].(*ast.ValueSpec); ok {
varName := t.Names[0].Name
expVarName := strcase.ToUpperStrippedCamel(topicName)
if varName != expVarName {
common.Errorf(pass, node, "unexpected topic variable name %q, did you mean %q?", varName, expVarName)
return optional.None[*schema.Topic]()
}
}
}

topic := &schema.Topic{
Pos: common.GoPosToSchemaPos(pass.Fset, node.Pos()),
Name: common.ExtractStringLiteralArg(pass, callExpr, 0),
Name: topicName,
Event: typeParamType,
}
common.ApplyMetadata[*schema.Subscription](pass, obj, func(md *common.ExtractedMetadata) {
Expand Down
1 change: 1 addition & 0 deletions internal/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var scaffoldFuncs = scaffolder.FuncMap{
"screamingSnake": strcase.ToUpperSnake,
"camel": strcase.ToUpperCamel,
"lowerCamel": strcase.ToLowerCamel,
"strippedCamel": strcase.ToUpperStrippedCamel,
"kebab": strcase.ToLowerKebab,
"screamingKebab": strcase.ToUpperKebab,
"upper": strings.ToUpper,
Expand Down

0 comments on commit ae7f5c5

Please sign in to comment.