Skip to content

Commit

Permalink
feat: use new verb, data, and enum annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
wesbillman committed Apr 29, 2024
1 parent ff1e609 commit 69a3f42
Show file tree
Hide file tree
Showing 39 changed files with 202 additions and 198 deletions.
14 changes: 7 additions & 7 deletions buildengine/build_go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ import (
var _ = context.Background
//ftl:export
//ftl:enum
type Color string
const (
Red Color = "Red"
Blue Color = "Blue"
Green Color = "Green"
)
//ftl:export
//ftl:enum
type ColorInt int
const (
RedInt ColorInt = 0
Expand All @@ -91,28 +91,28 @@ type EchoRequest struct {
type EchoResponse struct {
}
//ftl:export
//ftl:verb
func Echo(context.Context, EchoRequest) (EchoResponse, error) {
panic("Verb stubs should not be called directly, instead use github.com/TBD54566975/ftl/runtime-go/ftl.Call()")
}
type SinkReq struct {
}
//ftl:export
//ftl:verb
func Sink(context.Context, SinkReq) error {
panic("Verb stubs should not be called directly, instead use github.com/TBD54566975/ftl/runtime-go/ftl.CallSink()")
}
type SourceResp struct {
}
//ftl:export
//ftl:verb
func Source(context.Context) (SourceResp, error) {
panic("Verb stubs should not be called directly, instead use github.com/TBD54566975/ftl/runtime-go/ftl.CallSource()")
}
//ftl:export
//ftl:verb
func Nothing(context.Context) error {
panic("Verb stubs should not be called directly, instead use github.com/TBD54566975/ftl/runtime-go/ftl.CallEmpty()")
}
Expand Down Expand Up @@ -161,7 +161,7 @@ type Req struct {
type Resp struct {
}
//ftl:export
//ftl:verb
func Call(context.Context, Req) (Resp, error) {
panic("Verb stubs should not be called directly, instead use github.com/TBD54566975/ftl/runtime-go/ftl.Call()")
}
Expand Down
6 changes: 3 additions & 3 deletions buildengine/build_kotlin.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,13 @@ var scaffoldFuncs = scaffolder.FuncMap{
_ = schema.VisitExcludingMetadataChildren(m, func(n schema.Node, next func() error) error {
switch n.(type) {
case *schema.Data:
imports.Add("xyz.block.ftl.Export")
imports.Add("xyz.block.ftl.Data")

case *schema.Enum:
imports.Add("xyz.block.ftl.Export")
imports.Add("xyz.block.ftl.Enum")

case *schema.Verb:
imports.Append("xyz.block.ftl.Context", "xyz.block.ftl.Ignore", "xyz.block.ftl.Export")
imports.Append("xyz.block.ftl.Context", "xyz.block.ftl.Ignore", "xyz.block.ftl.Verb")

case *schema.Time:
imports.Add("java.time.OffsetDateTime")
Expand Down
41 changes: 22 additions & 19 deletions buildengine/build_kotlin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,22 @@ func TestGenerateAllTypes(t *testing.T) {
package ftl.test
import java.time.OffsetDateTime
import xyz.block.ftl.Export
import xyz.block.ftl.Data
@Export
@Data
data class ParamTestData<T>(
val t: T,
)
@Export
@Data
data class TestRequest(
val field: Long,
)
/**
* Response comments
*/
@Export
@Data
data class TestResponse(
val int: Long,
val float: Float,
Expand Down Expand Up @@ -192,18 +192,19 @@ func TestGenerateAllVerbs(t *testing.T) {
package ftl.test
import xyz.block.ftl.Context
import xyz.block.ftl.Export
import xyz.block.ftl.Data
import xyz.block.ftl.Ignore
import xyz.block.ftl.Verb
@Export
@Data
data class Request(
val data: Long,
)
/**
* TestVerb comments
*/
@Export
@Verb
@Ignore
fun testVerb(context: Context, req: Request): ftl.builtin.Empty = throw
NotImplementedError("Verb stubs should not be called directly, instead use context.call(::testVerb, ...)")
Expand Down Expand Up @@ -233,12 +234,12 @@ func TestGenerateBuiltins(t *testing.T) {
*/
package ftl.builtin
import xyz.block.ftl.Export
import xyz.block.ftl.Data
/**
* HTTP request structure used for HTTP ingress verbs.
*/
@Export
@Data
data class HttpRequest<Body>(
val method: String,
val path: String,
Expand All @@ -251,15 +252,15 @@ data class HttpRequest<Body>(
/**
* HTTP response structure used for HTTP ingress verbs.
*/
@Export
@Data
data class HttpResponse<Body, Error>(
val status: Long,
val headers: Map<String, List<String>>,
val body: Body? = null,
val error: Error? = null,
)
@Export
@Data
class Empty
`
bctx := buildContext{
Expand Down Expand Up @@ -298,10 +299,11 @@ func TestGenerateEmptyRefs(t *testing.T) {
package ftl.test
import xyz.block.ftl.Context
import xyz.block.ftl.Export
import xyz.block.ftl.Data
import xyz.block.ftl.Ignore
import xyz.block.ftl.Verb
@Export
@Verb
@Ignore
fun emptyVerb(context: Context, req: ftl.builtin.Empty): ftl.builtin.Empty = throw
NotImplementedError("Verb stubs should not be called directly, instead use context.call(::emptyVerb, ...)")
Expand Down Expand Up @@ -360,28 +362,29 @@ func TestGenerateSourcesAndSinks(t *testing.T) {
package ftl.test
import xyz.block.ftl.Context
import xyz.block.ftl.Export
import xyz.block.ftl.Data
import xyz.block.ftl.Ignore
import xyz.block.ftl.Verb
@Export
@Data
data class SinkReq(
val data: Long,
)
@Export
@Verb
@Ignore
fun sink(context: Context, req: SinkReq): Unit = throw
NotImplementedError("Verb stubs should not be called directly, instead use context.callSink(::sink, ...)")
@Export
@Data
data class SourceResp(
val data: Long,
)
@Export
@Verb
@Ignore
fun source(context: Context): SourceResp = throw
NotImplementedError("Verb stubs should not be called directly, instead use context.callSource(::source, ...)")
@Export
@Verb
@Ignore
fun nothing(context: Context): Unit = throw
NotImplementedError("Verb stubs should not be called directly, instead use context.callEmpty(::nothing, ...)")
Expand Down
2 changes: 1 addition & 1 deletion buildengine/testdata/projects/alpha/alpha.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type EchoResponse struct {
Message string `json:"message"`
}

//ftl:export
//ftl:verb
func Echo(ctx context.Context, req EchoRequest) (EchoResponse, error) {
ftl.Call(ctx, other.Echo, other.EchoRequest{})
return EchoResponse{Message: fmt.Sprintf("Hello, %s!", req.Name.Default("anonymous"))}, nil
Expand Down
2 changes: 1 addition & 1 deletion buildengine/testdata/projects/another/another.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type EchoResponse struct {
Message string `json:"message"`
}

//ftl:export
//ftl:verb
func Echo(ctx context.Context, req EchoRequest) (EchoResponse, error) {
return EchoResponse{Message: fmt.Sprintf("Hello, %s!", req.Name.Default("anonymous"))}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package ftl.echo

import xyz.block.ftl.Context
import xyz.block.ftl.Method
import xyz.block.ftl.Export
import xyz.block.ftl.Verb

data class EchoRequest(val name: String? = "anonymous")
data class EchoResponse(val message: String)

@Export
@Verb
fun echo(context: Context, req: EchoRequest): EchoResponse {
return EchoResponse(message = "Hello, ${req.name}!")
}
2 changes: 1 addition & 1 deletion buildengine/testdata/projects/external/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type ExternalResponse struct {

// External returns the current month as an external type.
//
//ftl:export
//ftl:verb
func Time(ctx context.Context, req ExternalRequest) (ExternalResponse, error) {
return ExternalResponse{Month: time.Now().Month()}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ftl.externalkotlin

import com.google.type.DayOfWeek
import xyz.block.ftl.Context
import xyz.block.ftl.Export
import xyz.block.ftl.Verb
import xyz.block.ftl.v1.schema.Optional

class InvalidInput(val field: String) : Exception()
Expand All @@ -11,7 +11,7 @@ data class ExternalRequest(val name: String?, val dayOfWeek: DayOfWeek)
data class ExternalResponse(val message: String)

@Throws(InvalidInput::class)
@Export
@Verb
fun external(context: Context, req: ExternalRequest): ExternalResponse {
return ExternalResponse(message = "Hello, ${req.name ?: "anonymous"}!")
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type EchoResponse struct {
Message string `json:"message"`
}

//ftl:export
//ftl:verb
func Echo(ctx context.Context, req EchoRequest) (EchoResponse, error) {
return EchoResponse{Message: fmt.Sprintf("Hello, %s!", req.Name.Default("anonymous"))}, nil
}
2 changes: 1 addition & 1 deletion buildengine/testdata/projects/other/other.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type EchoResponse struct {
Message string `json:"message"`
}

//ftl:export
//ftl:verb
func Echo(ctx context.Context, req EchoRequest) (EchoResponse, error) {
return EchoResponse{Message: fmt.Sprintf("Hello, %s!", req.Name.Default("anonymous"))}, nil
}
2 changes: 1 addition & 1 deletion examples/go/echo/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type EchoResponse struct {

// Echo returns a greeting with the current time.
//
//ftl:export
//ftl:verb
func Echo(ctx context.Context, req EchoRequest) (EchoResponse, error) {
tresp, err := ftl.Call(ctx, time.Time, time.TimeRequest{})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion examples/go/time/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type TimeResponse struct {

// Time returns the current time.
//
//ftl:export
//ftl:verb
func Time(ctx context.Context, req TimeRequest) (TimeResponse, error) {
return TimeResponse{Time: time.Now()}, nil
}

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

48 changes: 42 additions & 6 deletions go-runtime/compile/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

// This file contains a parser for Go FTL directives.
//
// eg. //ftl:ingress GET /foo/bar
// eg. //ftl:ingress http GET /foo/bar

type directiveWrapper struct {
Directive directive `parser:"'ftl' ':' @@"`
Expand All @@ -24,14 +24,50 @@ type directiveWrapper struct {
//sumtype:decl
type directive interface{ directive() }

type directiveExport struct {
type directiveVerb struct {
Pos lexer.Position

Export bool `parser:"@'export'"`
Verb bool `parser:"@'verb'"`
Export bool `parser:"@'export'?"`
}

func (*directiveExport) directive() {}
func (d *directiveExport) String() string { return "ftl:export" }
func (*directiveVerb) directive() {}
func (d *directiveVerb) String() string {
if d.Export {
return "ftl:verb export"
}
return "ftl:verb"
}

type directiveData struct {
Pos lexer.Position

Data bool `parser:"@'data'"`
Export bool `parser:"@'export'?"`
}

func (*directiveData) directive() {}
func (d *directiveData) String() string {
if d.Export {
return "ftl:data export"
}
return "ftl:data"
}

type directiveEnum struct {
Pos lexer.Position

Enum bool `parser:"@'enum'"`
Export bool `parser:"@'export'?"`
}

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

type directiveIngress struct {
Pos schema.Position
Expand Down Expand Up @@ -68,7 +104,7 @@ var directiveParser = participle.MustBuild[directiveWrapper](
participle.Elide("Whitespace"),
participle.Unquote(),
participle.UseLookahead(2),
participle.Union[directive](&directiveExport{}, &directiveIngress{}, &directiveCronJob{}),
participle.Union[directive](&directiveVerb{}, &directiveData{}, &directiveEnum{}, &directiveIngress{}, &directiveCronJob{}),
participle.Union[schema.IngressPathComponent](&schema.IngressPathLiteral{}, &schema.IngressPathParameter{}),
)

Expand Down
Loading

0 comments on commit 69a3f42

Please sign in to comment.