good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
J
jrpc
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Trevor Judice
jrpc
Commits
1c88d0e7
Commit
1c88d0e7
authored
Sep 14, 2022
by
a
Browse files
Options
Downloads
Patches
Plain Diff
use jsoniter
parent
32aea540
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
client.go
+5
-4
5 additions, 4 deletions
client.go
http.go
+3
-1
3 additions, 1 deletion
http.go
json.go
+2
-2
2 additions, 2 deletions
json.go
protocol.go
+1
-1
1 addition, 1 deletion
protocol.go
wire.go
+10
-8
10 additions, 8 deletions
wire.go
with
21 additions
and
16 deletions
client.go
+
5
−
4
View file @
1c88d0e7
...
...
@@ -27,6 +27,7 @@ import (
"time"
"git.tuxpa.in/a/zlog/log"
jsoniter
"github.com/json-iterator/go"
)
var
(
...
...
@@ -285,7 +286,7 @@ func (c *Client) call(ctx context.Context, result any, msg *jsonrpcMessage) erro
case
len
(
resp
.
Result
)
==
0
:
return
ErrNoResult
default
:
return
json
.
Unmarshal
(
resp
.
Result
,
&
result
)
return
json
iter
.
Unmarshal
(
resp
.
Result
,
&
result
)
}
}
...
...
@@ -402,7 +403,7 @@ func (c *Client) BatchCallContext(ctx context.Context, b []BatchElem) error {
elem
.
Error
=
ErrNoResult
continue
}
elem
.
Error
=
json
.
Unmarshal
(
resp
.
Result
,
elem
.
Result
)
elem
.
Error
=
json
iter
.
Unmarshal
(
resp
.
Result
,
elem
.
Result
)
}
return
err
...
...
@@ -427,7 +428,7 @@ func (c *Client) newMessage(method string, paramsIn ...any) (*jsonrpcMessage, er
msg
:=
&
jsonrpcMessage
{
ID
:
c
.
nextID
(),
Method
:
method
}
if
paramsIn
!=
nil
{
// prevent sending "params":null
var
err
error
if
msg
.
Params
,
err
=
json
.
Marshal
(
paramsIn
);
err
!=
nil
{
if
msg
.
Params
,
err
=
json
iter
.
Marshal
(
paramsIn
);
err
!=
nil
{
return
nil
,
err
}
}
...
...
@@ -437,7 +438,7 @@ func (c *Client) newMessageP(method string, paramIn any) (*jsonrpcMessage, error
msg
:=
&
jsonrpcMessage
{
ID
:
c
.
nextID
(),
Method
:
method
}
if
paramIn
!=
nil
{
// prevent sending "params":null
var
err
error
if
msg
.
Params
,
err
=
json
.
Marshal
(
paramIn
);
err
!=
nil
{
if
msg
.
Params
,
err
=
json
iter
.
Marshal
(
paramIn
);
err
!=
nil
{
return
nil
,
err
}
}
...
...
This diff is collapsed.
Click to expand it.
http.go
+
3
−
1
View file @
1c88d0e7
...
...
@@ -29,6 +29,8 @@ import (
"net/url"
"sync"
"time"
jsoniter
"github.com/json-iterator/go"
)
const
(
...
...
@@ -177,7 +179,7 @@ func (c *Client) sendBatchHTTP(ctx context.Context, op *requestOp, msgs []*jsonr
}
func
(
hc
*
httpConn
)
doRequest
(
ctx
context
.
Context
,
msg
any
)
(
io
.
ReadCloser
,
error
)
{
body
,
err
:=
json
.
Marshal
(
msg
)
body
,
err
:=
json
iter
.
Marshal
(
msg
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
This diff is collapsed.
Click to expand it.
json.go
+
2
−
2
View file @
1c88d0e7
...
...
@@ -92,7 +92,7 @@ func (msg *jsonrpcMessage) namespace() string {
}
func
(
msg
*
jsonrpcMessage
)
String
()
string
{
b
,
_
:=
json
.
Marshal
(
msg
)
b
,
_
:=
json
iter
.
Marshal
(
msg
)
return
string
(
b
)
}
...
...
@@ -267,7 +267,7 @@ func (c *jsonCodec) closed() <-chan any {
func
parseMessage
(
raw
json
.
RawMessage
)
([]
*
jsonrpcMessage
,
bool
)
{
if
!
isBatch
(
raw
)
{
msgs
:=
[]
*
jsonrpcMessage
{{}}
json
.
Unmarshal
(
raw
,
&
msgs
[
0
])
json
iter
.
Unmarshal
(
raw
,
&
msgs
[
0
])
return
msgs
,
false
}
dec
:=
json
.
NewDecoder
(
bytes
.
NewReader
(
raw
))
...
...
This diff is collapsed.
Click to expand it.
protocol.go
+
1
−
1
View file @
1c88d0e7
...
...
@@ -31,7 +31,7 @@ type Request struct {
func
NewRequest
(
ctx
context
.
Context
,
id
string
,
method
string
,
params
any
)
*
Request
{
r
:=
&
Request
{
ctx
:
ctx
}
pms
,
_
:=
json
.
Marshal
(
params
)
pms
,
_
:=
json
iter
.
Marshal
(
params
)
r
.
msg
=
jsonrpcMessage
{
ID
:
NewStringIDPtr
(
id
),
Method
:
method
,
...
...
This diff is collapsed.
Click to expand it.
wire.go
+
10
−
8
View file @
1c88d0e7
...
...
@@ -3,6 +3,8 @@ package jrpc
import
(
"encoding/json"
"fmt"
jsoniter
"github.com/json-iterator/go"
)
// Version represents a JSON-RPC version.
...
...
@@ -21,13 +23,13 @@ var (
// MarshalJSON implements json.Marshaler.
func
(
version
)
MarshalJSON
()
([]
byte
,
error
)
{
return
json
.
Marshal
(
Version
)
return
json
iter
.
Marshal
(
Version
)
}
// UnmarshalJSON implements json.Unmarshaler.
func
(
version
)
UnmarshalJSON
(
data
[]
byte
)
error
{
version
:=
""
if
err
:=
json
.
Unmarshal
(
data
,
&
version
);
err
!=
nil
{
if
err
:=
json
iter
.
Unmarshal
(
data
,
&
version
);
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to Unmarshal: %w"
,
err
)
}
if
version
!=
Version
{
...
...
@@ -93,12 +95,12 @@ func (id *ID) RawMessage() json.RawMessage {
return
null
}
if
id
.
name
!=
""
{
ans
,
err
:=
json
.
Marshal
(
id
.
name
)
ans
,
err
:=
json
iter
.
Marshal
(
id
.
name
)
if
err
==
nil
{
return
ans
}
}
ans
,
err
:=
json
.
Marshal
(
id
.
number
)
ans
,
err
:=
json
iter
.
Marshal
(
id
.
number
)
if
err
==
nil
{
return
ans
}
...
...
@@ -114,18 +116,18 @@ func (id *ID) MarshalJSON() ([]byte, error) {
return
null
,
nil
}
if
id
.
name
!=
""
{
return
json
.
Marshal
(
id
.
name
)
return
json
iter
.
Marshal
(
id
.
name
)
}
return
json
.
Marshal
(
id
.
number
)
return
json
iter
.
Marshal
(
id
.
number
)
}
// UnmarshalJSON implements json.Unmarshaler.
func
(
id
*
ID
)
UnmarshalJSON
(
data
[]
byte
)
error
{
*
id
=
ID
{}
if
err
:=
json
.
Unmarshal
(
data
,
&
id
.
number
);
err
==
nil
{
if
err
:=
json
iter
.
Unmarshal
(
data
,
&
id
.
number
);
err
==
nil
{
return
nil
}
if
err
:=
json
.
Unmarshal
(
data
,
&
id
.
name
);
err
==
nil
{
if
err
:=
json
iter
.
Unmarshal
(
data
,
&
id
.
name
);
err
==
nil
{
return
nil
}
id
.
null
=
true
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment