Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3-cmds
Commits
90f810b3
Unverified
Commit
90f810b3
authored
Dec 07, 2017
by
keks
Committed by
GitHub
Dec 07, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #36 from ipfs/fix/maybeerror-nil-value
MaybeError: add support for untyped parsing
parents
2bf1c52c
eba6003d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
24 deletions
+30
-24
maybeerror_test.go
maybeerror_test.go
+18
-19
writer.go
writer.go
+12
-5
No files found.
maybeerror_test.go
View file @
90f810b3
...
...
@@ -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
())
...
...
writer.go
View file @
90f810b3
...
...
@@ -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
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment