asty icon indicating copy to clipboard operation
asty copied to clipboard

Discussion: provide a typescript definition for the generated content?

Open xhd2015 opened this issue 2 years ago • 10 comments

I'm using the generated json to do some code generation, like automatically generate an HTTP handle from an ordinary golang func, and I choose typescript to write the script because of its convienence. I think it will be good to provide a typescript description for the generated JSON stuff.

P.S: I'm also using AST to do lots automation stuff, like code rewritten with function trace, testing with function mock inserted

xhd2015 avatar Apr 20 '23 10:04 xhd2015

@xhd2015 Thanks for the comment. What form of typescript definitions do you think will be the best for this? There is no API for typescript so not much to put into .d.ts. Do you have an example?

Evgenus avatar May 18 '23 18:05 Evgenus

I've just tried that and it works so well that I never again want to write a struct by just taking parameters from a function's parameter and converting them to upper case and add json tag to them.

The tools is still on the way...

Anyway, here is a glance at files that I used to help me writing typescript script:

export enum NodeType {
    Ident = "Ident",
    File = "File",
    GenDecl = "GenDecl",
   // ...
}

export interface AST {
    NodeType: NodeType
}

export interface File extends AST {
    FileSet: FileSet
    Package: Position
    Name: Ident
    Decls: Decl[] | FuncDecl[]
}

export interface GenDecl extends AST {
    Tok: Token
    TokPos: Position
    Lparen: Position
    Rparen: Position
    Specs: Spec[]
}

//...

util.ts

import * as shell from "@node-ext/shell";
import { File } from "./ast-go";

// asty go2json -input main.go -comments -imports -positions
export async function parseAst(file: string): Promise<File> {
    const content = await shell.exec(["asty", "go2json", "-input",file, "-comments", "-imports", "-positions"])
    return JSON.parse(content)
}

xhd2015 avatar May 19 '23 13:05 xhd2015

Today I just made the tool looks like a chain: image

As the above image shows, the tool is able to take parameters of any go function and wrap them in a struct which can be consumed by an HTTP handler.

But not only this, the tool can then generate a typescript interface definition from the generated struct. That is, a chain of generation tools.

And eventually it will generate an API function for the js code, and if that is a table in the database, it can generate react tables and forms based on that information. In the end, we will just need to write a sql like CREATE TABLE ... and then everything from backend to frontend will be generated using such tool.

xhd2015 avatar May 20 '23 06:05 xhd2015

@xhd2015 That is an excellent application for asty. I would happily host your work on asty-org if you desire. I am also working on JSON transformation library to be used on top of asty output. Check this out https://github.com/transon-org/transon. There is a list of alternatives in readme. I see you are using jd. Rego language is also a powerful tool for JSON manipulation, rather not for code generation, though.

PS:

Anyway, here is a glance at files that I used to help me writing typescript script

Perhaps, this can be also generated )) I was always fascinated with the idea that something is generated using it's own code of the previous version.

Evgenus avatar May 21 '23 22:05 Evgenus

@xhd2015 hey. How are you doing? Do you have any progress? Can I assist somehow?

Evgenus avatar Jun 03 '23 22:06 Evgenus

@xhd2015 hey. How are you doing? Do you have any progress? Can I assist somehow?

Sorry for late reply, I was working on some messy tasks last two weeks and had little time to continue on this.

Back to this topic, I still haven't come up with a natural way to fit various customized needs for generating code from AST.

The core problem here is to convert arbitrary AST to arbitrary text, which involves templating language.

You see I wrote a tool to convert function to HTTP handler, that's requires a little dirty work which is implemented in javascript.

If we can provide an easy-to-write templating language that takes source code as input and convert user-written template to generated code, that would be very amazing.

If we want to put something on asty-org I think it should be easy to understand and easy to use. I haven't just found out a generic solution to this problem, and still working on that.

xhd2015 avatar Jun 04 '23 02:06 xhd2015

Currently I'm working on a terminal replacement tool, like the image I posted above. It basically executes given cmd with input by user, and records the command history.

The most important feature of this terminal-like tool is to automatically execute the code when input or cmd changes, and developer can see their result immediately. This may help us write scripts more efficiently.

My thought is to combine the AST tool with modern web technology, and let it modify the filesystem directly. It somehow feels like a web-based code editor, which I'm afraid is just another solved problem and I should instead look for a vscode extension solution.

xhd2015 avatar Jun 04 '23 02:06 xhd2015

If we can provide an easy-to-write templating language that takes source code as input and convert user-written template to generated code, that would be very amazing.

Do you see this as a template language on top of JSON or something in the golang syntax? If you think of JSON templating there are plenty of solutions including mine https://github.com/transon-org/transon

If we want to put something on asty-org I think it should be easy to understand and easy to use. I haven't just found out a generic solution to this problem, and still working on that.

Sure. I will be happy to help/host/co-maintain.

Evgenus avatar Jun 06 '23 21:06 Evgenus

transon is wonderful for JSON transforming.However to transform go code, do we need a more go-specific template engine? It would be very convenient if we can just refer to common go concepts in the template. For example:

{{ for f in funcs}}
type {{f.name.toUpperCase}}Req struct {
    {{ for field in f.argsWithoutCtx }}
        {{field.name.toUpperCase} {{field.type}} `json:"{{field.name.toCamelCase}}"`
   {{ end }}
}
    func {{f.name}}({{f.ctx}}, req *{{f.name.toUpperCase}}Req) ...
{{ end }}

This is just a demo that I use as an example, it is actually just a plain usage of the go template. But it's still too verbose, which is not ideal in this simple task.

xhd2015 avatar Jun 14 '23 07:06 xhd2015

In short, it should be as simple as a VSCode code snippet to define, and to use.

xhd2015 avatar Jun 14 '23 07:06 xhd2015