From d4de6ca5ee9508c102fc6ec82ece7bc637bd01f4 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Thu, 8 Aug 2024 10:58:13 -0700 Subject: [PATCH] json.ErrorEmpty --- errors.go | 9 ++++++++ go.mod | 9 ++++++-- go.sum | 8 +++++-- optional_marshaljson.go | 19 +++++++++++++---- optional_marshaljson_int_test.go | 36 +++++++++++++++++++++++++++++--- 5 files changed, 70 insertions(+), 11 deletions(-) create mode 100644 errors.go diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..89a904d --- /dev/null +++ b/errors.go @@ -0,0 +1,9 @@ +package opt + +import ( + "github.com/reiver/go-erorr" +) + +const ( + errBadReflection = erorr.Error("opt: bad reflection") +) diff --git a/go.mod b/go.mod index 8b0180d..9c93d0a 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,10 @@ module github.com/reiver/go-opt -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-20240808163155-5fb56b49f847 +) + +require github.com/reiver/go-lck v0.0.0-20240808133902-b56df221c39f // indirect diff --git a/go.sum b/go.sum index eec5ece..dba8420 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,6 @@ -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-20240808163155-5fb56b49f847 h1:Y8BYW4keqqtdEWECdG7Pg60U0awT3DioXiIfcXvWhMY= +github.com/reiver/go-json v0.0.0-20240808163155-5fb56b49f847/go.mod h1:ns4kpzbK+OyMUZYT8UmBxrsYWNmvB/irZGmyIh99cxY= +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= diff --git a/optional_marshaljson.go b/optional_marshaljson.go index 43be907..7b0c6e4 100644 --- a/optional_marshaljson.go +++ b/optional_marshaljson.go @@ -1,7 +1,11 @@ package opt import ( - "encoding/json" + "encoding" + "fmt" + "reflect" + + "github.com/reiver/go-json" "github.com/reiver/go-erorr" ) @@ -11,15 +15,22 @@ var _ json.Marshaler = Nothing[string]() // MarshalJSON makes it so json.Marshaler is implemented. func (receiver Optional[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: - return nil, erorr.Errorf("opt: 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("opt: 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("opt: cannot marshal opt.Nothing[%T]() into JSON", receiver.value) + return nil, json.ErrEmpty(fmt.Sprintf("opt: cannot marshal opt.Nothing[%T]() into JSON", receiver.value)) } return json.Marshal(receiver.value) diff --git a/optional_marshaljson_int_test.go b/optional_marshaljson_int_test.go index b54666d..6e4930f 100644 --- a/optional_marshaljson_int_test.go +++ b/optional_marshaljson_int_test.go @@ -10,88 +10,118 @@ func TestOptional_MarshalJSON_int(t *testing.T) { tests := []struct{ Value opt.Optional[int] + Expected string }{ { Value: opt.Something(-65536), + Expected: `-65536`, }, { Value: opt.Something(-65535), + Expected: `-65535`, }, { Value: opt.Something(-256), + Expected: `-256`, }, { Value: opt.Something(-255), + Expected: `-255`, }, { Value: opt.Something(-5), + Expected: `-5`, }, { Value: opt.Something(-4), + Expected: `-4`, }, { Value: opt.Something(-3), + Expected: `-3`, }, { Value: opt.Something(-2), + Expected: `-2`, }, { Value: opt.Something(-1), + Expected: `-1`, }, { Value: opt.Something(0), + Expected: `0`, }, { Value: opt.Something(1), + Expected: `1`, }, { Value: opt.Something(2), + Expected: `2`, }, { Value: opt.Something(3), + Expected: `3`, }, { Value: opt.Something(4), + Expected: `4`, }, { Value: opt.Something(5), + Expected: `5`, }, { Value: opt.Something(255), + Expected: `255`, }, { Value: opt.Something(256), + Expected: `256`, }, { Value: opt.Something(65535), + Expected: `65535`, }, { Value: opt.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 + } } }