From fc545108d9e370c1bd8eb876b3b85f17825fee57 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Wed, 7 Aug 2024 00:37:51 -0700 Subject: [PATCH] struct --- nullable_marshaljson.go | 5 ++++- nullable_unmarshaljson.go | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/nullable_marshaljson.go b/nullable_marshaljson.go index 0d3e199..0b478ef 100644 --- a/nullable_marshaljson.go +++ b/nullable_marshaljson.go @@ -2,6 +2,7 @@ package nul import ( "encoding/json" + "reflect" "github.com/reiver/go-erorr" ) @@ -15,7 +16,9 @@ func (receiver Nullable[T]) MarshalJSON() ([]byte, error) { case bool, string,json.Marshaler: // these are OK. default: - 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 reflect.Struct != reflect.TypeOf(receiver.value).Kind() { + 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() { diff --git a/nullable_unmarshaljson.go b/nullable_unmarshaljson.go index 8aec384..2b9db7c 100644 --- a/nullable_unmarshaljson.go +++ b/nullable_unmarshaljson.go @@ -2,6 +2,7 @@ package nul import ( "encoding/json" + "reflect" "github.com/reiver/go-erorr" ) @@ -21,7 +22,9 @@ func (receiver *Nullable[T]) UnmarshalJSON(data []byte) error { case bool,string,json.Unmarshaler: // these are OK. default: - 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 reflect.Struct != reflect.TypeOf(receiver.value).Kind() { + 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] {