-
Notifications
You must be signed in to change notification settings - Fork 83
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
Generated models can be difficult to use due to unnecessary pointer types #19
Comments
Thanks for the feedback. A lot of thought was given in to how to handle the generated models, although that doesn't mean I got it right. I agree that the verbosity of creating a new struct with hard coded values is not great. Let me walk through the thinking behind the current approach. The service is written in c#, so we need to be able to represent all the data in Go that can be represented in c#. Since structs in Go can not be null, we are forced to use a pointer for struct fields, so that the nil pointer can represent that null state.
Simple types like This leaves us with the decision on how to handle the simple types that are not nullable: Option 1: Keep all simple type fields as pointers. This has the drawback of extra verbosity. The benefits are in the consistency. The consumer does not have to be confused at why some Option 2: Do not use pointers for simple types that are not nullable. This removes a small amount of the verbosity, making it very easy to set values on those fields. The drawback is that this only helps in a subset of fields, and introduces the inconsistency mentioned above. What are your thoughts? Of the two options, which would have been better for your project? |
@nmiodice , have you had a chance to review my response above? Do you have any feedback regarding the response? |
I'm glad to see a complete Azure Devops library for go coming to fruition, thanks for your effort! I've been following the current go-github approaches, which is to use pointers for all struct members, along with generated accessors and helper functions like https://github.com/google/go-github/blob/master/github/github.go#L1042-L1056. They discussed this topic in several issues over the years. There are some interesting comments about taking a different approach if they started over, like google/go-github#537 (comment), but they stuck with pointers for consistency. From a consumer and new amateur Go user standpoint, I don't mind the extra verbosity too much. I appreciate the readability and don't mind checking for nil all over the place. |
Thanks for the info/feedback @mcdafydd. Based on the information you provided, I think it makes the most sense to keep things as they are. I am closing this issue for now, and we can reopen if a compelling alternative pops up. |
The generated models can be difficult to use with primitive types like
string
,int
andbool
because they are consumed as pointers and can make the code more verbose.For example, in order to use this struct with hard-coded values, I need to write my code like so:
Without pointers, it can be simplified to this:
Note: the current strategy probably lowers the runtime memory footprint of an application for larger types like
strings
,structs
,maps
, etc... but the efficiency gain is lost forint
andbool
(i believe).The text was updated successfully, but these errors were encountered: