initial commits

master
Charles Iliya Krempeaux 2023-09-24 09:40:03 +09:00
parent 26221eaae3
commit 168345c9e7
1 changed files with 29 additions and 0 deletions

View File

@ -0,0 +1,29 @@
package nul
import (
"fmt"
"encoding/json"
)
var _ json.Marshaler = Nothing[bool]()
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:
// 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)
}
if receiver.isnothing() {
return nil, fmt.Errorf("cannot marshal opt.Nothing[%T]() into JSON", receiver.value)
}
if receiver.isnull {
return json.Marshal(nil)
}
return json.Marshal(receiver.value)
}