tkrajina / typescriptify-golang-structs

A Golang struct to TypeScript class/interface converter
Apache License 2.0
505 stars 87 forks source link

fix ignored pointers #37

Closed oldbane closed 3 years ago

oldbane commented 3 years ago

I am having members show up in my typescript interfaces that are named -? which totally breaks the output and is incorrect. It turns out those were JSON-ignored pointer types on the go structs I was convering.

The issue happens because when parsing the tag inside the method getJSONFieldName on lines 518-520 the field name is appended a question mark if it is a pointer regardless if that field was actually marked as JSON-ignored. This then causes incorrectly filtering out JSON-ignored fields in the convertType method.

Another solution to this would be declaring a bool that sets to true if the field is ignored in the loop in getJSONFieldName and not appending a ? for ignored fields thus correctly preserving the "-" name. I would be happy to submit the alternate fix where we update that loop and if-statement in getJSONFieldName if you'd prefer that instead.

tkrajina commented 3 years ago

Yes, I agree that maybe fixing it in 518 makes sense. If you want, feel free to fix it there. But even your solution (with my suggestion to ignore everything starting with -, not only -?) should fix that problem.

Can you also add a test for this behaviour? You can use a simple test like https://github.com/tkrajina/typescriptify-golang-structs/blob/master/typescriptify/typescriptify_test.go#L350 as a template for a new test.

oldbane commented 3 years ago

I think that the detection of a JSON-ignored field should have happened prior to this line. Thus, I think the solution to this issue means that we should edit the getJSONFieldName to be more robust when dealing with ignored pointers fields. I will change my submission to be a fix in that method, and provide a test for it.

tkrajina commented 3 years ago

I agree, go for it 👍

oldbane commented 3 years ago

@tkrajina I have updated the fix and added a test :relaxed:

tkrajina commented 3 years ago

Thanks @nobane !

oldbane commented 3 years ago

My pleasure, thanks for making and working on this very useful project @tkrajina! I hope you have a good New Year.