Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
eb926e2f
Unverified
Commit
eb926e2f
authored
Jan 23, 2022
by
Matthew Slipper
Committed by
GitHub
Jan 23, 2022
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2051 from mslipper/bugfix/issue-2049
go/proxyd: Properly handle null results
parents
21084129
437e853f
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
105 additions
and
0 deletions
+105
-0
rpc.go
go/proxyd/rpc.go
+30
-0
rpc_test.go
go/proxyd/rpc_test.go
+75
-0
No files found.
go/proxyd/rpc.go
View file @
eb926e2f
...
@@ -15,16 +15,46 @@ type RPCReq struct {
...
@@ -15,16 +15,46 @@ type RPCReq struct {
}
}
type
RPCRes
struct
{
type
RPCRes
struct
{
JSONRPC
string
Result
interface
{}
Error
*
RPCErr
ID
json
.
RawMessage
}
type
rpcResJSON
struct
{
JSONRPC
string
`json:"jsonrpc"`
JSONRPC
string
`json:"jsonrpc"`
Result
interface
{}
`json:"result,omitempty"`
Result
interface
{}
`json:"result,omitempty"`
Error
*
RPCErr
`json:"error,omitempty"`
Error
*
RPCErr
`json:"error,omitempty"`
ID
json
.
RawMessage
`json:"id"`
ID
json
.
RawMessage
`json:"id"`
}
}
type
nullResultRPCRes
struct
{
JSONRPC
string
`json:"jsonrpc"`
Result
interface
{}
`json:"result"`
ID
json
.
RawMessage
`json:"id"`
}
func
(
r
*
RPCRes
)
IsError
()
bool
{
func
(
r
*
RPCRes
)
IsError
()
bool
{
return
r
.
Error
!=
nil
return
r
.
Error
!=
nil
}
}
func
(
r
*
RPCRes
)
MarshalJSON
()
([]
byte
,
error
)
{
if
r
.
Result
==
nil
&&
r
.
Error
==
nil
{
return
json
.
Marshal
(
&
nullResultRPCRes
{
JSONRPC
:
r
.
JSONRPC
,
Result
:
nil
,
ID
:
r
.
ID
,
})
}
return
json
.
Marshal
(
&
rpcResJSON
{
JSONRPC
:
r
.
JSONRPC
,
Result
:
r
.
Result
,
Error
:
r
.
Error
,
ID
:
r
.
ID
,
})
}
type
RPCErr
struct
{
type
RPCErr
struct
{
Code
int
`json:"code"`
Code
int
`json:"code"`
Message
string
`json:"message"`
Message
string
`json:"message"`
...
...
go/proxyd/rpc_test.go
0 → 100644
View file @
eb926e2f
package
proxyd
import
(
"encoding/json"
"github.com/stretchr/testify/require"
"testing"
)
func
TestRPCResJSON
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
name
string
in
*
RPCRes
out
string
}{
{
"string result"
,
&
RPCRes
{
JSONRPC
:
JSONRPCVersion
,
Result
:
"foobar"
,
ID
:
[]
byte
(
"123"
),
},
`{"jsonrpc":"2.0","result":"foobar","id":123}`
,
},
{
"object result"
,
&
RPCRes
{
JSONRPC
:
JSONRPCVersion
,
Result
:
struct
{
Str
string
`json:"str"`
}{
"test"
,
},
ID
:
[]
byte
(
"123"
),
},
`{"jsonrpc":"2.0","result":{"str":"test"},"id":123}`
,
},
{
"nil result"
,
&
RPCRes
{
JSONRPC
:
JSONRPCVersion
,
Result
:
nil
,
ID
:
[]
byte
(
"123"
),
},
`{"jsonrpc":"2.0","result":null,"id":123}`
,
},
{
"error result"
,
&
RPCRes
{
JSONRPC
:
JSONRPCVersion
,
Error
:
&
RPCErr
{
Code
:
1234
,
Message
:
"test err"
,
},
ID
:
[]
byte
(
"123"
),
},
`{"jsonrpc":"2.0","error":{"code":1234,"message":"test err"},"id":123}`
,
},
{
"string ID"
,
&
RPCRes
{
JSONRPC
:
JSONRPCVersion
,
Result
:
"foobar"
,
ID
:
[]
byte
(
"
\"
123
\"
"
),
},
`{"jsonrpc":"2.0","result":"foobar","id":"123"}`
,
},
}
for
_
,
tt
:=
range
tests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
out
,
err
:=
json
.
Marshal
(
tt
.
in
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
tt
.
out
,
string
(
out
))
})
}
}
\ No newline at end of file
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