Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support enums in go runtime #1006

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/schema/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ func (*Any) schemaType() {}
func (*Any) schemaDecl() {}
func (*Any) String() string { return "Any" }
func (a *Any) ToProto() proto.Message { return &schemapb.Any{Pos: posToProto(a.Pos)} }
func (*Any) GetName() string { return "" }
1 change: 1 addition & 0 deletions backend/schema/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ func (*Bool) schemaType() {}
func (*Bool) schemaDecl() {}
func (*Bool) String() string { return "Bool" }
func (b *Bool) ToProto() proto.Message { return &schemapb.Bool{Pos: posToProto(b.Pos)} }
func (*Bool) GetName() string { return "" }
1 change: 1 addition & 0 deletions backend/schema/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ func (*Bytes) schemaType() {}
func (*Bytes) schemaDecl() {}
func (*Bytes) String() string { return "Bytes" }
func (b *Bytes) ToProto() proto.Message { return &schemapb.Bytes{Pos: posToProto(b.Pos)} }
func (*Bytes) GetName() string { return "" }
2 changes: 2 additions & 0 deletions backend/schema/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ func (d *Data) schemaChildren() []Node {
return children
}

func (d *Data) GetName() string { return d.Name }

func (d *Data) String() string {
w := &strings.Builder{}
fmt.Fprint(w, encodeComments(d.Comments))
Expand Down
3 changes: 3 additions & 0 deletions backend/schema/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func (d *Database) ToProto() proto.Message {
Comments: d.Comments,
}
}

func (d *Database) GetName() string { return d.Name }

func DatabaseFromProto(s *schemapb.Database) *Database {
return &Database{
Pos: posFromProto(s.Pos),
Expand Down
2 changes: 2 additions & 0 deletions backend/schema/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func (e *Enum) ToProto() proto.Message {
}
}

func (e *Enum) GetName() string { return e.Name }

func EnumFromProto(s *schemapb.Enum) *Enum {
return &Enum{
Pos: posFromProto(s.Pos),
Expand Down
1 change: 1 addition & 0 deletions backend/schema/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ func (*Float) schemaType() {}
func (*Float) schemaDecl() {}
func (*Float) String() string { return "Float" }
func (f *Float) ToProto() proto.Message { return &schemapb.Float{Pos: posToProto(f.Pos)} }
func (*Float) GetName() string { return "" }
1 change: 1 addition & 0 deletions backend/schema/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ func (*Int) schemaChildren() []Node { return nil }
func (*Int) schemaType() {}
func (*Int) String() string { return "Int" }
func (i *Int) ToProto() proto.Message { return &schemapb.Int{Pos: posToProto(i.Pos)} }
func (*Int) GetName() string { return "" }
2 changes: 1 addition & 1 deletion backend/schema/metadatacalls.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (m *MetadataCalls) String() string {
w += len(str)
fmt.Fprint(out, str)
}
fmt.Fprintln(out)
fmt.Fprint(out)
return out.String()
}

Expand Down
2 changes: 2 additions & 0 deletions backend/schema/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ func (m *Module) ToProto() proto.Message {
}
}

func (m *Module) GetName() string { return m.Name }

// ModuleFromProtoFile loads a module from the given proto-encoded file.
func ModuleFromProtoFile(filename string) (*Module, error) {
data, err := os.ReadFile(filename)
Expand Down
1 change: 1 addition & 0 deletions backend/schema/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ type Value interface {
//sumtype:decl
type Decl interface {
Node
GetName() string
schemaDecl()
}

Expand Down
1 change: 0 additions & 1 deletion backend/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ module todo {
verb create(todo.CreateRequest) todo.CreateResponse
+calls todo.destroy


verb destroy(builtin.HttpRequest<todo.DestroyRequest>) builtin.HttpResponse<todo.DestroyResponse, String>
+ingress http GET /todo/destroy/{id}
}
Expand Down
1 change: 1 addition & 0 deletions backend/schema/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ func (*String) schemaType() {}
func (*String) schemaDecl() {}
func (*String) String() string { return "String" }
func (s *String) ToProto() proto.Message { return &schemapb.String{Pos: posToProto(s.Pos)} }
func (*String) GetName() string { return "" }
1 change: 1 addition & 0 deletions backend/schema/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ func (*Time) schemaType() {}
func (*Time) schemaDecl() {}
func (*Time) String() string { return "Time" }
func (t *Time) ToProto() proto.Message { return &schemapb.Time{Pos: posToProto(t.Pos)} }
func (*Time) GetName() string { return "" }
1 change: 1 addition & 0 deletions backend/schema/typeparameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func (t *TypeParameter) ToProto() protoreflect.ProtoMessage {
}
func (t *TypeParameter) schemaChildren() []Node { return nil }
func (t *TypeParameter) schemaDecl() {}
func (t *TypeParameter) GetName() string { return t.Name }

func typeParametersToSchema(s []*schemapb.TypeParameter) []*TypeParameter {
var out []*TypeParameter
Expand Down
1 change: 1 addition & 0 deletions backend/schema/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ func (u *Unit) schemaDecl() {}
func (u *Unit) String() string { return "Unit" }
func (u *Unit) ToProto() protoreflect.ProtoMessage { return &schemapb.Unit{Pos: posToProto(u.Pos)} }
func (u *Unit) schemaChildren() []Node { return nil }
func (u *Unit) GetName() string { return "" }
10 changes: 10 additions & 0 deletions backend/schema/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,16 @@ func ValidateModule(module *Module) error {
return next()
})
merr = cleanErrors(merr)
sort.SliceStable(module.Decls, func(i, j int) bool {
iDecl := module.Decls[i]
jDecl := module.Decls[j]
iType := reflect.TypeOf(iDecl).String()
jType := reflect.TypeOf(jDecl).String()
if iType == jType {
return iDecl.GetName() < jDecl.GetName()
}
return iType < jType
})
return errors.Join(merr...)
}

Expand Down
3 changes: 3 additions & 0 deletions backend/schema/verb.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func (v *Verb) schemaChildren() []Node {
}
return children
}

func (v *Verb) GetName() string { return v.Name }

func (v *Verb) String() string {
w := &strings.Builder{}
fmt.Fprint(w, encodeComments(v.Comments))
Expand Down
89 changes: 89 additions & 0 deletions buildengine/build_go_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package buildengine

import (
"testing"

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

func TestGenerateGoModule(t *testing.T) {
sch := &schema.Schema{
Modules: []*schema.Module{
schema.Builtins(),
{Name: "other", Decls: []schema.Decl{
&schema.Enum{
Name: "Color",
Type: &schema.String{},
Variants: []*schema.EnumVariant{
{Name: "Red", Value: &schema.StringValue{Value: "Red"}},
{Name: "Blue", Value: &schema.StringValue{Value: "Blue"}},
{Name: "Green", Value: &schema.StringValue{Value: "Green"}},
},
},
&schema.Enum{
Name: "ColorInt",
Type: &schema.Int{},
Variants: []*schema.EnumVariant{
{Name: "RedInt", Value: &schema.IntValue{Value: 0}},
{Name: "BlueInt", Value: &schema.IntValue{Value: 1}},
{Name: "GreenInt", Value: &schema.IntValue{Value: 2}},
},
},
&schema.Data{Name: "EchoRequest"},
&schema.Data{Name: "EchoResponse"},
&schema.Verb{
Name: "echo",
Request: &schema.DataRef{Name: "EchoRequest"},
Response: &schema.DataRef{Name: "EchoResponse"},
},
}},
{Name: "test"},
},
}
expected := `// Code generated by FTL. DO NOT EDIT.

//ftl:module other
package other

import (
"context"
)

var _ = context.Background

//ftl:enum
type Color string
const (
Red Color = "Red"
Blue Color = "Blue"
Green Color = "Green"
)

//ftl:enum
type ColorInt int
worstell marked this conversation as resolved.
Show resolved Hide resolved
const (
RedInt ColorInt = 0
BlueInt ColorInt = 1
GreenInt ColorInt = 2
)

type EchoRequest struct {
}

type EchoResponse struct {
}

//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()")
}
`
bctx := buildContext{
moduleDir: "testdata/modules/another",
buildDir: "_ftl",
sch: sch,
}
testBuild(t, bctx, []assertion{
assertGeneratedModule("go/modules/other/external_module.go", expected),
})
}
36 changes: 5 additions & 31 deletions buildengine/build_kotlin.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,36 +158,7 @@ var scaffoldFuncs = scaffolder.FuncMap{
"imports": func(m *schema.Module) []string {
imports := sets.NewSet[string]()
_ = schema.Visit(m, func(n schema.Node, next func() error) error {
switch n := n.(type) {
worstell marked this conversation as resolved.
Show resolved Hide resolved
case *schema.DataRef:
decl := m.Resolve(schema.Ref{
Module: n.Module,
Name: n.Name,
})
if decl != nil {
if data, ok := decl.Decl.(*schema.Data); ok {
if len(data.Fields) == 0 {
imports.Add("ftl.builtin.Empty")
break
}
}
}

if n.Module == "" {
break
}

imports.Add("ftl." + n.Module + "." + n.Name)

for _, tp := range n.TypeParameters {
tpRef, err := schema.ParseDataRef(tp.String())
if err != nil {
return err
}
if tpRef.Module != "" && tpRef.Module != m.Name {
imports.Add("ftl." + tpRef.Module + "." + tpRef.Name)
}
}
switch n.(type) {
case *schema.Verb:
imports.Append("xyz.block.ftl.Context", "xyz.block.ftl.Ignore", "xyz.block.ftl.Verb")

Expand All @@ -214,12 +185,15 @@ func genType(module *schema.Module, t schema.Type) string {
if decl != nil {
if data, ok := decl.Decl.(*schema.Data); ok {
if len(data.Fields) == 0 {
return "Empty"
return "ftl.builtin.Empty"
}
}
}

desc := t.Name
if t.Module != "" {
desc = "ftl." + t.Module + "." + desc
}
if len(t.TypeParameters) > 0 {
desc += "<"
for i, tp := range t.TypeParameters {
Expand Down
Loading