Helper func for OptNil types #1225
adrianlungu
started this conversation in
Ideas
Replies: 1 comment 1 reply
-
You can use generics to write a simple helper function to get a pointer: func AsPtr[
T any,
OptT interface {
Get() (T, bool)
},
](o OptT) *T {
v, ok := o.Get()
if !ok {
return nil
}
return &v
} Probably we should add an |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
While using ogen, I've often run into code where we check if an
OptNil
typeIsSet
and!IsNull
as the end field is a pointer, so I thought, why not have a different helperGet
function that returns&Value
ornil
?i.e. if we have
If value of
field
isnull
or missing altogether, it would return nil, otherwise it would return a pointer to""
or"value"
.Beta Was this translation helpful? Give feedback.
All reactions