From 56f55e5e3093cb1ec885eae74ca91fb98afa0022 Mon Sep 17 00:00:00 2001 From: Stanislas Michalak Date: Thu, 14 Feb 2019 22:38:39 +0100 Subject: [PATCH 1/2] Handle nulls.Int Unmarshal errors properly (#349) * Handle nulls.Int Unmarshal errors properly Fixes #341 * Add tests for nulls.Int UnmarshalJSON --- nulls/int.go | 14 ++++++++--- nulls/int_test.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 nulls/int_test.go diff --git a/nulls/int.go b/nulls/int.go index 5afe5349..3c406136 100644 --- a/nulls/int.go +++ b/nulls/int.go @@ -57,10 +57,18 @@ func (ns Int) MarshalJSON() ([]byte, error) { // UnmarshalJSON will unmarshal a JSON value into // the propert representation of that value. func (ns *Int) UnmarshalJSON(text []byte) error { - if i, err := strconv.ParseInt(string(text), 10, strconv.IntSize); err == nil { - ns.Valid = true - ns.Int = int(i) + txt := string(text) + ns.Valid = true + if txt == "null" { + ns.Valid = false + return nil + } + i, err := strconv.ParseInt(txt, 10, strconv.IntSize) + if err != nil { + ns.Valid = false + return err } + ns.Int = int(i) return nil } diff --git a/nulls/int_test.go b/nulls/int_test.go new file mode 100644 index 00000000..b68563c7 --- /dev/null +++ b/nulls/int_test.go @@ -0,0 +1,62 @@ +package nulls_test + +import ( + "testing" + + "github.com/gobuffalo/pop/nulls" + "github.com/stretchr/testify/require" +) + +func Test_IntUnmarshalJSON(t *testing.T) { + r := require.New(t) + cases := []struct { + Input []byte + Value int + Valid bool + }{ + { + Input: []byte{'0'}, + Value: 0, + Valid: true, + }, + { + Input: []byte{'4', '2'}, + Value: 42, + Valid: true, + }, + { + Input: []byte{'n', 'u', 'l', 'l'}, + Value: 0, + Valid: false, + }, + } + + for _, c := range cases { + i := nulls.Int{} + r.NoError(i.UnmarshalJSON(c.Input)) + r.Equal(c.Value, i.Int) + r.Equal(c.Valid, i.Valid) + } +} + +func Test_IntUnmarshalJSON_Errors(t *testing.T) { + r := require.New(t) + + cases := []struct { + Input []byte + }{ + { + Input: []byte{'a'}, + }, + { + Input: []byte{}, + }, + } + + for _, c := range cases { + i := nulls.Int{} + r.Error(i.UnmarshalJSON(c.Input)) + r.Equal(0, i.Int) + r.False(i.Valid) + } +} From ea82301ead5317bfee63d08eca9383a680093f57 Mon Sep 17 00:00:00 2001 From: Stanislas Michalak Date: Thu, 14 Feb 2019 22:44:10 +0100 Subject: [PATCH 2/2] Bump version --- genny/config/config-packr.go | 1 + packrd/packed-packr.go | 1 + soda/cmd/version.go | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/genny/config/config-packr.go b/genny/config/config-packr.go index 9da88ac5..9d14c82e 100644 --- a/genny/config/config-packr.go +++ b/genny/config/config-packr.go @@ -1,3 +1,4 @@ +// +build !skippackr // Code generated by github.com/gobuffalo/packr/v2. DO NOT EDIT. // You can use the "packr clean" command to clean up this, diff --git a/packrd/packed-packr.go b/packrd/packed-packr.go index 2275420d..e352d302 100644 --- a/packrd/packed-packr.go +++ b/packrd/packed-packr.go @@ -1,3 +1,4 @@ +// +build !skippackr // Code generated by github.com/gobuffalo/packr/v2. DO NOT EDIT. // You can use the "packr2 clean" command to clean up this, diff --git a/soda/cmd/version.go b/soda/cmd/version.go index cd8f78a5..2b3230a1 100644 --- a/soda/cmd/version.go +++ b/soda/cmd/version.go @@ -1,4 +1,4 @@ package cmd // Version defines the current Pop version. -const Version = "v4.9.6" +const Version = "v4.9.7"