From 177fbfa748b61484f57618d847fa002b953e14f8 Mon Sep 17 00:00:00 2001 From: "Openly, Inc" Date: Fri, 7 Dec 2018 14:37:24 +0000 Subject: [PATCH 1/5] Marshal empty as null (#9) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- bool.go | 11 ++++++++--- byte.go | 11 ++++++++--- cmd/optional/main.go | 9 +++++++-- cmd/optional/testdata/optional_bar.go | 11 ++++++++--- cmd/optional/testdata/optional_foo.go | 11 ++++++++--- cmd/optional/testdata/string.go | 11 ++++++++--- complex128.go | 11 ++++++++--- complex64.go | 11 ++++++++--- error.go | 11 ++++++++--- float32.go | 11 ++++++++--- float64.go | 11 ++++++++--- int.go | 11 ++++++++--- int16.go | 11 ++++++++--- int32.go | 11 ++++++++--- int64.go | 11 ++++++++--- int8.go | 11 ++++++++--- rune.go | 11 ++++++++--- string.go | 11 ++++++++--- uint.go | 11 ++++++++--- uint16.go | 11 ++++++++--- uint32.go | 11 ++++++++--- uint64.go | 11 ++++++++--- uint8.go | 11 ++++++++--- uintptr.go | 11 ++++++++--- 24 files changed, 191 insertions(+), 71 deletions(-) diff --git a/bool.go b/bool.go index b0d0432..fbc6f2b 100644 --- a/bool.go +++ b/bool.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:25.812586 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:53.447681 +0000 UTC package optional @@ -56,11 +56,16 @@ func (b Bool) MarshalJSON() ([]byte, error) { if b.Present() { return json.Marshal(b.value) } - var zero bool - return json.Marshal(zero) + return json.Marshal(nil) } func (b *Bool) UnmarshalJSON(data []byte) error { + + if string(data) == "null" { + b.value = nil + return nil + } + var value bool if err := json.Unmarshal(data, &value); err != nil { diff --git a/byte.go b/byte.go index d73e356..39409d8 100644 --- a/byte.go +++ b/byte.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:26.274563 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:53.79355 +0000 UTC package optional @@ -56,11 +56,16 @@ func (b Byte) MarshalJSON() ([]byte, error) { if b.Present() { return json.Marshal(b.value) } - var zero byte - return json.Marshal(zero) + return json.Marshal(nil) } func (b *Byte) UnmarshalJSON(data []byte) error { + + if string(data) == "null" { + b.value = nil + return nil + } + var value byte if err := json.Unmarshal(data, &value); err != nil { diff --git a/cmd/optional/main.go b/cmd/optional/main.go index 54b9dbe..0c8ce56 100644 --- a/cmd/optional/main.go +++ b/cmd/optional/main.go @@ -183,11 +183,16 @@ func ({{ .VariableName }} {{ .OutputName }}) MarshalJSON() ([]byte, error) { if {{ .VariableName }}.Present() { return json.Marshal({{ .VariableName }}.value) } - var zero {{ .TypeName }} - return json.Marshal(zero) + return json.Marshal(nil) } func ({{ .VariableName }} *{{ .OutputName }}) UnmarshalJSON(data []byte) error { + + if string(data) == "null" { + {{ .VariableName }}.value = nil + return nil + } + var value {{ .TypeName }} if err := json.Unmarshal(data, &value); err != nil { diff --git a/cmd/optional/testdata/optional_bar.go b/cmd/optional/testdata/optional_bar.go index 20ff0c8..01c9307 100644 --- a/cmd/optional/testdata/optional_bar.go +++ b/cmd/optional/testdata/optional_bar.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:54:14.621363 +0000 UTC +// This file was generated by robots at 2018-12-06 17:57:17.641444 +0000 UTC package bar @@ -56,11 +56,16 @@ func (o optionalBar) MarshalJSON() ([]byte, error) { if o.Present() { return json.Marshal(o.value) } - var zero bar - return json.Marshal(zero) + return json.Marshal(nil) } func (o *optionalBar) UnmarshalJSON(data []byte) error { + + if string(data) == "null" { + o.value = nil + return nil + } + var value bar if err := json.Unmarshal(data, &value); err != nil { diff --git a/cmd/optional/testdata/optional_foo.go b/cmd/optional/testdata/optional_foo.go index 667a74e..18a2c24 100644 --- a/cmd/optional/testdata/optional_foo.go +++ b/cmd/optional/testdata/optional_foo.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:54:14.61916 +0000 UTC +// This file was generated by robots at 2018-12-06 17:57:17.640425 +0000 UTC package foo @@ -56,11 +56,16 @@ func (o OptionalFoo) MarshalJSON() ([]byte, error) { if o.Present() { return json.Marshal(o.value) } - var zero Foo - return json.Marshal(zero) + return json.Marshal(nil) } func (o *OptionalFoo) UnmarshalJSON(data []byte) error { + + if string(data) == "null" { + o.value = nil + return nil + } + var value Foo if err := json.Unmarshal(data, &value); err != nil { diff --git a/cmd/optional/testdata/string.go b/cmd/optional/testdata/string.go index 44e2117..64074b9 100644 --- a/cmd/optional/testdata/string.go +++ b/cmd/optional/testdata/string.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:54:14.615858 +0000 UTC +// This file was generated by robots at 2018-12-06 17:57:17.639386 +0000 UTC package optional @@ -56,11 +56,16 @@ func (s String) MarshalJSON() ([]byte, error) { if s.Present() { return json.Marshal(s.value) } - var zero string - return json.Marshal(zero) + return json.Marshal(nil) } func (s *String) UnmarshalJSON(data []byte) error { + + if string(data) == "null" { + s.value = nil + return nil + } + var value string if err := json.Unmarshal(data, &value); err != nil { diff --git a/complex128.go b/complex128.go index e6954c3..b85dfbe 100644 --- a/complex128.go +++ b/complex128.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:26.694747 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:54.128811 +0000 UTC package optional @@ -56,11 +56,16 @@ func (c Complex128) MarshalJSON() ([]byte, error) { if c.Present() { return json.Marshal(c.value) } - var zero complex128 - return json.Marshal(zero) + return json.Marshal(nil) } func (c *Complex128) UnmarshalJSON(data []byte) error { + + if string(data) == "null" { + c.value = nil + return nil + } + var value complex128 if err := json.Unmarshal(data, &value); err != nil { diff --git a/complex64.go b/complex64.go index 4e011eb..04d5ee5 100644 --- a/complex64.go +++ b/complex64.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:27.082956 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:54.464548 +0000 UTC package optional @@ -56,11 +56,16 @@ func (c Complex64) MarshalJSON() ([]byte, error) { if c.Present() { return json.Marshal(c.value) } - var zero complex64 - return json.Marshal(zero) + return json.Marshal(nil) } func (c *Complex64) UnmarshalJSON(data []byte) error { + + if string(data) == "null" { + c.value = nil + return nil + } + var value complex64 if err := json.Unmarshal(data, &value); err != nil { diff --git a/error.go b/error.go index 41d3b7a..bc79ddc 100644 --- a/error.go +++ b/error.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:27.535329 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:54.804224 +0000 UTC package optional @@ -56,11 +56,16 @@ func (e Error) MarshalJSON() ([]byte, error) { if e.Present() { return json.Marshal(e.value) } - var zero error - return json.Marshal(zero) + return json.Marshal(nil) } func (e *Error) UnmarshalJSON(data []byte) error { + + if string(data) == "null" { + e.value = nil + return nil + } + var value error if err := json.Unmarshal(data, &value); err != nil { diff --git a/float32.go b/float32.go index eb78e2e..9f42c2c 100644 --- a/float32.go +++ b/float32.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:27.939662 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:55.1819 +0000 UTC package optional @@ -56,11 +56,16 @@ func (f Float32) MarshalJSON() ([]byte, error) { if f.Present() { return json.Marshal(f.value) } - var zero float32 - return json.Marshal(zero) + return json.Marshal(nil) } func (f *Float32) UnmarshalJSON(data []byte) error { + + if string(data) == "null" { + f.value = nil + return nil + } + var value float32 if err := json.Unmarshal(data, &value); err != nil { diff --git a/float64.go b/float64.go index 1125530..15966b6 100644 --- a/float64.go +++ b/float64.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:28.345245 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:55.521255 +0000 UTC package optional @@ -56,11 +56,16 @@ func (f Float64) MarshalJSON() ([]byte, error) { if f.Present() { return json.Marshal(f.value) } - var zero float64 - return json.Marshal(zero) + return json.Marshal(nil) } func (f *Float64) UnmarshalJSON(data []byte) error { + + if string(data) == "null" { + f.value = nil + return nil + } + var value float64 if err := json.Unmarshal(data, &value); err != nil { diff --git a/int.go b/int.go index f675e32..caead99 100644 --- a/int.go +++ b/int.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:28.766753 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:55.863991 +0000 UTC package optional @@ -56,11 +56,16 @@ func (i Int) MarshalJSON() ([]byte, error) { if i.Present() { return json.Marshal(i.value) } - var zero int - return json.Marshal(zero) + return json.Marshal(nil) } func (i *Int) UnmarshalJSON(data []byte) error { + + if string(data) == "null" { + i.value = nil + return nil + } + var value int if err := json.Unmarshal(data, &value); err != nil { diff --git a/int16.go b/int16.go index a6da247..9532996 100644 --- a/int16.go +++ b/int16.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:29.175694 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:56.212093 +0000 UTC package optional @@ -56,11 +56,16 @@ func (i Int16) MarshalJSON() ([]byte, error) { if i.Present() { return json.Marshal(i.value) } - var zero int16 - return json.Marshal(zero) + return json.Marshal(nil) } func (i *Int16) UnmarshalJSON(data []byte) error { + + if string(data) == "null" { + i.value = nil + return nil + } + var value int16 if err := json.Unmarshal(data, &value); err != nil { diff --git a/int32.go b/int32.go index f759790..07c212d 100644 --- a/int32.go +++ b/int32.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:29.586464 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:56.641482 +0000 UTC package optional @@ -56,11 +56,16 @@ func (i Int32) MarshalJSON() ([]byte, error) { if i.Present() { return json.Marshal(i.value) } - var zero int32 - return json.Marshal(zero) + return json.Marshal(nil) } func (i *Int32) UnmarshalJSON(data []byte) error { + + if string(data) == "null" { + i.value = nil + return nil + } + var value int32 if err := json.Unmarshal(data, &value); err != nil { diff --git a/int64.go b/int64.go index 0089cc3..7f8f01c 100644 --- a/int64.go +++ b/int64.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:30.027992 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:57.002648 +0000 UTC package optional @@ -56,11 +56,16 @@ func (i Int64) MarshalJSON() ([]byte, error) { if i.Present() { return json.Marshal(i.value) } - var zero int64 - return json.Marshal(zero) + return json.Marshal(nil) } func (i *Int64) UnmarshalJSON(data []byte) error { + + if string(data) == "null" { + i.value = nil + return nil + } + var value int64 if err := json.Unmarshal(data, &value); err != nil { diff --git a/int8.go b/int8.go index 4cdb349..182f814 100644 --- a/int8.go +++ b/int8.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:30.427813 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:57.555216 +0000 UTC package optional @@ -56,11 +56,16 @@ func (i Int8) MarshalJSON() ([]byte, error) { if i.Present() { return json.Marshal(i.value) } - var zero int8 - return json.Marshal(zero) + return json.Marshal(nil) } func (i *Int8) UnmarshalJSON(data []byte) error { + + if string(data) == "null" { + i.value = nil + return nil + } + var value int8 if err := json.Unmarshal(data, &value); err != nil { diff --git a/rune.go b/rune.go index a68653a..469de67 100644 --- a/rune.go +++ b/rune.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:30.867604 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:57.969673 +0000 UTC package optional @@ -56,11 +56,16 @@ func (r Rune) MarshalJSON() ([]byte, error) { if r.Present() { return json.Marshal(r.value) } - var zero rune - return json.Marshal(zero) + return json.Marshal(nil) } func (r *Rune) UnmarshalJSON(data []byte) error { + + if string(data) == "null" { + r.value = nil + return nil + } + var value rune if err := json.Unmarshal(data, &value); err != nil { diff --git a/string.go b/string.go index a346334..19e67c1 100644 --- a/string.go +++ b/string.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:31.29776 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:58.516631 +0000 UTC package optional @@ -56,11 +56,16 @@ func (s String) MarshalJSON() ([]byte, error) { if s.Present() { return json.Marshal(s.value) } - var zero string - return json.Marshal(zero) + return json.Marshal(nil) } func (s *String) UnmarshalJSON(data []byte) error { + + if string(data) == "null" { + s.value = nil + return nil + } + var value string if err := json.Unmarshal(data, &value); err != nil { diff --git a/uint.go b/uint.go index c9e610e..33e93e3 100644 --- a/uint.go +++ b/uint.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:31.699785 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:58.898667 +0000 UTC package optional @@ -56,11 +56,16 @@ func (u Uint) MarshalJSON() ([]byte, error) { if u.Present() { return json.Marshal(u.value) } - var zero uint - return json.Marshal(zero) + return json.Marshal(nil) } func (u *Uint) UnmarshalJSON(data []byte) error { + + if string(data) == "null" { + u.value = nil + return nil + } + var value uint if err := json.Unmarshal(data, &value); err != nil { diff --git a/uint16.go b/uint16.go index d7c7591..428ecbf 100644 --- a/uint16.go +++ b/uint16.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:32.12056 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:59.455534 +0000 UTC package optional @@ -56,11 +56,16 @@ func (u Uint16) MarshalJSON() ([]byte, error) { if u.Present() { return json.Marshal(u.value) } - var zero uint16 - return json.Marshal(zero) + return json.Marshal(nil) } func (u *Uint16) UnmarshalJSON(data []byte) error { + + if string(data) == "null" { + u.value = nil + return nil + } + var value uint16 if err := json.Unmarshal(data, &value); err != nil { diff --git a/uint32.go b/uint32.go index 125b416..5fa2ebf 100644 --- a/uint32.go +++ b/uint32.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:32.56791 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:59.856381 +0000 UTC package optional @@ -56,11 +56,16 @@ func (u Uint32) MarshalJSON() ([]byte, error) { if u.Present() { return json.Marshal(u.value) } - var zero uint32 - return json.Marshal(zero) + return json.Marshal(nil) } func (u *Uint32) UnmarshalJSON(data []byte) error { + + if string(data) == "null" { + u.value = nil + return nil + } + var value uint32 if err := json.Unmarshal(data, &value); err != nil { diff --git a/uint64.go b/uint64.go index 1252c06..f0d79a4 100644 --- a/uint64.go +++ b/uint64.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:32.98085 +0000 UTC +// This file was generated by robots at 2018-12-06 14:30:00.317311 +0000 UTC package optional @@ -56,11 +56,16 @@ func (u Uint64) MarshalJSON() ([]byte, error) { if u.Present() { return json.Marshal(u.value) } - var zero uint64 - return json.Marshal(zero) + return json.Marshal(nil) } func (u *Uint64) UnmarshalJSON(data []byte) error { + + if string(data) == "null" { + u.value = nil + return nil + } + var value uint64 if err := json.Unmarshal(data, &value); err != nil { diff --git a/uint8.go b/uint8.go index 920cb84..9b2be7f 100644 --- a/uint8.go +++ b/uint8.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:33.373985 +0000 UTC +// This file was generated by robots at 2018-12-06 14:30:00.699077 +0000 UTC package optional @@ -56,11 +56,16 @@ func (u Uint8) MarshalJSON() ([]byte, error) { if u.Present() { return json.Marshal(u.value) } - var zero uint8 - return json.Marshal(zero) + return json.Marshal(nil) } func (u *Uint8) UnmarshalJSON(data []byte) error { + + if string(data) == "null" { + u.value = nil + return nil + } + var value uint8 if err := json.Unmarshal(data, &value); err != nil { diff --git a/uintptr.go b/uintptr.go index b947661..af79fef 100644 --- a/uintptr.go +++ b/uintptr.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-11-14 00:52:33.811844 +0000 UTC +// This file was generated by robots at 2018-12-06 14:30:01.109101 +0000 UTC package optional @@ -56,11 +56,16 @@ func (u Uintptr) MarshalJSON() ([]byte, error) { if u.Present() { return json.Marshal(u.value) } - var zero uintptr - return json.Marshal(zero) + return json.Marshal(nil) } func (u *Uintptr) UnmarshalJSON(data []byte) error { + + if string(data) == "null" { + u.value = nil + return nil + } + var value uintptr if err := json.Unmarshal(data, &value); err != nil { From a341c6e9bd998794f4e6c8d966f0a7afd8061eee Mon Sep 17 00:00:00 2001 From: Mark Phelps Date: Fri, 7 Dec 2018 08:19:31 -0600 Subject: [PATCH 2/5] Add example test --- example_test.go | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/example_test.go b/example_test.go index b880945..4c320cf 100644 --- a/example_test.go +++ b/example_test.go @@ -99,7 +99,8 @@ func Example_set() { func Example_marshalJSON() { type example struct { - Field *optional.String `json:"field,omitempty"` + Field *optional.String `json:"field,omitempty"` + FieldTwo *optional.String `json:"field_two"` } var values = []optional.String{ @@ -110,28 +111,35 @@ func Example_marshalJSON() { for _, v := range values { out, _ := json.Marshal(&example{ - Field: &v, + Field: &v, + FieldTwo: &v, }) fmt.Println(string(out)) } + out, _ := json.Marshal(&example{}) + fmt.Println(string(out)) + // Output: - // {"field":"foo"} - // {"field":""} - // {"field":"bar"} + // {"field":"foo","field_two":"foo"} + // {"field":"","field_two":""} + // {"field":"bar","field_two":"bar"} + // {"field_two":null} } func Example_unmarshalJSON() { var values = []string{ - `{"field":"foo"}`, - `{"field":""}`, - `{"field":"bar"}`, + `{"field":"foo","field_two":"foo"}`, + `{"field":"","field_two":""}`, + `{"field":"null","field_two":"null"}`, + `{"field":"bar","field_two":"bar"}`, "{}", } for _, v := range values { var o = &struct { - Field optional.String `json:"field,omitempty"` + Field optional.String `json:"field,omitempty"` + FieldTwo optional.String `json:"field_two"` }{} if err := json.Unmarshal([]byte(v), o); err != nil { @@ -141,10 +149,18 @@ func Example_unmarshalJSON() { o.Field.If(func(s string) { fmt.Println(s) }) + + o.FieldTwo.If(func(s string) { + fmt.Println(s) + }) } // Output: // foo + // foo + // + // // bar + // bar } From 515f25fcf244b0198b29a28d0d56b5d3fa7ece60 Mon Sep 17 00:00:00 2001 From: Mark Phelps Date: Fri, 7 Dec 2018 09:15:38 -0600 Subject: [PATCH 3/5] Update README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index bbd45ef..12aef16 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,8 @@ In Go, variables declared without an explicit initial value are given their zero ## Marshalling/Unmarshalling JSON +**Note:** v0.6.0 introduces a potential breaking change to anyone depending on marshalling non-present values to their zero values instead of null. See: [#9](https://github.com/markphelps/optional/pull/9) for more context. + Option types also marshal to/from JSON as you would expect: ### Marshalling @@ -165,3 +167,7 @@ See [example_test.go](example_test.go) and the [documentation](http://godoc.org/ 1. Commit your changes (`git commit -am 'Add some feature'`) 1. Push to the branch (`git push origin my-new-feature`) 1. Create a new Pull Request + +### Golden Files + +If changing the API you may need to update the [golden files](https://medium.com/soon-london/testing-with-golden-files-in-go-7fccc71c43d3) for your tests to pass by running `go test ./cmd/optional/... -update`. From 1cfacce06768d82696c94da95eff0e2a1c270ed6 Mon Sep 17 00:00:00 2001 From: Matt Wielbut Date: Fri, 2 Sep 2022 08:36:01 -0400 Subject: [PATCH 4/5] Adds methods to more easily go from pointer to optional and reverse --- bool.go | 20 +++++++++++++-- byte.go | 20 +++++++++++++-- cmd/optional/main.go | 17 ++++++++++++ cmd/optional/testdata/optional_bar.go | 17 ++++++++++++ cmd/optional/testdata/optional_foo.go | 17 ++++++++++++ cmd/optional/testdata/string.go | 17 ++++++++++++ complex128.go | 20 +++++++++++++-- complex64.go | 20 +++++++++++++-- error.go | 20 +++++++++++++-- float32.go | 20 +++++++++++++-- float64.go | 20 +++++++++++++-- int.go | 20 +++++++++++++-- int16.go | 20 +++++++++++++-- int32.go | 20 +++++++++++++-- int64.go | 20 +++++++++++++-- int8.go | 20 +++++++++++++-- int_test.go | 22 ++++++++++++++++ rune.go | 20 +++++++++++++-- string.go | 20 +++++++++++++-- string_test.go | 37 +++++++++++++++++++++++++++ uint.go | 20 +++++++++++++-- uint16.go | 20 +++++++++++++-- uint32.go | 20 +++++++++++++-- uint64.go | 20 +++++++++++++-- uint8.go | 20 +++++++++++++-- uintptr.go | 20 +++++++++++++-- 26 files changed, 487 insertions(+), 40 deletions(-) diff --git a/bool.go b/bool.go index 880ffa2..392e7a7 100644 --- a/bool.go +++ b/bool.go @@ -1,5 +1,4 @@ -// Code generated by go generate -// This file was generated by robots at 2021-05-04 14:21:52.312823 +0000 UTC +// Code generated by 'go generate' package optional @@ -18,11 +17,28 @@ func NewBool(v bool) Bool { return Bool{&v} } +// NewBoolFromPointer creates an optional.Bool from a bool pointer. +func NewBoolFromPointer(v *bool) Bool { + if v == nil { + return Bool{} + } + return NewBool(*v) +} + // Set sets the bool value. func (b *Bool) Set(v bool) { b.value = &v } +// ToPointer returns a *bool of the value or nil if not present. +func (b Bool) ToPointer() *bool { + if !b.Present() { + return nil + } + v := *b.value + return &v +} + // Get returns the bool value or an error if not present. func (b Bool) Get() (bool, error) { if !b.Present() { diff --git a/byte.go b/byte.go index d4478d7..f4758c9 100644 --- a/byte.go +++ b/byte.go @@ -1,5 +1,4 @@ -// Code generated by go generate -// This file was generated by robots at 2021-05-04 14:21:52.752446 +0000 UTC +// Code generated by 'go generate' package optional @@ -18,11 +17,28 @@ func NewByte(v byte) Byte { return Byte{&v} } +// NewByteFromPointer creates an optional.Byte from a byte pointer. +func NewByteFromPointer(v *byte) Byte { + if v == nil { + return Byte{} + } + return NewByte(*v) +} + // Set sets the byte value. func (b *Byte) Set(v byte) { b.value = &v } +// ToPointer returns a *byte of the value or nil if not present. +func (b Byte) ToPointer() *byte { + if !b.Present() { + return nil + } + v := *b.value + return &v +} + // Get returns the byte value or an error if not present. func (b Byte) Get() (byte, error) { if !b.Present() { diff --git a/cmd/optional/main.go b/cmd/optional/main.go index 1eceb29..186e930 100644 --- a/cmd/optional/main.go +++ b/cmd/optional/main.go @@ -146,11 +146,28 @@ func New{{ .OutputName }}(v {{ .TypeName }}) {{ .OutputName }} { return {{ .OutputName }}{&v} } +// New{{ .OutputName }}FromPointer creates an optional.{{ .OutputName }} from a {{ .TypeName }} pointer. +func New{{ .OutputName }}FromPointer(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 } +// ToPointer returns a *{{ .TypeName }} of the value or nil if not present. +func ({{ .VariableName }} {{ .OutputName }}) ToPointer() *{{ .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() { diff --git a/cmd/optional/testdata/optional_bar.go b/cmd/optional/testdata/optional_bar.go index 0f970b2..53ad73b 100644 --- a/cmd/optional/testdata/optional_bar.go +++ b/cmd/optional/testdata/optional_bar.go @@ -17,11 +17,28 @@ func NewoptionalBar(v bar) optionalBar { return optionalBar{&v} } +// NewoptionalBarFromPointer creates an optional.optionalBar from a bar pointer. +func NewoptionalBarFromPointer(v *bar) optionalBar { + if v == nil { + return optionalBar{} + } + return NewoptionalBar(*v) +} + // Set sets the bar value. func (o *optionalBar) Set(v bar) { o.value = &v } +// ToPointer returns a *bar of the value or nil if not present. +func (o optionalBar) ToPointer() *bar { + if !o.Present() { + return nil + } + v := *o.value + return &v +} + // Get returns the bar value or an error if not present. func (o optionalBar) Get() (bar, error) { if !o.Present() { diff --git a/cmd/optional/testdata/optional_foo.go b/cmd/optional/testdata/optional_foo.go index c3639c6..0450786 100644 --- a/cmd/optional/testdata/optional_foo.go +++ b/cmd/optional/testdata/optional_foo.go @@ -17,11 +17,28 @@ func NewOptionalFoo(v Foo) OptionalFoo { return OptionalFoo{&v} } +// NewOptionalFooFromPointer creates an optional.OptionalFoo from a Foo pointer. +func NewOptionalFooFromPointer(v *Foo) OptionalFoo { + if v == nil { + return OptionalFoo{} + } + return NewOptionalFoo(*v) +} + // Set sets the Foo value. func (o *OptionalFoo) Set(v Foo) { o.value = &v } +// ToPointer returns a *Foo of the value or nil if not present. +func (o OptionalFoo) ToPointer() *Foo { + if !o.Present() { + return nil + } + v := *o.value + return &v +} + // Get returns the Foo value or an error if not present. func (o OptionalFoo) Get() (Foo, error) { if !o.Present() { diff --git a/cmd/optional/testdata/string.go b/cmd/optional/testdata/string.go index 4dbde1e..2971379 100644 --- a/cmd/optional/testdata/string.go +++ b/cmd/optional/testdata/string.go @@ -17,11 +17,28 @@ func NewString(v string) String { return String{&v} } +// NewStringFromPointer creates an optional.String from a string pointer. +func NewStringFromPointer(v *string) String { + if v == nil { + return String{} + } + return NewString(*v) +} + // Set sets the string value. func (s *String) Set(v string) { s.value = &v } +// ToPointer returns a *string of the value or nil if not present. +func (s String) ToPointer() *string { + if !s.Present() { + return nil + } + v := *s.value + return &v +} + // Get returns the string value or an error if not present. func (s String) Get() (string, error) { if !s.Present() { diff --git a/complex128.go b/complex128.go index 099a98b..5fd2ed0 100644 --- a/complex128.go +++ b/complex128.go @@ -1,5 +1,4 @@ -// Code generated by go generate -// This file was generated by robots at 2021-05-04 14:21:53.141535 +0000 UTC +// Code generated by 'go generate' package optional @@ -17,11 +16,28 @@ func NewComplex128(v complex128) Complex128 { return Complex128{&v} } +// NewComplex128FromPointer creates an optional.Complex128 from a complex128 pointer. +func NewComplex128FromPointer(v *complex128) Complex128 { + if v == nil { + return Complex128{} + } + return NewComplex128(*v) +} + // Set sets the complex128 value. func (c *Complex128) Set(v complex128) { c.value = &v } +// ToPointer returns a *complex128 of the value or nil if not present. +func (c Complex128) ToPointer() *complex128 { + if !c.Present() { + return nil + } + v := *c.value + return &v +} + // Get returns the complex128 value or an error if not present. func (c Complex128) Get() (complex128, error) { if !c.Present() { diff --git a/complex64.go b/complex64.go index d1298b4..74e7ea7 100644 --- a/complex64.go +++ b/complex64.go @@ -1,5 +1,4 @@ -// Code generated by go generate -// This file was generated by robots at 2021-05-04 14:21:53.525719 +0000 UTC +// Code generated by 'go generate' package optional @@ -17,11 +16,28 @@ func NewComplex64(v complex64) Complex64 { return Complex64{&v} } +// NewComplex64FromPointer creates an optional.Complex64 from a complex64 pointer. +func NewComplex64FromPointer(v *complex64) Complex64 { + if v == nil { + return Complex64{} + } + return NewComplex64(*v) +} + // Set sets the complex64 value. func (c *Complex64) Set(v complex64) { c.value = &v } +// ToPointer returns a *complex64 of the value or nil if not present. +func (c Complex64) ToPointer() *complex64 { + if !c.Present() { + return nil + } + v := *c.value + return &v +} + // Get returns the complex64 value or an error if not present. func (c Complex64) Get() (complex64, error) { if !c.Present() { diff --git a/error.go b/error.go index d90d7bc..8ee77d4 100644 --- a/error.go +++ b/error.go @@ -1,5 +1,4 @@ -// Code generated by go generate -// This file was generated by robots at 2021-05-04 14:21:53.917031 +0000 UTC +// Code generated by 'go generate' package optional @@ -18,11 +17,28 @@ func NewError(v error) Error { return Error{&v} } +// NewErrorFromPointer creates an optional.Error from a error pointer. +func NewErrorFromPointer(v *error) Error { + if v == nil { + return Error{} + } + return NewError(*v) +} + // Set sets the error value. func (e *Error) Set(v error) { e.value = &v } +// ToPointer returns a *error of the value or nil if not present. +func (e Error) ToPointer() *error { + if !e.Present() { + return nil + } + v := *e.value + return &v +} + // Get returns the error value or an error if not present. func (e Error) Get() (error, error) { if !e.Present() { diff --git a/float32.go b/float32.go index c0545d8..de6cbd6 100644 --- a/float32.go +++ b/float32.go @@ -1,5 +1,4 @@ -// Code generated by go generate -// This file was generated by robots at 2021-05-04 14:21:54.315544 +0000 UTC +// Code generated by 'go generate' package optional @@ -18,11 +17,28 @@ func NewFloat32(v float32) Float32 { return Float32{&v} } +// NewFloat32FromPointer creates an optional.Float32 from a float32 pointer. +func NewFloat32FromPointer(v *float32) Float32 { + if v == nil { + return Float32{} + } + return NewFloat32(*v) +} + // Set sets the float32 value. func (f *Float32) Set(v float32) { f.value = &v } +// ToPointer returns a *float32 of the value or nil if not present. +func (f Float32) ToPointer() *float32 { + if !f.Present() { + return nil + } + v := *f.value + return &v +} + // Get returns the float32 value or an error if not present. func (f Float32) Get() (float32, error) { if !f.Present() { diff --git a/float64.go b/float64.go index 17fd2c8..bba0ae9 100644 --- a/float64.go +++ b/float64.go @@ -1,5 +1,4 @@ -// Code generated by go generate -// This file was generated by robots at 2021-05-04 14:21:54.698931 +0000 UTC +// Code generated by 'go generate' package optional @@ -18,11 +17,28 @@ func NewFloat64(v float64) Float64 { return Float64{&v} } +// NewFloat64FromPointer creates an optional.Float64 from a float64 pointer. +func NewFloat64FromPointer(v *float64) Float64 { + if v == nil { + return Float64{} + } + return NewFloat64(*v) +} + // Set sets the float64 value. func (f *Float64) Set(v float64) { f.value = &v } +// ToPointer returns a *float64 of the value or nil if not present. +func (f Float64) ToPointer() *float64 { + if !f.Present() { + return nil + } + v := *f.value + return &v +} + // Get returns the float64 value or an error if not present. func (f Float64) Get() (float64, error) { if !f.Present() { diff --git a/int.go b/int.go index 0eca2cc..1a3ff65 100644 --- a/int.go +++ b/int.go @@ -1,5 +1,4 @@ -// Code generated by go generate -// This file was generated by robots at 2021-05-04 14:21:55.077372 +0000 UTC +// Code generated by 'go generate' package optional @@ -18,11 +17,28 @@ func NewInt(v int) Int { return Int{&v} } +// NewIntFromPointer creates an optional.Int from a int pointer. +func NewIntFromPointer(v *int) Int { + if v == nil { + return Int{} + } + return NewInt(*v) +} + // Set sets the int value. func (i *Int) Set(v int) { i.value = &v } +// ToPointer returns a *int of the value or nil if not present. +func (i Int) ToPointer() *int { + if !i.Present() { + return nil + } + v := *i.value + return &v +} + // Get returns the int value or an error if not present. func (i Int) Get() (int, error) { if !i.Present() { diff --git a/int16.go b/int16.go index e8a0f89..a2e56ac 100644 --- a/int16.go +++ b/int16.go @@ -1,5 +1,4 @@ -// Code generated by go generate -// This file was generated by robots at 2021-05-04 14:21:55.464815 +0000 UTC +// Code generated by 'go generate' package optional @@ -18,11 +17,28 @@ func NewInt16(v int16) Int16 { return Int16{&v} } +// NewInt16FromPointer creates an optional.Int16 from a int16 pointer. +func NewInt16FromPointer(v *int16) Int16 { + if v == nil { + return Int16{} + } + return NewInt16(*v) +} + // Set sets the int16 value. func (i *Int16) Set(v int16) { i.value = &v } +// ToPointer returns a *int16 of the value or nil if not present. +func (i Int16) ToPointer() *int16 { + if !i.Present() { + return nil + } + v := *i.value + return &v +} + // Get returns the int16 value or an error if not present. func (i Int16) Get() (int16, error) { if !i.Present() { diff --git a/int32.go b/int32.go index df62918..9f1a4ce 100644 --- a/int32.go +++ b/int32.go @@ -1,5 +1,4 @@ -// Code generated by go generate -// This file was generated by robots at 2021-05-04 14:21:55.878008 +0000 UTC +// Code generated by 'go generate' package optional @@ -18,11 +17,28 @@ func NewInt32(v int32) Int32 { return Int32{&v} } +// NewInt32FromPointer creates an optional.Int32 from a int32 pointer. +func NewInt32FromPointer(v *int32) Int32 { + if v == nil { + return Int32{} + } + return NewInt32(*v) +} + // Set sets the int32 value. func (i *Int32) Set(v int32) { i.value = &v } +// ToPointer returns a *int32 of the value or nil if not present. +func (i Int32) ToPointer() *int32 { + if !i.Present() { + return nil + } + v := *i.value + return &v +} + // Get returns the int32 value or an error if not present. func (i Int32) Get() (int32, error) { if !i.Present() { diff --git a/int64.go b/int64.go index 1f6a0e0..72132bc 100644 --- a/int64.go +++ b/int64.go @@ -1,5 +1,4 @@ -// Code generated by go generate -// This file was generated by robots at 2021-05-04 14:21:56.588737 +0000 UTC +// Code generated by 'go generate' package optional @@ -18,11 +17,28 @@ func NewInt64(v int64) Int64 { return Int64{&v} } +// NewInt64FromPointer creates an optional.Int64 from a int64 pointer. +func NewInt64FromPointer(v *int64) Int64 { + if v == nil { + return Int64{} + } + return NewInt64(*v) +} + // Set sets the int64 value. func (i *Int64) Set(v int64) { i.value = &v } +// ToPointer returns a *int64 of the value or nil if not present. +func (i Int64) ToPointer() *int64 { + if !i.Present() { + return nil + } + v := *i.value + return &v +} + // Get returns the int64 value or an error if not present. func (i Int64) Get() (int64, error) { if !i.Present() { diff --git a/int8.go b/int8.go index db58a8c..cd31504 100644 --- a/int8.go +++ b/int8.go @@ -1,5 +1,4 @@ -// Code generated by go generate -// This file was generated by robots at 2021-05-04 14:21:57.374933 +0000 UTC +// Code generated by 'go generate' package optional @@ -18,11 +17,28 @@ func NewInt8(v int8) Int8 { return Int8{&v} } +// NewInt8FromPointer creates an optional.Int8 from a int8 pointer. +func NewInt8FromPointer(v *int8) Int8 { + if v == nil { + return Int8{} + } + return NewInt8(*v) +} + // Set sets the int8 value. func (i *Int8) Set(v int8) { i.value = &v } +// ToPointer returns a *int8 of the value or nil if not present. +func (i Int8) ToPointer() *int8 { + if !i.Present() { + return nil + } + v := *i.value + return &v +} + // Get returns the int8 value or an error if not present. func (i Int8) Get() (int8, error) { if !i.Present() { diff --git a/int_test.go b/int_test.go index efe5fc6..8adcb6c 100644 --- a/int_test.go +++ b/int_test.go @@ -7,6 +7,28 @@ import ( "github.com/stretchr/testify/assert" ) +func TestInt_NewIntFromPointer_NotNil(t *testing.T) { + i := 42 + p := &i + o := NewIntFromPointer(p) + + v, err := o.Get() + assert.True(t, o.Present()) + assert.NoError(t, err) + assert.Equal(t, 42, v) + assert.True(t, p != o.value, "expect return pointer to be different from embedded pointer") +} + +func TestInt_NewIntFromPointer_Nil(t *testing.T) { + o := NewIntFromPointer(nil) + var zero int + + v, err := o.Get() + assert.False(t, o.Present()) + assert.Error(t, err) + assert.Equal(t, zero, v) +} + func TestInt_Get_Present(t *testing.T) { o := NewInt(42) diff --git a/rune.go b/rune.go index 6d93bb6..d9a1998 100644 --- a/rune.go +++ b/rune.go @@ -1,5 +1,4 @@ -// Code generated by go generate -// This file was generated by robots at 2021-05-04 14:21:57.770327 +0000 UTC +// Code generated by 'go generate' package optional @@ -18,11 +17,28 @@ func NewRune(v rune) Rune { return Rune{&v} } +// NewRuneFromPointer creates an optional.Rune from a rune pointer. +func NewRuneFromPointer(v *rune) Rune { + if v == nil { + return Rune{} + } + return NewRune(*v) +} + // Set sets the rune value. func (r *Rune) Set(v rune) { r.value = &v } +// ToPointer returns a *rune of the value or nil if not present. +func (r Rune) ToPointer() *rune { + if !r.Present() { + return nil + } + v := *r.value + return &v +} + // Get returns the rune value or an error if not present. func (r Rune) Get() (rune, error) { if !r.Present() { diff --git a/string.go b/string.go index 2b107ad..2971379 100644 --- a/string.go +++ b/string.go @@ -1,5 +1,4 @@ -// Code generated by go generate -// This file was generated by robots at 2021-05-04 14:21:58.455478 +0000 UTC +// Code generated by 'go generate' package optional @@ -18,11 +17,28 @@ func NewString(v string) String { return String{&v} } +// NewStringFromPointer creates an optional.String from a string pointer. +func NewStringFromPointer(v *string) String { + if v == nil { + return String{} + } + return NewString(*v) +} + // Set sets the string value. func (s *String) Set(v string) { s.value = &v } +// ToPointer returns a *string of the value or nil if not present. +func (s String) ToPointer() *string { + if !s.Present() { + return nil + } + v := *s.value + return &v +} + // Get returns the string value or an error if not present. func (s String) Get() (string, error) { if !s.Present() { diff --git a/string_test.go b/string_test.go index 52f19e8..e8a9e69 100644 --- a/string_test.go +++ b/string_test.go @@ -7,6 +7,27 @@ import ( "github.com/stretchr/testify/assert" ) +func TestString_NewStringFromPointer_NotNil(t *testing.T) { + s := "foo" + p := &s + o := NewStringFromPointer(p) + + v, err := o.Get() + assert.True(t, o.Present()) + assert.NoError(t, err) + assert.Equal(t, "foo", v) + assert.True(t, p != o.value, "expect return pointer to be different from embedded pointer") +} + +func TestString_NewStringFromPointer_Nil(t *testing.T) { + o := NewStringFromPointer(nil) + + v, err := o.Get() + assert.False(t, o.Present()) + assert.Error(t, err) + assert.Equal(t, "", v) +} + func TestString_Get_Present(t *testing.T) { o := NewString("foo") @@ -79,6 +100,22 @@ func TestString_If_NotPresent(t *testing.T) { assert.False(t, canary) } +func TestString_ToPointer_NotNil(t *testing.T) { + o := NewString("foo") + + p := o.ToPointer() + assert.NotNil(t, p) + assert.Equal(t, "foo", *p) + assert.True(t, p != o.value, "expect return pointer to be different from embedded pointer") +} + +func TestString_ToPointer_Nil(t *testing.T) { + o := String{} + + p := o.ToPointer() + assert.Nil(t, p) +} + func TestString_MarshalJSON(t *testing.T) { type fields struct { WithValue String diff --git a/uint.go b/uint.go index 96223a8..8bfaab7 100644 --- a/uint.go +++ b/uint.go @@ -1,5 +1,4 @@ -// Code generated by go generate -// This file was generated by robots at 2021-05-04 14:21:58.844644 +0000 UTC +// Code generated by 'go generate' package optional @@ -18,11 +17,28 @@ func NewUint(v uint) Uint { return Uint{&v} } +// NewUintFromPointer creates an optional.Uint from a uint pointer. +func NewUintFromPointer(v *uint) Uint { + if v == nil { + return Uint{} + } + return NewUint(*v) +} + // Set sets the uint value. func (u *Uint) Set(v uint) { u.value = &v } +// ToPointer returns a *uint of the value or nil if not present. +func (u Uint) ToPointer() *uint { + if !u.Present() { + return nil + } + v := *u.value + return &v +} + // Get returns the uint value or an error if not present. func (u Uint) Get() (uint, error) { if !u.Present() { diff --git a/uint16.go b/uint16.go index d79e595..7108652 100644 --- a/uint16.go +++ b/uint16.go @@ -1,5 +1,4 @@ -// Code generated by go generate -// This file was generated by robots at 2021-05-04 14:21:59.22585 +0000 UTC +// Code generated by 'go generate' package optional @@ -18,11 +17,28 @@ func NewUint16(v uint16) Uint16 { return Uint16{&v} } +// NewUint16FromPointer creates an optional.Uint16 from a uint16 pointer. +func NewUint16FromPointer(v *uint16) Uint16 { + if v == nil { + return Uint16{} + } + return NewUint16(*v) +} + // Set sets the uint16 value. func (u *Uint16) Set(v uint16) { u.value = &v } +// ToPointer returns a *uint16 of the value or nil if not present. +func (u Uint16) ToPointer() *uint16 { + if !u.Present() { + return nil + } + v := *u.value + return &v +} + // Get returns the uint16 value or an error if not present. func (u Uint16) Get() (uint16, error) { if !u.Present() { diff --git a/uint32.go b/uint32.go index cefc35e..6c65761 100644 --- a/uint32.go +++ b/uint32.go @@ -1,5 +1,4 @@ -// Code generated by go generate -// This file was generated by robots at 2021-05-04 14:21:59.609003 +0000 UTC +// Code generated by 'go generate' package optional @@ -18,11 +17,28 @@ func NewUint32(v uint32) Uint32 { return Uint32{&v} } +// NewUint32FromPointer creates an optional.Uint32 from a uint32 pointer. +func NewUint32FromPointer(v *uint32) Uint32 { + if v == nil { + return Uint32{} + } + return NewUint32(*v) +} + // Set sets the uint32 value. func (u *Uint32) Set(v uint32) { u.value = &v } +// ToPointer returns a *uint32 of the value or nil if not present. +func (u Uint32) ToPointer() *uint32 { + if !u.Present() { + return nil + } + v := *u.value + return &v +} + // Get returns the uint32 value or an error if not present. func (u Uint32) Get() (uint32, error) { if !u.Present() { diff --git a/uint64.go b/uint64.go index db7a98d..9a8fee2 100644 --- a/uint64.go +++ b/uint64.go @@ -1,5 +1,4 @@ -// Code generated by go generate -// This file was generated by robots at 2021-05-04 14:21:59.988145 +0000 UTC +// Code generated by 'go generate' package optional @@ -18,11 +17,28 @@ func NewUint64(v uint64) Uint64 { return Uint64{&v} } +// NewUint64FromPointer creates an optional.Uint64 from a uint64 pointer. +func NewUint64FromPointer(v *uint64) Uint64 { + if v == nil { + return Uint64{} + } + return NewUint64(*v) +} + // Set sets the uint64 value. func (u *Uint64) Set(v uint64) { u.value = &v } +// ToPointer returns a *uint64 of the value or nil if not present. +func (u Uint64) ToPointer() *uint64 { + if !u.Present() { + return nil + } + v := *u.value + return &v +} + // Get returns the uint64 value or an error if not present. func (u Uint64) Get() (uint64, error) { if !u.Present() { diff --git a/uint8.go b/uint8.go index 8a281f8..6f58460 100644 --- a/uint8.go +++ b/uint8.go @@ -1,5 +1,4 @@ -// Code generated by go generate -// This file was generated by robots at 2021-05-04 14:22:00.389367 +0000 UTC +// Code generated by 'go generate' package optional @@ -18,11 +17,28 @@ func NewUint8(v uint8) Uint8 { return Uint8{&v} } +// NewUint8FromPointer creates an optional.Uint8 from a uint8 pointer. +func NewUint8FromPointer(v *uint8) Uint8 { + if v == nil { + return Uint8{} + } + return NewUint8(*v) +} + // Set sets the uint8 value. func (u *Uint8) Set(v uint8) { u.value = &v } +// ToPointer returns a *uint8 of the value or nil if not present. +func (u Uint8) ToPointer() *uint8 { + if !u.Present() { + return nil + } + v := *u.value + return &v +} + // Get returns the uint8 value or an error if not present. func (u Uint8) Get() (uint8, error) { if !u.Present() { diff --git a/uintptr.go b/uintptr.go index deea7aa..09c9a9d 100644 --- a/uintptr.go +++ b/uintptr.go @@ -1,5 +1,4 @@ -// Code generated by go generate -// This file was generated by robots at 2021-05-04 14:22:00.780925 +0000 UTC +// Code generated by 'go generate' package optional @@ -18,11 +17,28 @@ func NewUintptr(v uintptr) Uintptr { return Uintptr{&v} } +// NewUintptrFromPointer creates an optional.Uintptr from a uintptr pointer. +func NewUintptrFromPointer(v *uintptr) Uintptr { + if v == nil { + return Uintptr{} + } + return NewUintptr(*v) +} + // Set sets the uintptr value. func (u *Uintptr) Set(v uintptr) { u.value = &v } +// ToPointer returns a *uintptr of the value or nil if not present. +func (u Uintptr) ToPointer() *uintptr { + if !u.Present() { + return nil + } + v := *u.value + return &v +} + // Get returns the uintptr value or an error if not present. func (u Uintptr) Get() (uintptr, error) { if !u.Present() { From ce559478b0d483fad4c3a67e648b97e54cd2bb1f Mon Sep 17 00:00:00 2001 From: Matt Wielbut Date: Wed, 5 Jul 2023 17:45:47 -0500 Subject: [PATCH 5/5] Adds methods to more easily go from pointer to optional and reverse --- bool.go | 8 ++++---- byte.go | 8 ++++---- cmd/optional/main.go | 8 ++++---- cmd/optional/testdata/optional_bar.go | 8 ++++---- cmd/optional/testdata/optional_foo.go | 8 ++++---- cmd/optional/testdata/string.go | 8 ++++---- complex128.go | 8 ++++---- complex64.go | 8 ++++---- error.go | 8 ++++---- float32.go | 8 ++++---- float64.go | 8 ++++---- int.go | 8 ++++---- int16.go | 8 ++++---- int32.go | 8 ++++---- int64.go | 8 ++++---- int8.go | 8 ++++---- int_test.go | 8 ++++---- rune.go | 8 ++++---- string.go | 8 ++++---- string_test.go | 16 ++++++++-------- uint.go | 8 ++++---- uint16.go | 8 ++++---- uint32.go | 8 ++++---- uint64.go | 8 ++++---- uint8.go | 8 ++++---- uintptr.go | 8 ++++---- 26 files changed, 108 insertions(+), 108 deletions(-) diff --git a/bool.go b/bool.go index 392e7a7..66bd13e 100644 --- a/bool.go +++ b/bool.go @@ -17,8 +17,8 @@ func NewBool(v bool) Bool { return Bool{&v} } -// NewBoolFromPointer creates an optional.Bool from a bool pointer. -func NewBoolFromPointer(v *bool) Bool { +// NewBoolFromPtr creates an optional.Bool from a bool pointer. +func NewBoolFromPtr(v *bool) Bool { if v == nil { return Bool{} } @@ -30,8 +30,8 @@ func (b *Bool) Set(v bool) { b.value = &v } -// ToPointer returns a *bool of the value or nil if not present. -func (b Bool) ToPointer() *bool { +// ToPtr returns a *bool of the value or nil if not present. +func (b Bool) ToPtr() *bool { if !b.Present() { return nil } diff --git a/byte.go b/byte.go index f4758c9..bc199e5 100644 --- a/byte.go +++ b/byte.go @@ -17,8 +17,8 @@ func NewByte(v byte) Byte { return Byte{&v} } -// NewByteFromPointer creates an optional.Byte from a byte pointer. -func NewByteFromPointer(v *byte) Byte { +// NewByteFromPtr creates an optional.Byte from a byte pointer. +func NewByteFromPtr(v *byte) Byte { if v == nil { return Byte{} } @@ -30,8 +30,8 @@ func (b *Byte) Set(v byte) { b.value = &v } -// ToPointer returns a *byte of the value or nil if not present. -func (b Byte) ToPointer() *byte { +// ToPtr returns a *byte of the value or nil if not present. +func (b Byte) ToPtr() *byte { if !b.Present() { return nil } diff --git a/cmd/optional/main.go b/cmd/optional/main.go index 186e930..1784574 100644 --- a/cmd/optional/main.go +++ b/cmd/optional/main.go @@ -146,8 +146,8 @@ func New{{ .OutputName }}(v {{ .TypeName }}) {{ .OutputName }} { return {{ .OutputName }}{&v} } -// New{{ .OutputName }}FromPointer creates an optional.{{ .OutputName }} from a {{ .TypeName }} pointer. -func New{{ .OutputName }}FromPointer(v *{{ .TypeName }}) {{ .OutputName }} { +// New{{ .OutputName }}FromPtr creates an optional.{{ .OutputName }} from a {{ .TypeName }} pointer. +func New{{ .OutputName }}FromPtr(v *{{ .TypeName }}) {{ .OutputName }} { if v == nil { return {{ .OutputName }}{} } @@ -159,8 +159,8 @@ func ({{ .VariableName }} *{{ .OutputName }}) Set(v {{ .TypeName }}) { {{ .VariableName }}.value = &v } -// ToPointer returns a *{{ .TypeName }} of the value or nil if not present. -func ({{ .VariableName }} {{ .OutputName }}) ToPointer() *{{ .TypeName }} { +// ToPtr returns a *{{ .TypeName }} of the value or nil if not present. +func ({{ .VariableName }} {{ .OutputName }}) ToPtr() *{{ .TypeName }} { if !{{ .VariableName }}.Present() { return nil } diff --git a/cmd/optional/testdata/optional_bar.go b/cmd/optional/testdata/optional_bar.go index 53ad73b..5024925 100644 --- a/cmd/optional/testdata/optional_bar.go +++ b/cmd/optional/testdata/optional_bar.go @@ -17,8 +17,8 @@ func NewoptionalBar(v bar) optionalBar { return optionalBar{&v} } -// NewoptionalBarFromPointer creates an optional.optionalBar from a bar pointer. -func NewoptionalBarFromPointer(v *bar) optionalBar { +// NewoptionalBarFromPtr creates an optional.optionalBar from a bar pointer. +func NewoptionalBarFromPtr(v *bar) optionalBar { if v == nil { return optionalBar{} } @@ -30,8 +30,8 @@ func (o *optionalBar) Set(v bar) { o.value = &v } -// ToPointer returns a *bar of the value or nil if not present. -func (o optionalBar) ToPointer() *bar { +// ToPtr returns a *bar of the value or nil if not present. +func (o optionalBar) ToPtr() *bar { if !o.Present() { return nil } diff --git a/cmd/optional/testdata/optional_foo.go b/cmd/optional/testdata/optional_foo.go index 0450786..3d133ba 100644 --- a/cmd/optional/testdata/optional_foo.go +++ b/cmd/optional/testdata/optional_foo.go @@ -17,8 +17,8 @@ func NewOptionalFoo(v Foo) OptionalFoo { return OptionalFoo{&v} } -// NewOptionalFooFromPointer creates an optional.OptionalFoo from a Foo pointer. -func NewOptionalFooFromPointer(v *Foo) OptionalFoo { +// NewOptionalFooFromPtr creates an optional.OptionalFoo from a Foo pointer. +func NewOptionalFooFromPtr(v *Foo) OptionalFoo { if v == nil { return OptionalFoo{} } @@ -30,8 +30,8 @@ func (o *OptionalFoo) Set(v Foo) { o.value = &v } -// ToPointer returns a *Foo of the value or nil if not present. -func (o OptionalFoo) ToPointer() *Foo { +// ToPtr returns a *Foo of the value or nil if not present. +func (o OptionalFoo) ToPtr() *Foo { if !o.Present() { return nil } diff --git a/cmd/optional/testdata/string.go b/cmd/optional/testdata/string.go index 2971379..e5b4027 100644 --- a/cmd/optional/testdata/string.go +++ b/cmd/optional/testdata/string.go @@ -17,8 +17,8 @@ func NewString(v string) String { return String{&v} } -// NewStringFromPointer creates an optional.String from a string pointer. -func NewStringFromPointer(v *string) String { +// NewStringFromPtr creates an optional.String from a string pointer. +func NewStringFromPtr(v *string) String { if v == nil { return String{} } @@ -30,8 +30,8 @@ func (s *String) Set(v string) { s.value = &v } -// ToPointer returns a *string of the value or nil if not present. -func (s String) ToPointer() *string { +// ToPtr returns a *string of the value or nil if not present. +func (s String) ToPtr() *string { if !s.Present() { return nil } diff --git a/complex128.go b/complex128.go index 5fd2ed0..5a31ad9 100644 --- a/complex128.go +++ b/complex128.go @@ -16,8 +16,8 @@ func NewComplex128(v complex128) Complex128 { return Complex128{&v} } -// NewComplex128FromPointer creates an optional.Complex128 from a complex128 pointer. -func NewComplex128FromPointer(v *complex128) Complex128 { +// NewComplex128FromPtr creates an optional.Complex128 from a complex128 pointer. +func NewComplex128FromPtr(v *complex128) Complex128 { if v == nil { return Complex128{} } @@ -29,8 +29,8 @@ func (c *Complex128) Set(v complex128) { c.value = &v } -// ToPointer returns a *complex128 of the value or nil if not present. -func (c Complex128) ToPointer() *complex128 { +// ToPtr returns a *complex128 of the value or nil if not present. +func (c Complex128) ToPtr() *complex128 { if !c.Present() { return nil } diff --git a/complex64.go b/complex64.go index 74e7ea7..7d06bcc 100644 --- a/complex64.go +++ b/complex64.go @@ -16,8 +16,8 @@ func NewComplex64(v complex64) Complex64 { return Complex64{&v} } -// NewComplex64FromPointer creates an optional.Complex64 from a complex64 pointer. -func NewComplex64FromPointer(v *complex64) Complex64 { +// NewComplex64FromPtr creates an optional.Complex64 from a complex64 pointer. +func NewComplex64FromPtr(v *complex64) Complex64 { if v == nil { return Complex64{} } @@ -29,8 +29,8 @@ func (c *Complex64) Set(v complex64) { c.value = &v } -// ToPointer returns a *complex64 of the value or nil if not present. -func (c Complex64) ToPointer() *complex64 { +// ToPtr returns a *complex64 of the value or nil if not present. +func (c Complex64) ToPtr() *complex64 { if !c.Present() { return nil } diff --git a/error.go b/error.go index 8ee77d4..5e8d0e6 100644 --- a/error.go +++ b/error.go @@ -17,8 +17,8 @@ func NewError(v error) Error { return Error{&v} } -// NewErrorFromPointer creates an optional.Error from a error pointer. -func NewErrorFromPointer(v *error) Error { +// NewErrorFromPtr creates an optional.Error from a error pointer. +func NewErrorFromPtr(v *error) Error { if v == nil { return Error{} } @@ -30,8 +30,8 @@ func (e *Error) Set(v error) { e.value = &v } -// ToPointer returns a *error of the value or nil if not present. -func (e Error) ToPointer() *error { +// ToPtr returns a *error of the value or nil if not present. +func (e Error) ToPtr() *error { if !e.Present() { return nil } diff --git a/float32.go b/float32.go index de6cbd6..c94121b 100644 --- a/float32.go +++ b/float32.go @@ -17,8 +17,8 @@ func NewFloat32(v float32) Float32 { return Float32{&v} } -// NewFloat32FromPointer creates an optional.Float32 from a float32 pointer. -func NewFloat32FromPointer(v *float32) Float32 { +// NewFloat32FromPtr creates an optional.Float32 from a float32 pointer. +func NewFloat32FromPtr(v *float32) Float32 { if v == nil { return Float32{} } @@ -30,8 +30,8 @@ func (f *Float32) Set(v float32) { f.value = &v } -// ToPointer returns a *float32 of the value or nil if not present. -func (f Float32) ToPointer() *float32 { +// ToPtr returns a *float32 of the value or nil if not present. +func (f Float32) ToPtr() *float32 { if !f.Present() { return nil } diff --git a/float64.go b/float64.go index bba0ae9..935750d 100644 --- a/float64.go +++ b/float64.go @@ -17,8 +17,8 @@ func NewFloat64(v float64) Float64 { return Float64{&v} } -// NewFloat64FromPointer creates an optional.Float64 from a float64 pointer. -func NewFloat64FromPointer(v *float64) Float64 { +// NewFloat64FromPtr creates an optional.Float64 from a float64 pointer. +func NewFloat64FromPtr(v *float64) Float64 { if v == nil { return Float64{} } @@ -30,8 +30,8 @@ func (f *Float64) Set(v float64) { f.value = &v } -// ToPointer returns a *float64 of the value or nil if not present. -func (f Float64) ToPointer() *float64 { +// ToPtr returns a *float64 of the value or nil if not present. +func (f Float64) ToPtr() *float64 { if !f.Present() { return nil } diff --git a/int.go b/int.go index 1a3ff65..92b1334 100644 --- a/int.go +++ b/int.go @@ -17,8 +17,8 @@ func NewInt(v int) Int { return Int{&v} } -// NewIntFromPointer creates an optional.Int from a int pointer. -func NewIntFromPointer(v *int) Int { +// NewIntFromPtr creates an optional.Int from a int pointer. +func NewIntFromPtr(v *int) Int { if v == nil { return Int{} } @@ -30,8 +30,8 @@ func (i *Int) Set(v int) { i.value = &v } -// ToPointer returns a *int of the value or nil if not present. -func (i Int) ToPointer() *int { +// ToPtr returns a *int of the value or nil if not present. +func (i Int) ToPtr() *int { if !i.Present() { return nil } diff --git a/int16.go b/int16.go index a2e56ac..05286d6 100644 --- a/int16.go +++ b/int16.go @@ -17,8 +17,8 @@ func NewInt16(v int16) Int16 { return Int16{&v} } -// NewInt16FromPointer creates an optional.Int16 from a int16 pointer. -func NewInt16FromPointer(v *int16) Int16 { +// NewInt16FromPtr creates an optional.Int16 from a int16 pointer. +func NewInt16FromPtr(v *int16) Int16 { if v == nil { return Int16{} } @@ -30,8 +30,8 @@ func (i *Int16) Set(v int16) { i.value = &v } -// ToPointer returns a *int16 of the value or nil if not present. -func (i Int16) ToPointer() *int16 { +// ToPtr returns a *int16 of the value or nil if not present. +func (i Int16) ToPtr() *int16 { if !i.Present() { return nil } diff --git a/int32.go b/int32.go index 9f1a4ce..75040ef 100644 --- a/int32.go +++ b/int32.go @@ -17,8 +17,8 @@ func NewInt32(v int32) Int32 { return Int32{&v} } -// NewInt32FromPointer creates an optional.Int32 from a int32 pointer. -func NewInt32FromPointer(v *int32) Int32 { +// NewInt32FromPtr creates an optional.Int32 from a int32 pointer. +func NewInt32FromPtr(v *int32) Int32 { if v == nil { return Int32{} } @@ -30,8 +30,8 @@ func (i *Int32) Set(v int32) { i.value = &v } -// ToPointer returns a *int32 of the value or nil if not present. -func (i Int32) ToPointer() *int32 { +// ToPtr returns a *int32 of the value or nil if not present. +func (i Int32) ToPtr() *int32 { if !i.Present() { return nil } diff --git a/int64.go b/int64.go index 72132bc..b131bcd 100644 --- a/int64.go +++ b/int64.go @@ -17,8 +17,8 @@ func NewInt64(v int64) Int64 { return Int64{&v} } -// NewInt64FromPointer creates an optional.Int64 from a int64 pointer. -func NewInt64FromPointer(v *int64) Int64 { +// NewInt64FromPtr creates an optional.Int64 from a int64 pointer. +func NewInt64FromPtr(v *int64) Int64 { if v == nil { return Int64{} } @@ -30,8 +30,8 @@ func (i *Int64) Set(v int64) { i.value = &v } -// ToPointer returns a *int64 of the value or nil if not present. -func (i Int64) ToPointer() *int64 { +// ToPtr returns a *int64 of the value or nil if not present. +func (i Int64) ToPtr() *int64 { if !i.Present() { return nil } diff --git a/int8.go b/int8.go index cd31504..ed94591 100644 --- a/int8.go +++ b/int8.go @@ -17,8 +17,8 @@ func NewInt8(v int8) Int8 { return Int8{&v} } -// NewInt8FromPointer creates an optional.Int8 from a int8 pointer. -func NewInt8FromPointer(v *int8) Int8 { +// NewInt8FromPtr creates an optional.Int8 from a int8 pointer. +func NewInt8FromPtr(v *int8) Int8 { if v == nil { return Int8{} } @@ -30,8 +30,8 @@ func (i *Int8) Set(v int8) { i.value = &v } -// ToPointer returns a *int8 of the value or nil if not present. -func (i Int8) ToPointer() *int8 { +// ToPtr returns a *int8 of the value or nil if not present. +func (i Int8) ToPtr() *int8 { if !i.Present() { return nil } diff --git a/int_test.go b/int_test.go index 8adcb6c..0ff015c 100644 --- a/int_test.go +++ b/int_test.go @@ -7,10 +7,10 @@ import ( "github.com/stretchr/testify/assert" ) -func TestInt_NewIntFromPointer_NotNil(t *testing.T) { +func TestInt_NewIntFromPtr_NotNil(t *testing.T) { i := 42 p := &i - o := NewIntFromPointer(p) + o := NewIntFromPtr(p) v, err := o.Get() assert.True(t, o.Present()) @@ -19,8 +19,8 @@ func TestInt_NewIntFromPointer_NotNil(t *testing.T) { assert.True(t, p != o.value, "expect return pointer to be different from embedded pointer") } -func TestInt_NewIntFromPointer_Nil(t *testing.T) { - o := NewIntFromPointer(nil) +func TestInt_NewIntFromPtr_Nil(t *testing.T) { + o := NewIntFromPtr(nil) var zero int v, err := o.Get() diff --git a/rune.go b/rune.go index d9a1998..50d1753 100644 --- a/rune.go +++ b/rune.go @@ -17,8 +17,8 @@ func NewRune(v rune) Rune { return Rune{&v} } -// NewRuneFromPointer creates an optional.Rune from a rune pointer. -func NewRuneFromPointer(v *rune) Rune { +// NewRuneFromPtr creates an optional.Rune from a rune pointer. +func NewRuneFromPtr(v *rune) Rune { if v == nil { return Rune{} } @@ -30,8 +30,8 @@ func (r *Rune) Set(v rune) { r.value = &v } -// ToPointer returns a *rune of the value or nil if not present. -func (r Rune) ToPointer() *rune { +// ToPtr returns a *rune of the value or nil if not present. +func (r Rune) ToPtr() *rune { if !r.Present() { return nil } diff --git a/string.go b/string.go index 2971379..e5b4027 100644 --- a/string.go +++ b/string.go @@ -17,8 +17,8 @@ func NewString(v string) String { return String{&v} } -// NewStringFromPointer creates an optional.String from a string pointer. -func NewStringFromPointer(v *string) String { +// NewStringFromPtr creates an optional.String from a string pointer. +func NewStringFromPtr(v *string) String { if v == nil { return String{} } @@ -30,8 +30,8 @@ func (s *String) Set(v string) { s.value = &v } -// ToPointer returns a *string of the value or nil if not present. -func (s String) ToPointer() *string { +// ToPtr returns a *string of the value or nil if not present. +func (s String) ToPtr() *string { if !s.Present() { return nil } diff --git a/string_test.go b/string_test.go index e8a9e69..eed1b13 100644 --- a/string_test.go +++ b/string_test.go @@ -7,10 +7,10 @@ import ( "github.com/stretchr/testify/assert" ) -func TestString_NewStringFromPointer_NotNil(t *testing.T) { +func TestString_NewStringFromPtr_NotNil(t *testing.T) { s := "foo" p := &s - o := NewStringFromPointer(p) + o := NewStringFromPtr(p) v, err := o.Get() assert.True(t, o.Present()) @@ -19,8 +19,8 @@ func TestString_NewStringFromPointer_NotNil(t *testing.T) { assert.True(t, p != o.value, "expect return pointer to be different from embedded pointer") } -func TestString_NewStringFromPointer_Nil(t *testing.T) { - o := NewStringFromPointer(nil) +func TestString_NewStringFromPtr_Nil(t *testing.T) { + o := NewStringFromPtr(nil) v, err := o.Get() assert.False(t, o.Present()) @@ -100,19 +100,19 @@ func TestString_If_NotPresent(t *testing.T) { assert.False(t, canary) } -func TestString_ToPointer_NotNil(t *testing.T) { +func TestString_ToPtr_NotNil(t *testing.T) { o := NewString("foo") - p := o.ToPointer() + p := o.ToPtr() assert.NotNil(t, p) assert.Equal(t, "foo", *p) assert.True(t, p != o.value, "expect return pointer to be different from embedded pointer") } -func TestString_ToPointer_Nil(t *testing.T) { +func TestString_ToPtr_Nil(t *testing.T) { o := String{} - p := o.ToPointer() + p := o.ToPtr() assert.Nil(t, p) } diff --git a/uint.go b/uint.go index 8bfaab7..a8218ed 100644 --- a/uint.go +++ b/uint.go @@ -17,8 +17,8 @@ func NewUint(v uint) Uint { return Uint{&v} } -// NewUintFromPointer creates an optional.Uint from a uint pointer. -func NewUintFromPointer(v *uint) Uint { +// NewUintFromPtr creates an optional.Uint from a uint pointer. +func NewUintFromPtr(v *uint) Uint { if v == nil { return Uint{} } @@ -30,8 +30,8 @@ func (u *Uint) Set(v uint) { u.value = &v } -// ToPointer returns a *uint of the value or nil if not present. -func (u Uint) ToPointer() *uint { +// ToPtr returns a *uint of the value or nil if not present. +func (u Uint) ToPtr() *uint { if !u.Present() { return nil } diff --git a/uint16.go b/uint16.go index 7108652..b9c6656 100644 --- a/uint16.go +++ b/uint16.go @@ -17,8 +17,8 @@ func NewUint16(v uint16) Uint16 { return Uint16{&v} } -// NewUint16FromPointer creates an optional.Uint16 from a uint16 pointer. -func NewUint16FromPointer(v *uint16) Uint16 { +// NewUint16FromPtr creates an optional.Uint16 from a uint16 pointer. +func NewUint16FromPtr(v *uint16) Uint16 { if v == nil { return Uint16{} } @@ -30,8 +30,8 @@ func (u *Uint16) Set(v uint16) { u.value = &v } -// ToPointer returns a *uint16 of the value or nil if not present. -func (u Uint16) ToPointer() *uint16 { +// ToPtr returns a *uint16 of the value or nil if not present. +func (u Uint16) ToPtr() *uint16 { if !u.Present() { return nil } diff --git a/uint32.go b/uint32.go index 6c65761..007771c 100644 --- a/uint32.go +++ b/uint32.go @@ -17,8 +17,8 @@ func NewUint32(v uint32) Uint32 { return Uint32{&v} } -// NewUint32FromPointer creates an optional.Uint32 from a uint32 pointer. -func NewUint32FromPointer(v *uint32) Uint32 { +// NewUint32FromPtr creates an optional.Uint32 from a uint32 pointer. +func NewUint32FromPtr(v *uint32) Uint32 { if v == nil { return Uint32{} } @@ -30,8 +30,8 @@ func (u *Uint32) Set(v uint32) { u.value = &v } -// ToPointer returns a *uint32 of the value or nil if not present. -func (u Uint32) ToPointer() *uint32 { +// ToPtr returns a *uint32 of the value or nil if not present. +func (u Uint32) ToPtr() *uint32 { if !u.Present() { return nil } diff --git a/uint64.go b/uint64.go index 9a8fee2..af1e499 100644 --- a/uint64.go +++ b/uint64.go @@ -17,8 +17,8 @@ func NewUint64(v uint64) Uint64 { return Uint64{&v} } -// NewUint64FromPointer creates an optional.Uint64 from a uint64 pointer. -func NewUint64FromPointer(v *uint64) Uint64 { +// NewUint64FromPtr creates an optional.Uint64 from a uint64 pointer. +func NewUint64FromPtr(v *uint64) Uint64 { if v == nil { return Uint64{} } @@ -30,8 +30,8 @@ func (u *Uint64) Set(v uint64) { u.value = &v } -// ToPointer returns a *uint64 of the value or nil if not present. -func (u Uint64) ToPointer() *uint64 { +// ToPtr returns a *uint64 of the value or nil if not present. +func (u Uint64) ToPtr() *uint64 { if !u.Present() { return nil } diff --git a/uint8.go b/uint8.go index 6f58460..1cf6f1a 100644 --- a/uint8.go +++ b/uint8.go @@ -17,8 +17,8 @@ func NewUint8(v uint8) Uint8 { return Uint8{&v} } -// NewUint8FromPointer creates an optional.Uint8 from a uint8 pointer. -func NewUint8FromPointer(v *uint8) Uint8 { +// NewUint8FromPtr creates an optional.Uint8 from a uint8 pointer. +func NewUint8FromPtr(v *uint8) Uint8 { if v == nil { return Uint8{} } @@ -30,8 +30,8 @@ func (u *Uint8) Set(v uint8) { u.value = &v } -// ToPointer returns a *uint8 of the value or nil if not present. -func (u Uint8) ToPointer() *uint8 { +// ToPtr returns a *uint8 of the value or nil if not present. +func (u Uint8) ToPtr() *uint8 { if !u.Present() { return nil } diff --git a/uintptr.go b/uintptr.go index 09c9a9d..81afca4 100644 --- a/uintptr.go +++ b/uintptr.go @@ -17,8 +17,8 @@ func NewUintptr(v uintptr) Uintptr { return Uintptr{&v} } -// NewUintptrFromPointer creates an optional.Uintptr from a uintptr pointer. -func NewUintptrFromPointer(v *uintptr) Uintptr { +// NewUintptrFromPtr creates an optional.Uintptr from a uintptr pointer. +func NewUintptrFromPtr(v *uintptr) Uintptr { if v == nil { return Uintptr{} } @@ -30,8 +30,8 @@ func (u *Uintptr) Set(v uintptr) { u.value = &v } -// ToPointer returns a *uintptr of the value or nil if not present. -func (u Uintptr) ToPointer() *uintptr { +// ToPtr returns a *uintptr of the value or nil if not present. +func (u Uintptr) ToPtr() *uintptr { if !u.Present() { return nil }