Add support for functions, arbitrary tag names and exporting all types#1
Draft
gwennlbh wants to merge 5 commits intoolahol:mainfrom
Draft
Add support for functions, arbitrary tag names and exporting all types#1gwennlbh wants to merge 5 commits intoolahol:mainfrom
gwennlbh wants to merge 5 commits intoolahol:mainfrom
Conversation
TODO: generalize to an option, that takes in an array of tag names to use to name struct fields
Author
|
Exemple usage in my project (see https://github.com/ortfo/gui/blob/e248e2c73baf5f1900e7e970766f6e25066b453a/backend/main.go#L284) typescript := tsreflect.New(tsreflect.ExportEverything())
// ... //
for name, function := range BackendFunctions {
// binds the function to the webview
w.Bind(fmt.Sprintf("backend__%s", name), function)
// Generate comma-separated list of arguments for the call (looks like "arg0, arg1," etc)
argslist := ""
for i := 0; i < reflect.TypeOf(function).NumIn(); i++ {
if i > 0 {
argslist += ", "
}
argslist += fmt.Sprintf("arg%d", i)
}
typescript.AddFunc(
reflect.TypeOf(function),
name,
true,
fmt.Sprintf("return backend__%s(%s);", name, argslist)
)
}
wd, _ := os.Getwd()
os.WriteFile(filepath.Join(wd, "../frontend/backend.generated.ts"), []byte(typescript.DeclarationsTypeScript()), 0644)Where The output generated file looks like this: https://github.com/ortfo/gui/blob/e248e2c73baf5f1900e7e970766f6e25066b453a/frontend/backend.generated.ts#L20 |
Author
|
btw I can split this into 3 PRs if you prefer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi! I'm using this package to automatically generate types of bound functions in a webview desktop app.
Essentially, the webview allows exposing to frontend (JS) code functions that run on the Go backend.
With this package, the frontend code can very easily have typedefinitions for the bound functions with zero effort.
Obviously, since I need to use this patch right now (and vendoring packages seems complicated when using go workspaces), this PR contains a change that renames the module
... i'll remove it before merging (if you want to merge this) ^^
Thanks a bunch for this package! It's a real time saver! :D
This PR adds three things:
(*tsreflect.Generator).AddFunc(typ, name, async, implementation?)where:nameis the function's name (since it's not stored intyp)asynccan be set totrueto mark the function as asynchronous (this generatesasync function name(...): Promise<...>instead offunction name(...): ...implementation(optional argument) is a string with source code, useful when the implementation is easy, like in my use case. Adds a body to the function:export function ... { ... }instead ofexport function ...exportkeyword beforeinterface/function:tsreflect.ExportEverythingjsonto take precedence when naming struct fields: in my use case, I have alsoyamltags on some structs since they are (un)marshaled from and to YAML files. sometimes there are both YAML and JSON tags, when they differ, sometimes not. For now YAML is hardcoded but I plan to make another option:tsreflect.UseTags(names... string)that takes tag names, in their order of priority (last elements are used as fallback when the elements before are not tag names on the structs' fields)Note that error values in the return types are handled correctly, they are elided from the generated code, since they should, in their typescript implementation, be translated into
throwsNote also: I haven't done the jsdoc part yet, but I suppose errors could be added there as a
@throws, but appart from adding@throws {Error}i can't see where we would get more specific infoTODO:
WithStructTags