made it so UnmarhsalJSON works with *int64 *uint64 *json.Number
parent
6b4ca5231f
commit
afc1a40c4b
|
@ -18,7 +18,7 @@ func (receiver *Optional[T]) UnmarshalJSON(data []byte) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
switch interface{}(&receiver.value).(type) {
|
switch interface{}(&receiver.value).(type) {
|
||||||
case *bool, *string, json.Unmarshaler:
|
case *bool, *int64, *uint64, *string, *json.Number, json.Unmarshaler:
|
||||||
// these are OK.
|
// these are OK.
|
||||||
default:
|
default:
|
||||||
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)
|
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)
|
||||||
|
|
|
@ -0,0 +1,82 @@
|
||||||
|
package opt_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
|
"sourcecode.social/reiver/go-opt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestOptional_UnmarshalJSON_int64(t *testing.T) {
|
||||||
|
|
||||||
|
tests := []struct{
|
||||||
|
JSON string
|
||||||
|
Expected opt.Optional[int64]
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
JSON: `-255`,
|
||||||
|
Expected: opt.Something(int64(-255)),
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
{
|
||||||
|
JSON: `-1`,
|
||||||
|
Expected: opt.Something(int64(-1)),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
JSON: `0`,
|
||||||
|
Expected: opt.Something(int64(0)),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
JSON: `1`,
|
||||||
|
Expected: opt.Something(int64(1)),
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
{
|
||||||
|
JSON: `255`,
|
||||||
|
Expected: opt.Something(int64(255)),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for testNumber, test := range tests {
|
||||||
|
|
||||||
|
var actual opt.Optional[int64]
|
||||||
|
|
||||||
|
err := json.Unmarshal([]byte(test.JSON), &actual)
|
||||||
|
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("JSON: %q", test.JSON)
|
||||||
|
t.Logf("EXPECTED: %#v", test.Expected)
|
||||||
|
t.Logf("ACTUAL: %#v", actual)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
expected := test.Expected
|
||||||
|
|
||||||
|
if expected != actual {
|
||||||
|
t.Errorf("For test #%d, the actual value of the optional type is not what was expected.", testNumber)
|
||||||
|
t.Logf("EXPECTED: %#v", expected)
|
||||||
|
t.Logf("ACTUAL: %#v", actual)
|
||||||
|
t.Logf("JSON: %q", test.JSON)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOptional_UnmarshalJSON_int64_fail(t *testing.T) {
|
||||||
|
|
||||||
|
var op opt.Optional[int64]
|
||||||
|
|
||||||
|
var jason string = `false`
|
||||||
|
|
||||||
|
err := json.Unmarshal([]byte(jason), &op)
|
||||||
|
if nil == err {
|
||||||
|
t.Errorf("Expected an error but did not actually get one.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,72 @@
|
||||||
|
package opt_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
|
"sourcecode.social/reiver/go-opt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestOptional_UnmarshalJSON_uint64(t *testing.T) {
|
||||||
|
|
||||||
|
tests := []struct{
|
||||||
|
JSON string
|
||||||
|
Expected opt.Optional[uint64]
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
JSON: `0`,
|
||||||
|
Expected: opt.Something(uint64(0)),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
JSON: `1`,
|
||||||
|
Expected: opt.Something(uint64(1)),
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
{
|
||||||
|
JSON: `255`,
|
||||||
|
Expected: opt.Something(uint64(255)),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for testNumber, test := range tests {
|
||||||
|
|
||||||
|
var actual opt.Optional[uint64]
|
||||||
|
|
||||||
|
err := json.Unmarshal([]byte(test.JSON), &actual)
|
||||||
|
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("JSON: %q", test.JSON)
|
||||||
|
t.Logf("EXPECTED: %#v", test.Expected)
|
||||||
|
t.Logf("ACTUAL: %#v", actual)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
expected := test.Expected
|
||||||
|
|
||||||
|
if expected != actual {
|
||||||
|
t.Errorf("For test #%d, the actual value of the optional type is not what was expected.", testNumber)
|
||||||
|
t.Logf("EXPECTED: %#v", expected)
|
||||||
|
t.Logf("ACTUAL: %#v", actual)
|
||||||
|
t.Logf("JSON: %q", test.JSON)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOptional_UnmarshalJSON_uint64_fail(t *testing.T) {
|
||||||
|
|
||||||
|
var op opt.Optional[uint64]
|
||||||
|
|
||||||
|
var jason string = `false`
|
||||||
|
|
||||||
|
err := json.Unmarshal([]byte(jason), &op)
|
||||||
|
if nil == err {
|
||||||
|
t.Errorf("Expected an error but did not actually get one.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue