-
Notifications
You must be signed in to change notification settings - Fork 8
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: add type aliases, refactor go schema parsing #1493
Conversation
making this a draft because its failing tests and I'm done for the night |
5336209
to
af9a4b1
Compare
Actually tests are succeeding, now just lint failing (sumtypes). Will submit and fix that tomorroq |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Love all the tests :)
mdecl := scopes.Resolve(*n) | ||
if mdecl == nil && n.Module == "" { | ||
merr = append(merr, errorf(n, "unknown reference %q", n)) | ||
} | ||
if mdecl := scopes.Resolve(*n); mdecl != nil { | ||
if mdecl != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh nice! :)
go-runtime/compile/schema_test.go
Outdated
`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 type alias or enum: NonFTLAlias`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was a bit confused by this error message at first. I wonder if shuffling it around would make it more clear 🤔
type "NonFTLAlias" is not declared as an ftl type alias or enum
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, but also in general I tend to leave out the typename iff the error is pointing directly at it already, as it's redundant information (not sure if that is the case here).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Switched it to:
type is not declared as an ftl enum or type alias
@@ -117,3 +119,16 @@ type PrivateData struct{} | |||
func (PrivateData) exportedTypeEnum() {} | |||
|
|||
var invalidConfig = ftl.Config[string]("not an identifier") | |||
|
|||
// There can be only one |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added @worstell as she's been touching this code a lot recently! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huge!
Who knew typealias support would be such a PITA?!
@@ -15,59 +15,71 @@ type TypeEnum interface { | |||
tag() | |||
} | |||
|
|||
type Bool bool | |||
//ftl:typealias |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not for this PR but I'm wondering if we should move these tests into go-runtime
...te/_ftl/go/modules/{{ range .NonMainModules }}{{ push .Name . }}{{ end }}/external_module.go
Outdated
Show resolved
Hide resolved
if maybeVisitTypeEnumVariant(pctx, node, directives) { | ||
return | ||
} | ||
maybeVisitTypeEnumVariant(pctx, node, directives) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this intentional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This allowed this case: #1492
Updating it now to collect errors for subsequent type enums it conforms to
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually no. This is there because even though something is a variant we still need to continue to process it's declaration. eg: a type alias that conforms to am enum.
go-runtime/compile/schema_test.go
Outdated
`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 type alias or enum: NonFTLAlias`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, but also in general I tend to leave out the typename iff the error is pointing directly at it already, as it's redundant information (not sure if that is the case here).
f5c8aed
to
9612b84
Compare
#1238
Adds the ability to declare type aliases in modules
There is a lot of overlap between typealiases and enums. They both redefine an existing type as a new type, and therefore need to make sure our schema parsing does not skip this defined type by looking at the underlying type.
There were also issues if implicit exports, enum cases (and more?) were encountered in the ast tree before we found the typealias or enum definition.
This PR solves that by doing an initial pass just to find all typealias and enum declarations for the module and then doing the normal pass.
Previously typealiases were allowed without any declaration but they would just fall back to the underlying type.
This PR makes this an error as we do not know if this type should be an enum or a type alias. We may want to discuss this more.
fixes #1475 #1476 #1477 #1492 #1449