improvements

master
Charles Iliya Krempeaux 2023-09-27 09:43:16 +09:00
parent 3e7cdb405e
commit 272fd84518
4 changed files with 11 additions and 5 deletions

2
go.mod
View File

@ -1,3 +1,5 @@
module sourcecode.social/reiver/go-opt module sourcecode.social/reiver/go-opt
go 1.18 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 opt package opt
import ( import (
"fmt"
"encoding/json" "encoding/json"
"sourcecode.social/reiver/go-erorr"
) )
var _ json.Marshaler = Nothing[bool]() var _ json.Marshaler = Nothing[bool]()
@ -14,11 +15,11 @@ func (receiver Optional[T]) MarshalJSON() ([]byte, error) {
case bool, string, json.Marshaler: case bool, string, json.Marshaler:
// these are OK. // these are OK.
default: 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("opt: 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() { if receiver.isnothing() {
return nil, fmt.Errorf("cannot marshal opt.Nothing[%T]() into JSON", receiver.value) return nil, erorr.Errorf("opt: cannot marshal opt.Nothing[%T]() into JSON", receiver.value)
} }
return json.Marshal(receiver.value) return json.Marshal(receiver.value)

View File

@ -1,8 +1,9 @@
package opt package opt
import ( import (
"fmt"
"encoding/json" "encoding/json"
"sourcecode.social/reiver/go-erorr"
) )
var _ json.Unmarshaler = new(Optional[bool]) var _ json.Unmarshaler = new(Optional[bool])
@ -14,7 +15,7 @@ func (receiver *Optional[T]) UnmarshalJSON(data []byte) error {
case *bool, *string, json.Unmarshaler: case *bool, *string, json.Unmarshaler:
// these are OK. // these are OK.
default: default:
return fmt.Errorf("cannot unmarshal into something of type %T from JSON because pointer to parameterized type is %T rather than *bool, *string, or json.Unmarshaler", receiver, &receiver.value) return erorr.Errorf("opt: cannot unmarshal into something of type %T from JSON because pointer to parameterized type is %T rather than *bool, *string, or json.Unmarshaler", receiver, &receiver.value)
} }
{ {