From 8a8eeb3194fa12b63d7a656f6bc03032c65833c7 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Wed, 27 Sep 2023 09:48:15 +0900 Subject: [PATCH] improvements --- go.mod | 2 ++ go.sum | 2 ++ nullable_marshaljson.go | 9 +++++---- nullable_unmarshaljson.go | 7 ++++--- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 5937bb8..84a2168 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module sourcecode.social/reiver/go-nul go 1.18 + +require sourcecode.social/reiver/go-erorr v0.0.0-20230922202459-231149d185a1 // indirect diff --git a/go.sum b/go.sum index e69de29..fa79421 100644 --- a/go.sum +++ b/go.sum @@ -0,0 +1,2 @@ +sourcecode.social/reiver/go-erorr v0.0.0-20230922202459-231149d185a1 h1:wpnz4JicQBLWrgGphYBls7DysIFCcnWgDz/vce/sY8E= +sourcecode.social/reiver/go-erorr v0.0.0-20230922202459-231149d185a1/go.mod h1:NFtd7fzEf0r6A6R7JXYZfayRhPaJy0zt/18VWoLzrxA= diff --git a/nullable_marshaljson.go b/nullable_marshaljson.go index 62cbacd..bd3fe64 100644 --- a/nullable_marshaljson.go +++ b/nullable_marshaljson.go @@ -1,8 +1,9 @@ package nul import ( - "fmt" "encoding/json" + + "sourcecode.social/reiver/go-erorr" ) var _ json.Marshaler = Nothing[bool]() @@ -11,14 +12,14 @@ 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: + case bool, string,json.Marshaler: // these are OK. default: - return nil, fmt.Errorf("cannot marshal something of type %T into JSON because parameterized type is ‘%T’ rather than ‘bool’ or ‘string’", receiver, receiver.value) + 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) } if receiver.isnothing() { - return nil, fmt.Errorf("cannot marshal opt.Nothing[%T]() into JSON", receiver.value) + return nil, erorr.Errorf("nul: cannot marshal opt.Nothing[%T]() into JSON", receiver.value) } if receiver.isnull { diff --git a/nullable_unmarshaljson.go b/nullable_unmarshaljson.go index 6155e1e..171f008 100644 --- a/nullable_unmarshaljson.go +++ b/nullable_unmarshaljson.go @@ -1,8 +1,9 @@ package nul import ( - "fmt" "encoding/json" + + "sourcecode.social/reiver/go-erorr" ) var _ json.Unmarshaler = new(Nullable[bool]) @@ -11,10 +12,10 @@ var _ json.Unmarshaler = new(Nullable[string]) // UnmarshalJSON makes it so json.Unmarshaler is implemented. func (receiver *Nullable[T]) UnmarshalJSON(data []byte) error { switch interface{}(receiver.value).(type) { - case bool, string: + case bool, string,json.Unmarshaler: // these are OK. default: - return fmt.Errorf("cannot unmarshal into something of type %T from JSON because parameterized type is ‘%T’ rather than ‘bool’ or ‘string’", receiver, receiver.value) + return erorr.Errorf("nul: cannot unmarshal into something of type %T from JSON because parameterized type is ‘%T’ rather than ‘bool’, ‘string’, or ‘json.Unmarshaler’", receiver, receiver.value) } if 4 == len(data) && 'n' == data[0] && 'u' == data[1] && 'l' == data[2] && 'l' == data[3] {