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

fix: do not require explicit annotation on type enum variants #1520

Merged
merged 1 commit into from
May 17, 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
3 changes: 0 additions & 3 deletions buildengine/testdata/projects/another/another.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,17 @@ type TypeEnum interface {
tag()
}

//ftl:typealias export
type A int

func (A) tag() {}

//ftl:typealias export
type B string

func (B) tag() {}

//ftl:enum export
type SecondTypeEnum interface{ typeEnum() }

//ftl:typealias export
type One int

func (One) typeEnum() {}
Expand Down
12 changes: 0 additions & 12 deletions buildengine/testdata/projects/other/other.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,34 @@ type TypeEnum interface {
tag()
}

//ftl:typealias
type MyBool bool

func (MyBool) tag() {}

//ftl:typealias
type MyBytes []byte

func (MyBytes) tag() {}

//ftl:typealias
type MyFloat float64

func (MyFloat) tag() {}

//ftl:typealias
type MyInt int

func (MyInt) tag() {}

//ftl:typealias
type MyTime time.Time

func (MyTime) tag() {}

//ftl:typealias
type List []string

func (List) tag() {}

//ftl:typealias
type Map map[string]string

func (Map) tag() {}

//ftl:typealias
type MyString string

func (MyString) tag() {}
Expand All @@ -59,12 +51,10 @@ type Struct struct{}

func (Struct) tag() {}

//ftl:typealias
type MyOption ftl.Option[string]

func (MyOption) tag() {}

//ftl:typealias
type MyUnit ftl.Unit

func (MyUnit) tag() {}
Expand All @@ -74,12 +64,10 @@ type SecondTypeEnum interface {
tag2()
}

//ftl:typealias
type A string

func (A) tag2() {}

//ftl:typealias
type B EchoRequest

func (B) tag2() {}
Expand Down
8 changes: 1 addition & 7 deletions go-runtime/compile/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,8 +658,7 @@ func visitGenDecl(pctx *parseContext, node *ast.GenDecl) {
if sType, ok := visitType(pctx, node.Pos(), typ, isExported).Get(); ok {
typeAlias.Type = sType
} else {
pctx.errors.add(errorf(node, "unsupported type %q for type alias",
pctx.pkg.TypesInfo.TypeOf(t.Type).Underlying()))
pctx.errors.add(errorf(node, "unsupported type %q for type alias", typ.Underlying()))
}
} else {
visitType(pctx, node.Pos(), pctx.pkg.TypesInfo.Defs[t.Name].Type(), isExported)
Expand Down Expand Up @@ -1219,15 +1218,10 @@ func visitType(pctx *parseContext, pos token.Pos, tnode types.Type, isExported b
switch underlying := tnode.Underlying().(type) {
case *types.Basic:
if named, ok := tnode.(*types.Named); ok {
if _, ok := visitType(pctx, pos, named.Underlying(), isExported).Get(); !ok {
return optional.None[schema.Type]()
}
ref, doneWithVisit := visitNamedRef(pctx, pos, named)
if doneWithVisit {
return ref
}
pctx.errors.add(noEndColumnErrorf(pos, "type is not declared as an ftl enum or type alias"))
return optional.None[schema.Type]()
}

switch underlying.Kind() {
Expand Down
14 changes: 3 additions & 11 deletions go-runtime/compile/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,9 @@ func TestExtractModuleSchema(t *testing.T) {

database postgres testDb

export typealias Blob String

export typealias List [String]

export enum BlobOrList {
Blob one.Blob
List one.List
Blob String
List [String]
}

export enum Color: String {
Expand Down Expand Up @@ -167,16 +163,14 @@ func TestExtractModuleSchemaTwo(t *testing.T) {
assert.Equal(t, r.MustGet().Errors, nil)
actual := schema.Normalise(r.MustGet().Module)
expected := `module two {
export typealias Scalar String

export enum TwoEnum: String {
Red = "Red"
Blue = "Blue"
Green = "Green"
}

export enum TypeEnum {
Scalar two.Scalar
Scalar String
List [String]
Exported two.Exported
WithoutDirective two.WithoutDirective
Expand Down Expand Up @@ -439,8 +433,6 @@ func TestErrorReporting(t *testing.T) {
`117:1-26: parent enum "ExportedTypeEnum" is exported, but directive "ftl:data" on "PrivateData" is not: all variants of exported enums that have a directive must be explicitly exported as well`,
`121:21-60: config and secret names must be valid identifiers`,
`127:1-26: only one directive expected for type alias`,
`133:2-2: type is not declared as an ftl enum or type alias`,
`133:2-7: unsupported type "ftl/failing.NonFTLAlias" for field "Value"`,
`143:1-35: type can not be a variant of more than 1 type enums (TypeEnum1, TypeEnum2)`,
}
assert.Equal(t, expected, actual)
Expand Down
2 changes: 0 additions & 2 deletions go-runtime/compile/testdata/one/one.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,10 @@ const (
//ftl:enum
type BlobOrList interface{ blobOrList() }

//ftl:typealias
type Blob string

func (Blob) blobOrList() {}

//ftl:typealias
type List []string

func (List) blobOrList() {}
Expand Down
1 change: 0 additions & 1 deletion go-runtime/compile/testdata/two/two.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const (
//ftl:enum export
type TypeEnum interface{ typeEnum() }

//ftl:typealias export
type Scalar string

func (Scalar) typeEnum() {}
Expand Down
2 changes: 0 additions & 2 deletions integration/testdata/go/httpingress/httpingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ type SumType interface {
tag()
}

//ftl:typealias export
type A string

func (A) tag() {}

//ftl:typealias export
type B []string

func (B) tag() {}
Expand Down
Loading