.UnmarshalJSON()

master
Charles Iliya Krempeaux 2023-09-24 06:27:34 +09:00
parent a8488bd6fb
commit e38043bd89
4 changed files with 336 additions and 0 deletions

View File

@ -0,0 +1,32 @@
package opt
import (
"fmt"
"encoding/json"
)
var _ json.Unmarshaler = new(Optional[bool])
var _ json.Unmarshaler = new(Optional[string])
// UnmarshalJSON makes it so json.Unmarshaler is implemented.
func (receiver *Optional[T]) UnmarshalJSON(data []byte) error {
switch interface{}(receiver.value).(type) {
case bool, string:
// these are OK.
default:
return fmt.Errorf("cannot unmarshal into something of type %T from JSON because parameterized type is %T rather than bool or string", receiver, receiver.value)
}
{
var dst T
err := json.Unmarshal(data, &dst)
if nil != err {
return err
}
*receiver = Something(dst)
}
return nil
}

View File

@ -0,0 +1,66 @@
package opt_test
import (
"testing"
"encoding/json"
"sourcecode.social/reiver/go-opt"
)
func TestOptional_UnmarshalJSON_bool(t *testing.T) {
tests := []struct{
JSON string
Expected opt.Optional[bool]
}{
{
JSON: `false`,
Expected: opt.Something(false),
},
{
JSON: `true`,
Expected: opt.Something(true),
},
}
for testNumber, test := range tests {
var actual opt.Optional[bool]
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_bool_fail(t *testing.T) {
var op opt.Optional[bool]
var jason string = "12345"
err := json.Unmarshal([]byte(jason), &op)
if nil == err {
t.Errorf("Expected an error but did not actually get one.")
return
}
}

View File

@ -0,0 +1,113 @@
package opt_test
import (
"testing"
"encoding/json"
"sourcecode.social/reiver/go-opt"
)
func TestOptional_UnmarshalJSON_int(t *testing.T) {
tests := []struct{
JSON string
}{
{
JSON: `-65536`,
},
{
JSON: `-65535`,
},
{
JSON: `-256`,
},
{
JSON: `-255`,
},
{
JSON: `-5`,
},
{
JSON: `-4`,
},
{
JSON: `-3`,
},
{
JSON: `-2`,
},
{
JSON: `-1`,
},
{
JSON: `0`,
},
{
JSON: `1`,
},
{
JSON: `2`,
},
{
JSON: `3`,
},
{
JSON: `4`,
},
{
JSON: `5`,
},
{
JSON: `-255`,
},
{
JSON: `-256`,
},
{
JSON: `65535`,
},
{
JSON: `65536`,
},
}
for testNumber, test := range tests {
var actual opt.Optional[int]
err := json.Unmarshal([]byte(test.JSON), &actual)
if nil == err {
t.Errorf("For test #%d, expected an error but did not actually get one.", testNumber)
t.Logf("ERROR: (%T) %s", err, err)
t.Logf("JSON: %q", test.JSON)
t.Logf("ACTUAL: %#v", actual)
continue
}
}
}
func TestOptional_UnmarshalJSON_int_fail(t *testing.T) {
var op opt.Optional[int]
var jason string = "12345"
err := json.Unmarshal([]byte(jason), &op)
if nil == err {
t.Errorf("Expected an error but did not actually get one.")
return
}
}

View File

@ -0,0 +1,125 @@
package opt_test
import (
"testing"
"encoding/json"
"sourcecode.social/reiver/go-opt"
)
func TestOptional_UnmarshalJSON_string(t *testing.T) {
tests := []struct{
JSON string
Expected opt.Optional[string]
}{
{
JSON: `""`,
Expected: opt.Something(""),
},
{
JSON: `"apple"`,
Expected: opt.Something("apple"),
},
{
JSON: `"banana"`,
Expected: opt.Something("banana"),
},
{
JSON: `"cherry"`,
Expected: opt.Something("cherry"),
},
{
JSON: `"ONCE"`,
Expected: opt.Something("ONCE"),
},
{
JSON: `"TWICE"`,
Expected: opt.Something("TWICE"),
},
{
JSON: `"THRICE"`,
Expected: opt.Something("THRICE"),
},
{
JSON: `"FOURCE"`,
Expected: opt.Something("FOURCE"),
},
{
JSON: `"🙂"`,
Expected: opt.Something("🙂"),
},
{
JSON: `"😈"`,
Expected: opt.Something("😈"),
},
{
JSON: `"❤️"`,
Expected: opt.Something("❤️"),
},
{
JSON: `"٠١٢٣۴۵۶٧٨٩"`,
Expected: opt.Something("٠١٢٣۴۵۶٧٨٩"),
},
{
JSON: `"𐏑𐏓𐏕"`,
Expected: opt.Something("𐏑𐏓𐏕"),
},
}
for testNumber, test := range tests {
var actual opt.Optional[string]
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_string_fail(t *testing.T) {
var op opt.Optional[string]
var jason string = "12345"
err := json.Unmarshal([]byte(jason), &op)
if nil == err {
t.Errorf("Expected an error but did not actually get one.")
return
}
}