-
Notifications
You must be signed in to change notification settings - Fork 3
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 experimental access to ast #108
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Carson Farmer <[email protected]>
Signed-off-by: Carson Farmer <[email protected]>
Just taking an early look here. This is gonna be really nice for the studio, and also SDK features in general! |
Signed-off-by: Carson Farmer <[email protected]>
Signed-off-by: Carson Farmer <[email protected]>
Hey @joewagner can you take a look at this? Also, ideally we'll rebuild the TS types from the Go structs at publish time. What do you think is the best way to go about this? |
I'll take a look asap! |
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.
@carsonfarmer This is gonna be a really nice feature. Let's release this week!
Signed-off-by: Carson Farmer <[email protected]>
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.
Also, perhaps @brunocalza can take a quick look here?
Signed-off-by: Carson Farmer <[email protected]>
Signed-off-by: Carson Farmer <[email protected]>
Looks good. The only comment is that with this we're exposing the AST, which means the AST becomes an API. Changes in the AST without breaking clients become way harder to make. The AST structure seems very stable, so maybe that's not a big deal. And also fine if the benefit of exposing is greater than the pain of breaking changes :) |
This is a very valid concern. I did mark the API as @internal, so ideally breaking changes are ok and expected. But I do think the benefits here outweigh the downsides? Also, I'm not actually sure that anyone other than us are actually using the parser in js directly? |
Signed-off-by: Carson Farmer <[email protected]>
Signed-off-by: Carson Farmer <[email protected]>
This is looking good @carsonfarmer. Related to @brunocalza's comment about exposing the actual AST, just want to remind you that it is not the actual AST data structure we'll be working with in Studio. There is a JSON data structure derived from the AST which is what the gateway returns when you get metadata about a table. That is what we store when importing a table to Studio, and that JSON structure is what we want to create/store when creating a table in Studio. You can see the validator code that processes the AST into that JSON object here: https://github.com/tablelandnetwork/go-tableland/blob/main/internal/gateway/impl/gateway_store.go#L98C4-L98C4 With direct access to the AST in Studio, we'd have to port that JSON-creating code in order to make the same JSON structure, or possibly there is a way to keep the AST internal and just have your If that direction does seem appealing, it would make sense to extract the AST <-> JSON Go code to some shared file that could be used by both the gateway and parser Go code. |
No this PR doesn't expose the full AST to the client. It only exposes this type: export interface CreateTable {
Table: Table | null;
ColumnsDef: ColumnDef[] | null;
Constraints: TableConstraint[] | null;
StrictMode: boolean;
} And this type is what is already exported from the parser via this PR. This type is the structure you'd need to get the table definitions into (see js/go-types.d.ts). There isn't an easy way to create a create statement from what you currently have in the client. It would require parsing the constraints into their specific constraint types (which are exposed as interfaces/types here). I was thinking you could use this type rather than the one you get from the API (because this one is more expressive). I can probably create a JS/TS wrapper in the studio to convert from string constraints to constraint objects... |
Signed-off-by: Carson Farmer <[email protected]>
Signed-off-by: Carson Farmer <[email protected]>
Signed-off-by: Carson Farmer <[email protected]>
Signed-off-by: Carson Farmer <[email protected]>
* @param {any} schema | ||
* @returns {string} The CREATE statement string | ||
*/ | ||
function generateCreateTableStatement(tableName, schema) { |
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 function converts from your Schema format into a create statement string @asutula. From there, we can generate the more verbose/complete AST format. However, we can't round-trip any constraints that include an expression, only things like primary key, unique, etc. Not check, generated as, and others.
Summary
Adds support (via updating the compiler) for full access to the underlying AST object. However, in this PR we focus solely on access to CreateTable statements to keep things simple and consistent.
It adds two new functions:
It also adds support for deriving types from go structs thanks to github.com/OneOfOne/struct2ts. The Makefile now has a new command that generates Typescript types from the relevant Go structs, and also creates a valid d.ts file to use.
WARNING: This does appear to increase the built size quite a bit. We might now be at the point where the size is unacceptably large.
Before:
After:
Context
This was based on a request from building Studio.
Implementation overview
We updated the compiler, and added an experimental function to return a raw JSON AST component for CreateTable statements.
Implementation details and review orientation
This is just a first step, it requires tests, a better API, etc
Checklist