-
-
Notifications
You must be signed in to change notification settings - Fork 200
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
Nullable types in golang #1418
Comments
Welcome to AsyncAPI. Thanks a lot for reporting your first issue. Please check out our contributors guide and the instructions about a basic recommended setup useful for opening a pull request. |
@wiegell thanks for opening the issue! As I am not that well versed in Go, hope you don't mind answering some questions 😅 Is it ALL types that just need v2 actually can handle this, we just need to alter a few lines of code for the type generation 🙂 |
Yes all pointers (*) are nullable, but some value types can also be nil. See this proposal: golang/go#30177 Motivation -> 2nd bullet explains using a pointer to simulate nullable. So it would be needed to add * to the golang type corresponding to a nullable version of the following asyncapi types: I presume asyncapi array type is translated to golang slice (which is nullable in contrast to a golang array of fixed length), so in that case a value type without * will suffice. I'm not with my computer the next couple of days, but i would like to check if my reasoning here checks out with the graphql generator mentioned above, and i'll get back to you! |
As a really good example you can check this project for struct generation: https://github.com/deepmap/oapi-codegen But we also need to take care of non required with the |
So i just checked with openapi - they use string pointers for non-required fields as well and then value types when the string is required. |
I started using AsyncAPI generation for Go and found this current limitation, which is a deal breaker for me as well. |
In Modelina v2, the constraint logic now have access to nullable and non-required fields, so all we have to change is: https://github.com/asyncapi/modelina/blob/master/src/generators/go/GoConstrainer.ts You can use the Java constrainer as reference:
Happy to help guide as much as needed 🙂 |
Thanks @jonaslagoni for pointing out the file. I'll take a look at that some time this week. |
Just an update: I ended up writing my own template and made a quick fix for the nullable types I needed, which is not worth sharing since it's more a hack than a proper solution. So I won't spend time with this ticket, unfortunately. |
🎉 This issue has been resolved in version 4.0.0-next.31 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Reason/Context
Please try answering few of those questions
Nullable strings are vital in many languages, also in go! In our current project we have put a lot of effort to support nullability between frontend and bff (graphql) - if it cannot be kept between services (asyncapi), it's a dealbreaker!
Currently
type: ["integer", "null"]
is translated tointerface{}
which is not typesafe. Other generators, e.g. https://github.com/99designs/gqlgen solves the problem by converting optional primitives to pointers, which are nullable. E.g. a typestring
becomes type*string
.53edd32
This change addresses nullable types in typescript - could it be expanded to go?
Description
Please try answering few of those questions
Yes
The text was updated successfully, but these errors were encountered: