-
Notifications
You must be signed in to change notification settings - Fork 43
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
Add automatically generated nil-tolerant access functions to the SDK #1103
Comments
FWIW I made a sketch of what this could look like at jim-minter@accessors ; in particular see jim-minter@accessors#diff-8fca08831d20d6fe1a0efbc2893dc0ffd083101c7f79f150e4d8c606ba63712b . The sketch isn't currently quite right (it doesn't handle dereferencing "enum" strings correctly). |
Another advantage of this suggestion is that, if implemented and taken to azure-sdk-for-go, all top-level ARM objects will all implement |
Another angle is that we have had production panics due to log lines like |
@JeffreyRichter and I discussed this early on in the design of the track 2 SDKs. We decided against it for the following reasons (these are the ones I remember, there might have been other reasons).
Additionally, fields that have the doc comment I'll also add that the example |
Also, if you're less concerned about the ambiguities of zero-values, you can achieve this today with a generic helper. func FromPtr[T any](v *T) T {
if v != nil {
return *v
}
return *new(T)
} Maybe this belongs in |
I'm dubious that the "too C#-ish" argument holds much water given that Go protobufs already do exactly this 😄 -- and having ARM types implement From the logging perspective, I'd rather write (for example) Also as nesting increases,
I think there's a key misunderstanding here, because it /does/ solve the problem, and it /does/ return the zero value of the See https://go.dev/play/p/OQI1T7uFLIE as an example. Say you have |
Agreed on the value of the "too C#-ish" argument. I only bring it up to ensure that we look at solutions through the lense of remaining idiomatic to the language and patterns established throughout the community. I missed that |
If we were to add these, how would a customer know when they need to care about an ambiguous zero-value and need to perform the |
If you want to know that the *string is nil, it's your choice and you can check that. the property is not made private, we only add a convenience accessor func. That convenience accessor func is particularly useful when traversing a deeply nested structure to reach the value of a field.
The above makes it clear IMO. The value of As stated above, this is how protobuf generated code works and it has not caused us any confusion in the years we've been using it. |
Currently, autorest.go models use pointers everywhere, risking nil pointer segmentation faults in client codebases. Adding boilerplate nil-check code to access fields safely makes client codebases less readable and maintainable and makes unit test coverage more expensive to achieve.
For instance, consider the following code snippet as a case in point:
To improve code quality, readability and maintainability, it would be great to add automatically generated nil-tolerant accessor functions to the SDK. This would simplify code at point of use and reduce segmentation fault risks.
Today, protobuf has a feature that does exactly this, and it's really useful. See examples from https://github.com/golang/protobuf/blob/5d5e8c018a13017f9d5b8bf4fad64aaa42a87308/internal/testprotos/proto3_proto/test.pb.go#L106 onwards ; generator at https://github.com/golang/protobuf/blob/5d5e8c018a13017f9d5b8bf4fad64aaa42a87308/protoc-gen-go/generator/generator.go#L1793-L1823 .
For example, protobuf would generated accessors like the following:
These would enable the above snippet to safely become something like:
Implementing this feature would enhance the developer experience and promote cleaner and safer code practices when working with Go autorest clients and azure-sdk-for-go.
The text was updated successfully, but these errors were encountered: