Skip to content
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

Merged

Conversation

mwielbut
Copy link
Contributor

@mwielbut mwielbut commented Sep 2, 2022

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:

a.FirstName.If(func(v string) {
	row.FirstName = &v
})

or the other way:

if r.FirstName != nil {
	a.FirstName = optional.NewString(*r.FirstName)
}

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:

row.FirstName = a.FirstName.ToPointer()
a.FirstName = optional.NewStringFromPointer(r.FirstName)

Openly, Inc and others added 7 commits December 7, 2018 16:10
* 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
@@ -146,11 +146,28 @@ func New{{ .OutputName }}(v {{ .TypeName }}) {{ .OutputName }} {
return {{ .OutputName }}{&v}
}

// New{{ .OutputName }}FromPointer creates an optional.{{ .OutputName }} from a {{ .TypeName }} pointer.
Copy link
Owner

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?

Copy link
Contributor Author

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated 👍

Copy link
Owner

@markphelps markphelps left a 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

@mwielbut
Copy link
Contributor Author

mwielbut commented Jul 7, 2023

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 :)

@markphelps markphelps merged commit e2abf76 into markphelps:master Jul 7, 2023
@markphelps
Copy link
Owner

@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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants