gzuidhof / tygo

Generate Typescript types from Golang source code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature request for only converting only certain structs

JamesTiberiusKirk opened this issue · comments

Sorry, tapped enter before I finished and it published it straight away.
First of all I wanna say that I love this package and its almost what im looking for!

But it might be a good idea to see if we can say only generate ts types for only certain structs.
Example: only for structs which match a name *DTO or *Data.
or another Example: only for structs which have the json tags

If we agree I could even help out and open a pr for this.

Im playing around with the codebase at the moment.
Maying im being a bit daft but based on name it seems a bit hard todo.
Maybe another idea is to leave a comment on each struct that we want to include
something like TsType anywhere in the comment for the ast.Node

Hey Dumitru,

One way to do this today is by splitting the structs you want into a file, and then using file inclusion or exclusion to get there with these config fields. As Golang doesn't really care in which files you put code, I generally find it's easy enough to split your structs across some files.

Would this work for you? Then we don't have to introduce any new code

I believe exclusion isn't particularly documented in the README - perhaps that is to be improved.

no, sepcarate files wount do for my use case unfortunatly
I have just finished an implementation of what I was talking about though.
Its not a lot of code and i think simple enouth. Ill open up a PR so you can have a look at it

Thanks for replying so fast btw :)

I don't know what your current draft implementation looks like of course, but perhaps we can allow users to specify it in their config file instead of putting tags into the Go code?

Of course either can work

yes, so what I have done is added another field in the config yaml (include_struct_comment) where the user can specify a specific string to use.
if the string has been specified, then the user would need to add a new comment line to each and every struct they would want to be transpiled to ts.
I have modified one of the examples in the repo to shocase the feature

I meant in the direction of:

include_symbols:
  - "*Suffix" # a glob with wildcard
  - "SomeConstant"

Just to make sure im not missunderstanding...
And the symbols will be used as "tags" left by the user in the comments of the struct?

I'm proposing that the user, in the config, specifies the structs / consts / types that they want present. Either with their exact names, or matching some regex. So there would be no special comments at all. What are your thoughts on that approach?

Also of course we can also have exclude_symbols if one only wants to exclude some instead.

yeah i like that approach, but not gunna like im not sure how i can get access to the node name from *ast.GenDecl in package_generator.go

unless you can think of a better place of implementing this?

I'm happy to have a look at it, but it will probably only be in the weekend :/. I imagine we can traverse the tree down a bit to find the name if we have to (if generation takes 1ms longer it's no problem).

ok great. let me know if theres anything else I can do

did you manage to have a look at all @gzuidhof ?

I'm sorry, I haven't gotten around to it yet :(