Unverified Commit 90f810b3 authored by keks's avatar keks Committed by GitHub

Merge pull request #36 from ipfs/fix/maybeerror-nil-value

MaybeError: add support for untyped parsing
parents 2bf1c52c eba6003d
......@@ -31,7 +31,7 @@ type anyTestCase struct {
func TestMaybeError(t *testing.T) {
testcases := []anyTestCase{
anyTestCase{
{
Value: &Foo{},
JSON: `{"Bar":23}{"Bar":42}{"Message":"some error", "Type": "error"}`,
Decoded: []ValueError{
......@@ -40,7 +40,7 @@ func TestMaybeError(t *testing.T) {
ValueError{Error: nil, Value: cmdkit.Error{Message: "some error", Code: 0}},
},
},
anyTestCase{
{
Value: Foo{},
JSON: `{"Bar":23}{"Bar":42}{"Message":"some error", "Type": "error"}`,
Decoded: []ValueError{
......@@ -49,7 +49,7 @@ func TestMaybeError(t *testing.T) {
ValueError{Error: nil, Value: cmdkit.Error{Message: "some error", Code: 0}},
},
},
anyTestCase{
{
Value: &Bar{},
JSON: `{"Foo":""}{"Foo":"Qmabc"}{"Message":"some error", "Type": "error"}`,
Decoded: []ValueError{
......@@ -58,7 +58,7 @@ func TestMaybeError(t *testing.T) {
ValueError{Error: nil, Value: cmdkit.Error{Message: "some error", Code: 0}},
},
},
anyTestCase{
{
Value: Bar{},
JSON: `{"Foo":""}{"Foo":"Qmabc"}{"Message":"some error", "Type": "error"}`,
Decoded: []ValueError{
......@@ -67,10 +67,18 @@ func TestMaybeError(t *testing.T) {
ValueError{Error: nil, Value: cmdkit.Error{Message: "some error", Code: 0}},
},
},
{
JSON: `{"Foo":"bar", "i": 4}"some string"5{"Message":"some error", "Type": "error"}`,
Decoded: []ValueError{
ValueError{Error: nil, Value: map[string]interface{}{"Foo": "bar", "i": 4.0}},
ValueError{Error: nil, Value: "some string"},
ValueError{Error: nil, Value: 5.0},
ValueError{Error: nil, Value: cmdkit.Error{Message: "some error", Code: 0}},
},
},
}
for _, tc := range testcases {
m := &MaybeError{Value: tc.Value}
r := strings.NewReader(tc.JSON)
d := json.NewDecoder(r)
......@@ -78,31 +86,22 @@ func TestMaybeError(t *testing.T) {
var err error
for _, dec := range tc.Decoded {
m := &MaybeError{Value: tc.Value}
err = d.Decode(m)
if err != dec.Error {
t.Fatalf("error is %v, expected %v", err, dec.Error)
}
rx := m.Get()
rxIsPtr := reflect.TypeOf(rx).Kind() == reflect.Ptr
ex := dec.Value
exIsPtr := reflect.TypeOf(ex).Kind() == reflect.Ptr
if rxIsPtr != exIsPtr {
t.Fatalf("value is %#v, expected %#v", m.Get(), dec.Value)
}
if rxIsPtr {
rx = reflect.ValueOf(rx).Elem().Interface()
ex = reflect.ValueOf(ex).Elem().Interface()
}
if rx != ex {
t.Fatalf("value is %#v, expected %#v", m.Get(), dec.Value)
if !reflect.DeepEqual(ex, rx) {
t.Errorf("value is %#v(%T), expected %#v(%T)", rx, rx, ex, ex)
}
}
m := &MaybeError{Value: tc.Value}
err = d.Decode(m)
if err != io.EOF {
t.Fatal("data left in decoder:", m.Get())
......
......@@ -188,11 +188,18 @@ func (m *MaybeError) UnmarshalJSON(data []byte) error {
return nil
}
// make sure we are working with a pointer here
v := reflect.ValueOf(m.Value)
if v.Kind() != reflect.Ptr {
m.Value = reflect.New(v.Type()).Interface()
if m.Value != nil {
// make sure we are working with a pointer here
v := reflect.ValueOf(m.Value)
if v.Kind() != reflect.Ptr {
m.Value = reflect.New(v.Type()).Interface()
}
err = json.Unmarshal(data, m.Value)
} else {
// let the json decoder decode into whatever it finds appropriate
err = json.Unmarshal(data, &m.Value)
}
return json.Unmarshal(data, m.Value)
return err
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment