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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions bool.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions byte.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions cmd/optional/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,28 @@ func New{{ .OutputName }}(v {{ .TypeName }}) {{ .OutputName }} {
return {{ .OutputName }}{&v}
}

// New{{ .OutputName }}FromPtr creates an optional.{{ .OutputName }} from a {{ .TypeName }} pointer.
func New{{ .OutputName }}FromPtr(v *{{ .TypeName }}) {{ .OutputName }} {
if v == nil {
return {{ .OutputName }}{}
}
return New{{ .OutputName }}(*v)
}

// Set sets the {{ .TypeName }} value.
func ({{ .VariableName }} *{{ .OutputName }}) Set(v {{ .TypeName }}) {
{{ .VariableName }}.value = &v
}

// ToPtr returns a *{{ .TypeName }} of the value or nil if not present.
func ({{ .VariableName }} {{ .OutputName }}) ToPtr() *{{ .TypeName }} {
if !{{ .VariableName }}.Present() {
return nil
}
v := *{{ .VariableName }}.value
return &v
}

// Get returns the {{ .TypeName }} value or an error if not present.
func ({{ .VariableName }} {{ .OutputName }}) Get() ({{ .TypeName }}, error) {
if !{{ .VariableName }}.Present() {
Expand Down
17 changes: 17 additions & 0 deletions cmd/optional/testdata/optional_bar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions cmd/optional/testdata/optional_foo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions cmd/optional/testdata/string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions complex128.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions complex64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions error.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions float32.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions float64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading