json.ErrorEmpty
							parent
							
								
									fc545108d9
								
							
						
					
					
						commit
						506ae7916a
					
				|  | @ -0,0 +1,9 @@ | |||
| package nul | ||||
| 
 | ||||
| import ( | ||||
| 	"github.com/reiver/go-erorr" | ||||
| ) | ||||
| 
 | ||||
| const ( | ||||
| 	errBadReflection = erorr.Error("opt: bad reflection") | ||||
| ) | ||||
							
								
								
									
										9
									
								
								go.mod
								
								
								
								
							
							
						
						
									
										9
									
								
								go.mod
								
								
								
								
							|  | @ -1,5 +1,10 @@ | |||
| module github.com/reiver/go-nul | ||||
| 
 | ||||
| go 1.18 | ||||
| go 1.22.4 | ||||
| 
 | ||||
| require github.com/reiver/go-erorr v0.0.0-20240704145350-0485e21eaa82 | ||||
| require ( | ||||
| 	github.com/reiver/go-erorr v0.0.0-20240801233437-8cbde6d1fa3f | ||||
| 	github.com/reiver/go-json v0.0.0-20240808191545-fa5fbb1bb3f6 | ||||
| ) | ||||
| 
 | ||||
| require github.com/reiver/go-lck v0.0.0-20240808133902-b56df221c39f // indirect | ||||
|  |  | |||
							
								
								
									
										10
									
								
								go.sum
								
								
								
								
							
							
						
						
									
										10
									
								
								go.sum
								
								
								
								
							|  | @ -1,2 +1,8 @@ | |||
| github.com/reiver/go-erorr v0.0.0-20240704145350-0485e21eaa82 h1:xxt7qL+7ZfRysXWXU2MpULOg/zWe5P+Fmw9VyUFCmZE= | ||||
| github.com/reiver/go-erorr v0.0.0-20240704145350-0485e21eaa82/go.mod h1:F0HbBf+Ak2ZlE8YkDW4Y+KxaUmT0KaaIJK6CXY3cJxE= | ||||
| github.com/reiver/go-erorr v0.0.0-20240801233437-8cbde6d1fa3f h1:D1QSxKHm8U73XhjsW3SFLkT0zT5pKJi+1KGboMhY1Rk= | ||||
| github.com/reiver/go-erorr v0.0.0-20240801233437-8cbde6d1fa3f/go.mod h1:F0HbBf+Ak2ZlE8YkDW4Y+KxaUmT0KaaIJK6CXY3cJxE= | ||||
| github.com/reiver/go-json v0.0.0-20240808191545-fa5fbb1bb3f6 h1:Ty3b38DeA5BKi42+3f3o8Bl4zEAKlJsF0ozArGJmo9k= | ||||
| github.com/reiver/go-json v0.0.0-20240808191545-fa5fbb1bb3f6/go.mod h1:IHoQXCaObMGCDoTKko/XzDk8QbpNzepuCLMt7KKCLBs= | ||||
| github.com/reiver/go-lck v0.0.0-20240808133902-b56df221c39f h1:KBVWBoNIM8mHkUR3dP64hm2p2vxoD5xHZ6wUllCjFTc= | ||||
| github.com/reiver/go-lck v0.0.0-20240808133902-b56df221c39f/go.mod h1:PFseSi8S0CBkc+YB6jYWtogPk83LumclnYJZZBQJJhs= | ||||
| github.com/reiver/go-opt v0.0.0-20240808175813-d4de6ca5ee95 h1:yMZeTlabM9bvx9ggghLCU4Djfp+FDcgC94+gyl+vzQE= | ||||
| github.com/reiver/go-opt v0.0.0-20240808175813-d4de6ca5ee95/go.mod h1:iXlSKwDjhyF10Fdd2J5PYKwBFlWgfq7OTRxCHU+yTUY= | ||||
|  |  | |||
|  | @ -1,10 +1,12 @@ | |||
| package nul | ||||
| 
 | ||||
| import ( | ||||
| 	"encoding/json" | ||||
| 	"encoding" | ||||
| 	"fmt" | ||||
| 	"reflect" | ||||
| 
 | ||||
| 	"github.com/reiver/go-erorr" | ||||
| 	"github.com/reiver/go-json" | ||||
| ) | ||||
| 
 | ||||
| var _ json.Marshaler = Nothing[bool]() | ||||
|  | @ -13,16 +15,20 @@ 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,json.Marshaler: | ||||
| 	case json.Marshaler, encoding.TextMarshaler, bool, int, int8, int16, int32, int64, string, uint, uint8, uint16, uint32, uint64: | ||||
| 		// these are OK.
 | ||||
| 	default: | ||||
| 		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) | ||||
| 		var reflectedType reflect.Type = reflect.TypeOf(receiver.value) | ||||
| 		if nil == reflectedType { | ||||
| 			return nil, errBadReflection | ||||
| 		} | ||||
| 		if reflect.Struct != reflectedType.Kind() { | ||||
| 			return nil, erorr.Errorf("nul: cannot marshal something of type %T into JSON because parameterized type is ‘%T’ is not supported", receiver, receiver.value) | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	if receiver.isnothing() { | ||||
| 		return nil, erorr.Errorf("nul: cannot marshal nul.Nothing[%T]() into JSON", receiver.value) | ||||
| 		return nil, json.ErrEmpty(fmt.Sprintf("nul: cannot marshal nul.Nothing[%T]() into JSON", receiver.value)) | ||||
| 	} | ||||
| 
 | ||||
| 	if receiver.isnull { | ||||
|  |  | |||
|  | @ -10,94 +10,125 @@ func TestNullable_MarshalJSON_int(t *testing.T) { | |||
| 
 | ||||
| 	tests := []struct{ | ||||
| 		Value nul.Nullable[int] | ||||
| 		Expected string | ||||
| 	}{ | ||||
| 		{ | ||||
| 			Value: nul.Null[int](), | ||||
| 			Expected: "null", | ||||
| 		}, | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| 		{ | ||||
| 			Value: nul.Something(-65536), | ||||
| 			Expected:           `-65536`, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Value: nul.Something(-65535), | ||||
| 			Expected:           `-65535`, | ||||
| 		}, | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| 		{ | ||||
| 			Value: nul.Something(-256), | ||||
| 			Expected:           `-256`, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Value: nul.Something(-255), | ||||
| 			Expected:           `-255`, | ||||
| 		}, | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| 		{ | ||||
| 			Value: nul.Something(-5), | ||||
| 			Expected:           `-5`, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Value: nul.Something(-4), | ||||
| 			Expected:           `-4`, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Value: nul.Something(-3), | ||||
| 			Expected:           `-3`, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Value: nul.Something(-2), | ||||
| 			Expected:           `-2`, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Value: nul.Something(-1), | ||||
| 			Expected:           `-1`, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Value: nul.Something(0), | ||||
| 			Expected:           `0`, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Value: nul.Something(1), | ||||
| 			Expected:           `1`, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Value: nul.Something(2), | ||||
| 			Expected:           `2`, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Value: nul.Something(3), | ||||
| 			Expected:           `3`, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Value: nul.Something(4), | ||||
| 			Expected:           `4`, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Value: nul.Something(5), | ||||
| 			Expected:           `5`, | ||||
| 		}, | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| 		{ | ||||
| 			Value: nul.Something(255), | ||||
| 			Expected:           `255`, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Value: nul.Something(256), | ||||
| 			Expected:           `256`, | ||||
| 		}, | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| 		{ | ||||
| 			Value: nul.Something(65535), | ||||
| 			Expected:           `65535`, | ||||
| 		}, | ||||
| 		{ | ||||
| 			Value: nul.Something(65536), | ||||
| 			Expected:           `65536`, | ||||
| 		}, | ||||
| 	} | ||||
| 
 | ||||
| 	for testNumber, test := range tests { | ||||
| 
 | ||||
| 		actualBytes, err := test.Value.MarshalJSON() | ||||
| 		if nil == err { | ||||
| 			t.Errorf("For test #%d, expected an error but did not actually get one." , testNumber) | ||||
| 			t.Logf("ACTUAL BYTES: %q", actualBytes) | ||||
| 		if nil != err { | ||||
| 			t.Errorf("For test #%d, did not expect an error but actually got one." , testNumber) | ||||
| 			t.Logf("ERROR: (%T) %s", err, err) | ||||
| 			t.Logf("VALUE: %#v", test.Value) | ||||
| 			continue | ||||
| 		} | ||||
| 
 | ||||
| 		actual := string(actualBytes) | ||||
| 		expected := test.Expected | ||||
| 
 | ||||
| 		if expected != actual { | ||||
| 			t.Errorf("For test #%d, the actual value for the JSON marshaling is not what was expected.", testNumber) | ||||
| 			t.Logf("EXPECTED: %q", expected) | ||||
| 			t.Logf("ACTUAL:   %q", actual) | ||||
| 			t.Logf("VALUE: %#v", test.Value) | ||||
| 			continue | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue