From 506ae7916afab4993dcf13d3d64b800f5fe2a742 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Thu, 8 Aug 2024 20:35:44 -0700 Subject: [PATCH] json.ErrorEmpty --- errors.go | 9 ++++++++ go.mod | 9 ++++++-- go.sum | 10 +++++++-- nullable_marshaljson.go | 16 +++++++++----- nullable_marshaljson_int_test.go | 37 +++++++++++++++++++++++++++++--- 5 files changed, 69 insertions(+), 12 deletions(-) create mode 100644 errors.go diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..4b877ad --- /dev/null +++ b/errors.go @@ -0,0 +1,9 @@ +package nul + +import ( + "github.com/reiver/go-erorr" +) + +const ( + errBadReflection = erorr.Error("opt: bad reflection") +) diff --git a/go.mod b/go.mod index 27dbe77..def03b3 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,10 @@ module github.com/reiver/go-nul -go 1.18 +go 1.22.4 -require github.com/reiver/go-erorr v0.0.0-20240704145350-0485e21eaa82 +require ( + github.com/reiver/go-erorr v0.0.0-20240801233437-8cbde6d1fa3f + github.com/reiver/go-json v0.0.0-20240808191545-fa5fbb1bb3f6 +) + +require github.com/reiver/go-lck v0.0.0-20240808133902-b56df221c39f // indirect diff --git a/go.sum b/go.sum index eec5ece..2d1b268 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,8 @@ -github.com/reiver/go-erorr v0.0.0-20240704145350-0485e21eaa82 h1:xxt7qL+7ZfRysXWXU2MpULOg/zWe5P+Fmw9VyUFCmZE= -github.com/reiver/go-erorr v0.0.0-20240704145350-0485e21eaa82/go.mod h1:F0HbBf+Ak2ZlE8YkDW4Y+KxaUmT0KaaIJK6CXY3cJxE= +github.com/reiver/go-erorr v0.0.0-20240801233437-8cbde6d1fa3f h1:D1QSxKHm8U73XhjsW3SFLkT0zT5pKJi+1KGboMhY1Rk= +github.com/reiver/go-erorr v0.0.0-20240801233437-8cbde6d1fa3f/go.mod h1:F0HbBf+Ak2ZlE8YkDW4Y+KxaUmT0KaaIJK6CXY3cJxE= +github.com/reiver/go-json v0.0.0-20240808191545-fa5fbb1bb3f6 h1:Ty3b38DeA5BKi42+3f3o8Bl4zEAKlJsF0ozArGJmo9k= +github.com/reiver/go-json v0.0.0-20240808191545-fa5fbb1bb3f6/go.mod h1:IHoQXCaObMGCDoTKko/XzDk8QbpNzepuCLMt7KKCLBs= +github.com/reiver/go-lck v0.0.0-20240808133902-b56df221c39f h1:KBVWBoNIM8mHkUR3dP64hm2p2vxoD5xHZ6wUllCjFTc= +github.com/reiver/go-lck v0.0.0-20240808133902-b56df221c39f/go.mod h1:PFseSi8S0CBkc+YB6jYWtogPk83LumclnYJZZBQJJhs= +github.com/reiver/go-opt v0.0.0-20240808175813-d4de6ca5ee95 h1:yMZeTlabM9bvx9ggghLCU4Djfp+FDcgC94+gyl+vzQE= +github.com/reiver/go-opt v0.0.0-20240808175813-d4de6ca5ee95/go.mod h1:iXlSKwDjhyF10Fdd2J5PYKwBFlWgfq7OTRxCHU+yTUY= diff --git a/nullable_marshaljson.go b/nullable_marshaljson.go index 0b478ef..d266a43 100644 --- a/nullable_marshaljson.go +++ b/nullable_marshaljson.go @@ -1,10 +1,12 @@ package nul import ( - "encoding/json" + "encoding" + "fmt" "reflect" "github.com/reiver/go-erorr" + "github.com/reiver/go-json" ) var _ json.Marshaler = Nothing[bool]() @@ -13,16 +15,20 @@ var _ json.Marshaler = Nothing[string]() // MarshalJSON makes it so json.Marshaler is implemented. func (receiver Nullable[T]) MarshalJSON() ([]byte, error) { switch interface{}(receiver.value).(type) { - case bool, string,json.Marshaler: + case json.Marshaler, encoding.TextMarshaler, bool, int, int8, int16, int32, int64, string, uint, uint8, uint16, uint32, uint64: // these are OK. default: - if reflect.Struct != reflect.TypeOf(receiver.value).Kind() { - return nil, erorr.Errorf("nul: cannot marshal something of type %T into JSON because parameterized type is ‘%T’ rather than ‘bool’, ‘string’, or ‘json.Marshaler’", receiver, receiver.value) + var reflectedType reflect.Type = reflect.TypeOf(receiver.value) + if nil == reflectedType { + return nil, errBadReflection + } + if reflect.Struct != reflectedType.Kind() { + return nil, erorr.Errorf("nul: cannot marshal something of type %T into JSON because parameterized type is ‘%T’ is not supported", receiver, receiver.value) } } if receiver.isnothing() { - return nil, erorr.Errorf("nul: cannot marshal nul.Nothing[%T]() into JSON", receiver.value) + return nil, json.ErrEmpty(fmt.Sprintf("nul: cannot marshal nul.Nothing[%T]() into JSON", receiver.value)) } if receiver.isnull { diff --git a/nullable_marshaljson_int_test.go b/nullable_marshaljson_int_test.go index e4295b3..6b083f0 100644 --- a/nullable_marshaljson_int_test.go +++ b/nullable_marshaljson_int_test.go @@ -10,94 +10,125 @@ func TestNullable_MarshalJSON_int(t *testing.T) { tests := []struct{ Value nul.Nullable[int] + Expected string }{ { Value: nul.Null[int](), + Expected: "null", }, { Value: nul.Something(-65536), + Expected: `-65536`, }, { Value: nul.Something(-65535), + Expected: `-65535`, }, { Value: nul.Something(-256), + Expected: `-256`, }, { Value: nul.Something(-255), + Expected: `-255`, }, { Value: nul.Something(-5), + Expected: `-5`, }, { Value: nul.Something(-4), + Expected: `-4`, }, { Value: nul.Something(-3), + Expected: `-3`, }, { Value: nul.Something(-2), + Expected: `-2`, }, { Value: nul.Something(-1), + Expected: `-1`, }, { Value: nul.Something(0), + Expected: `0`, }, { Value: nul.Something(1), + Expected: `1`, }, { Value: nul.Something(2), + Expected: `2`, }, { Value: nul.Something(3), + Expected: `3`, }, { Value: nul.Something(4), + Expected: `4`, }, { Value: nul.Something(5), + Expected: `5`, }, { Value: nul.Something(255), + Expected: `255`, }, { Value: nul.Something(256), + Expected: `256`, }, { Value: nul.Something(65535), + Expected: `65535`, }, { Value: nul.Something(65536), + Expected: `65536`, }, } for testNumber, test := range tests { actualBytes, err := test.Value.MarshalJSON() - if nil == err { - t.Errorf("For test #%d, expected an error but did not actually get one." , testNumber) - t.Logf("ACTUAL BYTES: %q", actualBytes) + if nil != err { + t.Errorf("For test #%d, did not expect an error but actually got one." , testNumber) t.Logf("ERROR: (%T) %s", err, err) t.Logf("VALUE: %#v", test.Value) continue } + + actual := string(actualBytes) + expected := test.Expected + + if expected != actual { + t.Errorf("For test #%d, the actual value for the JSON marshaling is not what was expected.", testNumber) + t.Logf("EXPECTED: %q", expected) + t.Logf("ACTUAL: %q", actual) + t.Logf("VALUE: %#v", test.Value) + continue + } } }