From 76ecfe527918d60bedaeb4909854aa9ff1361e70 Mon Sep 17 00:00:00 2001 From: Matt Wielbut Date: Thu, 6 Dec 2018 14:10:38 +0000 Subject: [PATCH 1/6] =?UTF-8?q?Support=20=E2=80=9Cnull=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change adds support for marshaling and unmarshaling “null” and reflecting that the value is not present, rather than relying on the “zero” value --- bool.go | 11 ++++++++--- byte.go | 11 ++++++++--- cmd/optional/main.go | 9 +++++++-- 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 ++++++++--- 21 files changed, 167 insertions(+), 62 deletions(-) diff --git a/bool.go b/bool.go index b0d0432..8c073ac 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:07:54.236383 +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" { + s.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..845bc8d 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:07:54.563833 +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" { + s.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..4142f3f 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" { + s.value = nil + return nil + } + var value {{ .TypeName }} if err := json.Unmarshal(data, &value); err != nil { diff --git a/complex128.go b/complex128.go index e6954c3..cdac613 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:07:54.877068 +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" { + s.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..d34113c 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:07:55.21537 +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" { + s.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..78b693e 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:07:55.548188 +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" { + s.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..5eab415 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:07:55.918125 +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" { + s.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..4c46aa8 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:07:56.261095 +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" { + s.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..118b725 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:07:56.587583 +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" { + s.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..dd49f9d 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:07:56.89583 +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" { + s.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..2afc9c3 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:07:57.233661 +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" { + s.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..d805d42 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:07:57.555013 +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" { + s.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..3c659eb 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:07:57.888442 +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" { + s.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..20e7f6e 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:07:58.218574 +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" { + s.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..a22e34b 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:07:58.540215 +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..855e979 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:07:58.872949 +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" { + s.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..6971e90 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:07:59.233036 +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" { + s.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..cfcb813 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:07:59.550126 +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" { + s.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..4263422 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:07:59.86444 +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" { + s.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..1653c91 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:08:00.186722 +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" { + s.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..25e0511 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:08:00.513848 +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" { + s.value = nil + return nil + } + var value uintptr if err := json.Unmarshal(data, &value); err != nil { From 1c9da6722c999217f13829319be254d07b678336 Mon Sep 17 00:00:00 2001 From: Matt Wielbut Date: Thu, 6 Dec 2018 14:51:29 +0000 Subject: [PATCH 2/6] Fix to specify {{ .VariableName }} --- bool.go | 4 ++-- byte.go | 4 ++-- cmd/optional/main.go | 2 +- complex128.go | 4 ++-- complex64.go | 4 ++-- error.go | 4 ++-- float32.go | 4 ++-- float64.go | 4 ++-- int.go | 4 ++-- int16.go | 4 ++-- int32.go | 4 ++-- int64.go | 4 ++-- int8.go | 4 ++-- rune.go | 4 ++-- string.go | 2 +- uint.go | 4 ++-- uint16.go | 4 ++-- uint32.go | 4 ++-- uint64.go | 4 ++-- uint8.go | 4 ++-- uintptr.go | 4 ++-- 21 files changed, 40 insertions(+), 40 deletions(-) diff --git a/bool.go b/bool.go index 8c073ac..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-12-06 14:07:54.236383 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:53.447681 +0000 UTC package optional @@ -62,7 +62,7 @@ func (b Bool) MarshalJSON() ([]byte, error) { func (b *Bool) UnmarshalJSON(data []byte) error { if string(data) == "null" { - s.value = nil + b.value = nil return nil } diff --git a/byte.go b/byte.go index 845bc8d..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-12-06 14:07:54.563833 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:53.79355 +0000 UTC package optional @@ -62,7 +62,7 @@ func (b Byte) MarshalJSON() ([]byte, error) { func (b *Byte) UnmarshalJSON(data []byte) error { if string(data) == "null" { - s.value = nil + b.value = nil return nil } diff --git a/cmd/optional/main.go b/cmd/optional/main.go index 4142f3f..0c8ce56 100644 --- a/cmd/optional/main.go +++ b/cmd/optional/main.go @@ -189,7 +189,7 @@ func ({{ .VariableName }} {{ .OutputName }}) MarshalJSON() ([]byte, error) { func ({{ .VariableName }} *{{ .OutputName }}) UnmarshalJSON(data []byte) error { if string(data) == "null" { - s.value = nil + {{ .VariableName }}.value = nil return nil } diff --git a/complex128.go b/complex128.go index cdac613..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-12-06 14:07:54.877068 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:54.128811 +0000 UTC package optional @@ -62,7 +62,7 @@ func (c Complex128) MarshalJSON() ([]byte, error) { func (c *Complex128) UnmarshalJSON(data []byte) error { if string(data) == "null" { - s.value = nil + c.value = nil return nil } diff --git a/complex64.go b/complex64.go index d34113c..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-12-06 14:07:55.21537 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:54.464548 +0000 UTC package optional @@ -62,7 +62,7 @@ func (c Complex64) MarshalJSON() ([]byte, error) { func (c *Complex64) UnmarshalJSON(data []byte) error { if string(data) == "null" { - s.value = nil + c.value = nil return nil } diff --git a/error.go b/error.go index 78b693e..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-12-06 14:07:55.548188 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:54.804224 +0000 UTC package optional @@ -62,7 +62,7 @@ func (e Error) MarshalJSON() ([]byte, error) { func (e *Error) UnmarshalJSON(data []byte) error { if string(data) == "null" { - s.value = nil + e.value = nil return nil } diff --git a/float32.go b/float32.go index 5eab415..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-12-06 14:07:55.918125 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:55.1819 +0000 UTC package optional @@ -62,7 +62,7 @@ func (f Float32) MarshalJSON() ([]byte, error) { func (f *Float32) UnmarshalJSON(data []byte) error { if string(data) == "null" { - s.value = nil + f.value = nil return nil } diff --git a/float64.go b/float64.go index 4c46aa8..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-12-06 14:07:56.261095 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:55.521255 +0000 UTC package optional @@ -62,7 +62,7 @@ func (f Float64) MarshalJSON() ([]byte, error) { func (f *Float64) UnmarshalJSON(data []byte) error { if string(data) == "null" { - s.value = nil + f.value = nil return nil } diff --git a/int.go b/int.go index 118b725..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-12-06 14:07:56.587583 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:55.863991 +0000 UTC package optional @@ -62,7 +62,7 @@ func (i Int) MarshalJSON() ([]byte, error) { func (i *Int) UnmarshalJSON(data []byte) error { if string(data) == "null" { - s.value = nil + i.value = nil return nil } diff --git a/int16.go b/int16.go index dd49f9d..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-12-06 14:07:56.89583 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:56.212093 +0000 UTC package optional @@ -62,7 +62,7 @@ func (i Int16) MarshalJSON() ([]byte, error) { func (i *Int16) UnmarshalJSON(data []byte) error { if string(data) == "null" { - s.value = nil + i.value = nil return nil } diff --git a/int32.go b/int32.go index 2afc9c3..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-12-06 14:07:57.233661 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:56.641482 +0000 UTC package optional @@ -62,7 +62,7 @@ func (i Int32) MarshalJSON() ([]byte, error) { func (i *Int32) UnmarshalJSON(data []byte) error { if string(data) == "null" { - s.value = nil + i.value = nil return nil } diff --git a/int64.go b/int64.go index d805d42..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-12-06 14:07:57.555013 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:57.002648 +0000 UTC package optional @@ -62,7 +62,7 @@ func (i Int64) MarshalJSON() ([]byte, error) { func (i *Int64) UnmarshalJSON(data []byte) error { if string(data) == "null" { - s.value = nil + i.value = nil return nil } diff --git a/int8.go b/int8.go index 3c659eb..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-12-06 14:07:57.888442 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:57.555216 +0000 UTC package optional @@ -62,7 +62,7 @@ func (i Int8) MarshalJSON() ([]byte, error) { func (i *Int8) UnmarshalJSON(data []byte) error { if string(data) == "null" { - s.value = nil + i.value = nil return nil } diff --git a/rune.go b/rune.go index 20e7f6e..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-12-06 14:07:58.218574 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:57.969673 +0000 UTC package optional @@ -62,7 +62,7 @@ func (r Rune) MarshalJSON() ([]byte, error) { func (r *Rune) UnmarshalJSON(data []byte) error { if string(data) == "null" { - s.value = nil + r.value = nil return nil } diff --git a/string.go b/string.go index a22e34b..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-12-06 14:07:58.540215 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:58.516631 +0000 UTC package optional diff --git a/uint.go b/uint.go index 855e979..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-12-06 14:07:58.872949 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:58.898667 +0000 UTC package optional @@ -62,7 +62,7 @@ func (u Uint) MarshalJSON() ([]byte, error) { func (u *Uint) UnmarshalJSON(data []byte) error { if string(data) == "null" { - s.value = nil + u.value = nil return nil } diff --git a/uint16.go b/uint16.go index 6971e90..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-12-06 14:07:59.233036 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:59.455534 +0000 UTC package optional @@ -62,7 +62,7 @@ func (u Uint16) MarshalJSON() ([]byte, error) { func (u *Uint16) UnmarshalJSON(data []byte) error { if string(data) == "null" { - s.value = nil + u.value = nil return nil } diff --git a/uint32.go b/uint32.go index cfcb813..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-12-06 14:07:59.550126 +0000 UTC +// This file was generated by robots at 2018-12-06 14:29:59.856381 +0000 UTC package optional @@ -62,7 +62,7 @@ func (u Uint32) MarshalJSON() ([]byte, error) { func (u *Uint32) UnmarshalJSON(data []byte) error { if string(data) == "null" { - s.value = nil + u.value = nil return nil } diff --git a/uint64.go b/uint64.go index 4263422..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-12-06 14:07:59.86444 +0000 UTC +// This file was generated by robots at 2018-12-06 14:30:00.317311 +0000 UTC package optional @@ -62,7 +62,7 @@ func (u Uint64) MarshalJSON() ([]byte, error) { func (u *Uint64) UnmarshalJSON(data []byte) error { if string(data) == "null" { - s.value = nil + u.value = nil return nil } diff --git a/uint8.go b/uint8.go index 1653c91..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-12-06 14:08:00.186722 +0000 UTC +// This file was generated by robots at 2018-12-06 14:30:00.699077 +0000 UTC package optional @@ -62,7 +62,7 @@ func (u Uint8) MarshalJSON() ([]byte, error) { func (u *Uint8) UnmarshalJSON(data []byte) error { if string(data) == "null" { - s.value = nil + u.value = nil return nil } diff --git a/uintptr.go b/uintptr.go index 25e0511..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-12-06 14:08:00.513848 +0000 UTC +// This file was generated by robots at 2018-12-06 14:30:01.109101 +0000 UTC package optional @@ -62,7 +62,7 @@ func (u Uintptr) MarshalJSON() ([]byte, error) { func (u *Uintptr) UnmarshalJSON(data []byte) error { if string(data) == "null" { - s.value = nil + u.value = nil return nil } From 3cbdf26a89418e67c2a9067999dcdef623184179 Mon Sep 17 00:00:00 2001 From: Matt Wielbut Date: Thu, 6 Dec 2018 14:53:12 +0000 Subject: [PATCH 3/6] Add support for go modules --- go.mod | 7 +++++++ go.sum | 6 ++++++ 2 files changed, 13 insertions(+) create mode 100644 go.mod create mode 100644 go.sum diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..0cdfc10 --- /dev/null +++ b/go.mod @@ -0,0 +1,7 @@ +module optional + +require ( + github.com/davecgh/go-spew v1.1.0 + github.com/pmezard/go-difflib v1.0.0 + github.com/stretchr/testify v1.2.2 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..311e617 --- /dev/null +++ b/go.sum @@ -0,0 +1,6 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= From ec18abf516500233b467818adf959197921ff5a0 Mon Sep 17 00:00:00 2001 From: Matt Wielbut Date: Thu, 6 Dec 2018 17:48:39 +0000 Subject: [PATCH 4/6] Fix go mod path --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 0cdfc10..09f7404 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module optional +module github.com/markphelps/optional require ( github.com/davecgh/go-spew v1.1.0 From 96324806cb2405a8f5cee928d157eb8f85578105 Mon Sep 17 00:00:00 2001 From: Matt Wielbut Date: Thu, 6 Dec 2018 17:59:05 +0000 Subject: [PATCH 5/6] Update golden test --- cmd/optional/testdata/optional_bar.go | 11 ++++++++--- cmd/optional/testdata/optional_foo.go | 11 ++++++++--- cmd/optional/testdata/string.go | 11 ++++++++--- 3 files changed, 24 insertions(+), 9 deletions(-) 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 { From 104550e7ba5a38471525d6335af599964946df58 Mon Sep 17 00:00:00 2001 From: Matt Wielbut Date: Fri, 7 Dec 2018 14:34:20 +0000 Subject: [PATCH 6/6] Remove go.mod per pull request --- go.mod | 7 ------- go.sum | 6 ------ 2 files changed, 13 deletions(-) delete mode 100644 go.mod delete mode 100644 go.sum diff --git a/go.mod b/go.mod deleted file mode 100644 index 09f7404..0000000 --- a/go.mod +++ /dev/null @@ -1,7 +0,0 @@ -module github.com/markphelps/optional - -require ( - github.com/davecgh/go-spew v1.1.0 - github.com/pmezard/go-difflib v1.0.0 - github.com/stretchr/testify v1.2.2 -) diff --git a/go.sum b/go.sum deleted file mode 100644 index 311e617..0000000 --- a/go.sum +++ /dev/null @@ -1,6 +0,0 @@ -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=