-
Notifications
You must be signed in to change notification settings - Fork 19
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 pointer methods #24
Add pointer methods #24
Conversation
* Support “null” This change adds support for marshaling and unmarshaling “null” and reflecting that the value is not present, rather than relying on the “zero” value * Fix to specify {{ .VariableName }} * Add support for go modules * Fix go mod path * Update golden test * Remove go.mod per pull request
cmd/optional/main.go
Outdated
@@ -146,11 +146,28 @@ func New{{ .OutputName }}(v {{ .TypeName }}) {{ .OutputName }} { | |||
return {{ .OutputName }}{&v} | |||
} | |||
|
|||
// New{{ .OutputName }}FromPointer creates an optional.{{ .OutputName }} from a {{ .TypeName }} pointer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better late than never ;)
WDYT about FromPtr
/ ToPtr
? saves some keystrokes and I think is just as readable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it. I’ll push up and update
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lgtm! Thank you. Will create a new release in the morning
@markphelps any chance we could deploy today? I'd love to update our code to use this :) |
@mwielbut thanks again for the contribution!! I just created https://github.com/markphelps/optional/releases/tag/v0.11.0 with the change I'm going to be paying more attention to this library going forward and hope to do more regular releases. Thanks again! |
This PR adds some convenience methods to make it easier to convert between pointer and Optional representation of values.
Using the Optional package inevitably leads to lots of code like this when we need to convert back to pointers:
or the other way:
Two new functions are introduced, one to create an Optional given a pointer, and the other to return the embedded value as a pointer. Note, in both cases the value is copied over, the pointer is never passed directly as that would break the encapsulation.
With the two methods, the above examples become simple one-line statements: