You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I need to frequently dive into the godoc to find out what type B is, and then new(types.Int64Value) and then set Value (or &types.Int64Value{Value: 0}) which can be quite verbose and ugly.
I figured that if Reset() wipes out a struct's values, and setting the pointer to nil removes the struct, there's surely scope for creating something the other way around -- going from nil (unset because you just ran new() on the parent) to an initialized struct without having to go to great lengths of boilerplate?
At the moment, if you run Reset() on a nil pointer, you get a nil pointer dereference panic. It would be good to test for nil first and new(type) it or something similar.
The closest thing I can see to making this better is upstream at golang/protobuf#414 but that doesn't seem to have gained much traction and still doesn't properly solve the nil pointer issue.
Thoughts / observations?
The text was updated successfully, but these errors were encountered:
Although thinking about it, the Reset() method passes in the pointer, which is nil, which you can't rewrite. I can't think of an elegant solution to that problem...
Sorry for the delay.
Someone recently reported an issue with the SetDefaults function, which I didn't know existed.
Does this sound like something you are looking for.
Maybe you could copy it and adjust it for your use case?
This ended up being far more complicated to implement in a friendly way for the consumers of the library I am writing than I ideally like, it's just easier to do new(type) or &type{}.
Thank you for the pointer though (forgive the pun!) -- this taught me something new at least!
Before I looked into this, I was wondering if the following was a common occurrence or not, but couldn't find too many people thinking the same way.
Let's say I have a generated type and instance of it:
I need to frequently dive into the godoc to find out what type
B
is, and thennew(types.Int64Value)
and then setValue
(or&types.Int64Value{Value: 0}
) which can be quite verbose and ugly.I figured that if
Reset()
wipes out a struct's values, and setting the pointer to nil removes the struct, there's surely scope for creating something the other way around -- going from nil (unset because you just ran new() on the parent) to an initialized struct without having to go to great lengths of boilerplate?At the moment, if you run
Reset()
on a nil pointer, you get a nil pointer dereference panic. It would be good to test for nil first andnew(type)
it or something similar.The closest thing I can see to making this better is upstream at golang/protobuf#414 but that doesn't seem to have gained much traction and still doesn't properly solve the nil pointer issue.
Thoughts / observations?
The text was updated successfully, but these errors were encountered: