Skip to content

Commit

Permalink
feat: support enums in go runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
worstell committed Mar 2, 2024
1 parent 9a8df99 commit 8dd6e0f
Show file tree
Hide file tree
Showing 8 changed files with 351 additions and 33 deletions.
15 changes: 15 additions & 0 deletions buildengine/build_go_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package buildengine

import (
"testing"
)

func TestGenerateGoModule(t *testing.T) {
// sch := &schema.Schema{
// Modules: []*schema.Module{{Name: "test"}},
// }
// expected := `// Code generated by FTL. DO NOT EDIT.
//package ftl.test
//
//`
}
70 changes: 40 additions & 30 deletions buildengine/build_kotlin_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package buildengine

import (
"context"
"os"
"path/filepath"
"testing"

"github.com/TBD54566975/ftl/backend/schema"
"github.com/TBD54566975/ftl/internal/log"
"github.com/alecthomas/assert/v2"
)

func TestGenerateBasicModule(t *testing.T) {
Expand All @@ -19,7 +14,14 @@ func TestGenerateBasicModule(t *testing.T) {
package ftl.test
`
assertExpectedSchema(t, sch, "test/Test.kt", expected)
bctx := buildContext{
moduleDir: "testdata/modules/echokotlin",
buildDir: "target",
sch: sch,
}
testBuild(t, bctx, []assertion{
assertGeneratedModule("generated-sources/ftl/test/Test.kt", expected),
})
}

func TestGenerateAllTypes(t *testing.T) {
Expand Down Expand Up @@ -122,7 +124,14 @@ data class TestResponse(
)
`
assertExpectedSchema(t, sch, "test/Test.kt", expected)
bctx := buildContext{
moduleDir: "testdata/modules/echokotlin",
buildDir: "target",
sch: sch,
}
testBuild(t, bctx, []assertion{
assertGeneratedModule("generated-sources/ftl/test/Test.kt", expected),
})
}

func TestGenerateAllVerbs(t *testing.T) {
Expand Down Expand Up @@ -172,7 +181,14 @@ data class Request(
fun testVerb(context: Context, req: Request): Empty = throw
NotImplementedError("Verb stubs should not be called directly, instead use context.call(::testVerb, ...)")
`
assertExpectedSchema(t, sch, "test/Test.kt", expected)
bctx := buildContext{
moduleDir: "testdata/modules/echokotlin",
buildDir: "target",
sch: sch,
}
testBuild(t, bctx, []assertion{
assertGeneratedModule("generated-sources/ftl/test/Test.kt", expected),
})
}

func TestGenerateBuiltins(t *testing.T) {
Expand Down Expand Up @@ -211,7 +227,14 @@ data class HttpResponse<Body, Error>(
class Empty
`
assertExpectedSchema(t, sch, "builtin/Builtin.kt", expected)
bctx := buildContext{
moduleDir: "testdata/modules/echokotlin",
buildDir: "target",
sch: sch,
}
testBuild(t, bctx, []assertion{
assertGeneratedModule("generated-sources/ftl/builtin/Builtin.kt", expected),
})
}

func TestGenerateEmptyDataRefs(t *testing.T) {
Expand Down Expand Up @@ -245,25 +268,12 @@ import xyz.block.ftl.Verb
fun emptyVerb(context: Context, req: Empty): Empty = throw
NotImplementedError("Verb stubs should not be called directly, instead use context.call(::emptyVerb, ...)")
`
assertExpectedSchema(t, sch, "test/Test.kt", expected)
}

func assertExpectedSchema(t *testing.T, sch *schema.Schema, outputPath string, expectedContent string) {
t.Helper()
ctx := log.ContextWithLogger(context.Background(), log.Configure(os.Stderr, log.Config{}))
module, err := LoadModule(ctx, "testdata/modules/echokotlin")
assert.NoError(t, err)

err = generateExternalModules(ctx, module, sch)
assert.NoError(t, err)

target := filepath.Join("testdata/modules/echokotlin", "target")
output := filepath.Join(target, "generated-sources", "ftl", outputPath)

fileContent, err := os.ReadFile(output)
assert.NoError(t, err)
assert.Equal(t, expectedContent, string(fileContent))

err = os.RemoveAll(target)
assert.NoError(t, err, "Error removing target directory")
bctx := buildContext{
moduleDir: "testdata/modules/echokotlin",
buildDir: "target",
sch: sch,
}
testBuild(t, bctx, []assertion{
assertGeneratedModule("generated-sources/ftl/test/Test.kt", expected),
})
}
54 changes: 54 additions & 0 deletions buildengine/build_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package buildengine

import (
"context"
"github.com/alecthomas/assert/v2"
"os"
"path/filepath"
"testing"

"github.com/TBD54566975/ftl/backend/schema"
"github.com/TBD54566975/ftl/internal/log"
)

type buildContext struct {
moduleDir string
buildDir string
sch *schema.Schema
}

type assertion func(t testing.TB, bctx buildContext) error

func testBuild(
t *testing.T,
bctx buildContext,
assertions []assertion,
) {
t.Helper()
ctx := log.ContextWithLogger(context.Background(), log.Configure(os.Stderr, log.Config{}))
module, err := LoadModule(ctx, bctx.moduleDir)
assert.NoError(t, err)

err = Build(ctx, bctx.sch, module)
assert.NoError(t, err)

for _, a := range assertions {
err = a(t, bctx)
assert.NoError(t, err)
}

err = os.RemoveAll(bctx.buildDir)
assert.NoError(t, err, "Error removing build directory")
}

func assertGeneratedModule(generatedModulePath string, expectedContent string) assertion {
return func(t testing.TB, bctx buildContext) error {
target := filepath.Join(bctx.moduleDir, bctx.buildDir)
output := filepath.Join(target, generatedModulePath)

fileContent, err := os.ReadFile(output)
assert.NoError(t, err)
assert.Equal(t, expectedContent, string(fileContent))
return nil
}
}

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

11 changes: 10 additions & 1 deletion go-runtime/compile/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,21 @@ type directiveModule struct {
func (*directiveModule) directive() {}
func (d *directiveModule) String() string { return "ftl:module" }

type directiveEnum struct {
Pos lexer.Position

Enum bool `parser:"@'enum'"`
}

func (*directiveEnum) directive() {}
func (d *directiveEnum) String() string { return "ftl:enum" }

var directiveParser = participle.MustBuild[directiveWrapper](
participle.Lexer(schema.Lexer),
participle.Elide("Whitespace"),
participle.Unquote(),
participle.UseLookahead(2),
participle.Union[directive](&directiveVerb{}, &directiveIngress{}, &directiveModule{}),
participle.Union[directive](&directiveVerb{}, &directiveIngress{}, &directiveModule{}, &directiveEnum{}),
participle.Union[schema.IngressPathComponent](&schema.IngressPathLiteral{}, &schema.IngressPathParameter{}),
)

Expand Down
Loading

0 comments on commit 8dd6e0f

Please sign in to comment.