improvements

master
Charles Iliya Krempeaux 2023-09-27 09:48:15 +09:00
parent da3d80e27a
commit 8a8eeb3194
4 changed files with 13 additions and 7 deletions

2
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -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 {

View File

@ -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] {