Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle null in json #166

Merged
merged 2 commits into from May 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 26 additions & 6 deletions v2/internal/message.go
Expand Up @@ -96,6 +96,22 @@ func (m *Message) unmarshalInterface(v interface{}) error {
return nil
}

type keyTypeErr struct {
key interface{}
}

func (err *keyTypeErr) Error() string {
return fmt.Sprintf("expected key to be a string but got %#v", err.key)
}

type valueTypeErr struct {
value interface{}
}

func (err *valueTypeErr) Error() string {
return fmt.Sprintf("unsupported type %#v", err.value)
}

func stringMap(v interface{}) (map[string]string, error) {
switch value := v.(type) {
case string:
Expand All @@ -118,7 +134,7 @@ func stringMap(v interface{}) (map[string]string, error) {
for k, v := range value {
kstr, ok := k.(string)
if !ok {
return nil, fmt.Errorf("expected key to be a string but got %#v", k)
return nil, &keyTypeErr{key: k}
}
err := stringSubmap(kstr, v, strdata)
if err != nil {
Expand All @@ -127,7 +143,7 @@ func stringMap(v interface{}) (map[string]string, error) {
}
return strdata, nil
default:
return nil, fmt.Errorf("unsupported type %#v", value)
return nil, &valueTypeErr{value: value}
}
}

Expand All @@ -147,12 +163,16 @@ func stringSubmap(k string, v interface{}, strdata map[string]string) error {
}
return nil
}
vstr, ok := v.(string)
if !ok {

switch vt := v.(type) {
case string:
strdata[k] = vt
return nil
case nil:
return nil
default:
return fmt.Errorf("expected value for key %q be a string but got %#v", k, v)
}
strdata[k] = vstr
return nil
}

// isMessage tells whether the given data is a message, or a map containing
Expand Down
160 changes: 160 additions & 0 deletions v2/internal/message_test.go
@@ -0,0 +1,160 @@
package internal

import (
"reflect"
"testing"
)

func TestNewMessage(t *testing.T) {
tests := []struct {
name string
data interface{}
message *Message
err error
}{
{
name: "string",
data: "other",
message: &Message{
Other: "other",
},
},
{
name: "nil value",
data: map[interface{}]interface{}{
"ID": "id",
"Zero": nil,
"Other": "other",
},
message: &Message{
ID: "id",
Other: "other",
},
},
{
name: "map[string]string",
data: map[string]string{
"ID": "id",
"Hash": "hash",
"Description": "description",
"LeftDelim": "leftdelim",
"RightDelim": "rightdelim",
"Zero": "zero",
"One": "one",
"Two": "two",
"Few": "few",
"Many": "many",
"Other": "other",
},
message: &Message{
ID: "id",
Hash: "hash",
Description: "description",
LeftDelim: "leftdelim",
RightDelim: "rightdelim",
Zero: "zero",
One: "one",
Two: "two",
Few: "few",
Many: "many",
Other: "other",
},
},
{
name: "map[string]interface{}",
data: map[string]interface{}{
"ID": "id",
"Hash": "hash",
"Description": "description",
"LeftDelim": "leftdelim",
"RightDelim": "rightdelim",
"Zero": "zero",
"One": "one",
"Two": "two",
"Few": "few",
"Many": "many",
"Other": "other",
},
message: &Message{
ID: "id",
Hash: "hash",
Description: "description",
LeftDelim: "leftdelim",
RightDelim: "rightdelim",
Zero: "zero",
One: "one",
Two: "two",
Few: "few",
Many: "many",
Other: "other",
},
},
{
name: "map[interface{}]interface{}",
data: map[interface{}]interface{}{
"ID": "id",
"Hash": "hash",
"Description": "description",
"LeftDelim": "leftdelim",
"RightDelim": "rightdelim",
"Zero": "zero",
"One": "one",
"Two": "two",
"Few": "few",
"Many": "many",
"Other": "other",
},
message: &Message{
ID: "id",
Hash: "hash",
Description: "description",
LeftDelim: "leftdelim",
RightDelim: "rightdelim",
Zero: "zero",
One: "one",
Two: "two",
Few: "few",
Many: "many",
Other: "other",
},
},
{
name: "map[int]int",
data: map[interface{}]interface{}{
1: 2,
},
err: &keyTypeErr{key: 1},
},
{
name: "int",
data: 1,
err: &valueTypeErr{value: 1},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual, err := NewMessage(test.data)
if !reflect.DeepEqual(err, test.err) {
t.Fatalf("expected %#v; got %#v", test.err, err)
}
if !reflect.DeepEqual(actual, test.message) {
t.Fatalf("\nexpected\n%#v\ngot\n%#v", test.message, actual)
}
})
}
}

func TestKeyTypeErr(t *testing.T) {
expected := "expected key to be a string but got 1"
if actual := (&keyTypeErr{key: 1}).Error(); actual != expected {
t.Fatalf("expected %#v; got %#v", expected, actual)
}
}

func TestValueTypeErr(t *testing.T) {
expected := "unsupported type 1"
if actual := (&valueTypeErr{value: 1}).Error(); actual != expected {
t.Fatalf("expected %#v; got %#v", expected, actual)
}
}