Skip to content

Commit c8c1222

Browse files
committed
Merge branch 'master' into return-type-check
2 parents 7227463 + b347fbf commit c8c1222

File tree

9 files changed

+54
-33
lines changed

9 files changed

+54
-33
lines changed

.github/pull_request_template.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fixes #.
2+
3+
For every PR, please check the following:
4+
- [ ] End-user documentation check. If this PR requires end-user documentation in the Julia VS Code extension docs, please add that at https://github.com/julia-vscode/docs.
5+
- [ ] Changelog mention. If this PR should be mentioned in the CHANGELOG for the Julia VS Code extension, please open a PR against https://github.com/julia-vscode/julia-vscode/blob/master/CHANGELOG.md with those changes.

.github/workflows/jlpkgbutler-ci-master-workflow.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
runs-on: ${{ matrix.os }}
1111
strategy:
1212
matrix:
13-
julia-version: ['1.0', '1.1', '1.2', '1.3', '1.4']
13+
julia-version: ['1.0', '1.1', '1.2', '1.3', '1.4', '1.5']
1414
julia-arch: [x64, x86]
1515
os: [ubuntu-latest, windows-latest, macOS-latest]
1616
exclude:

.github/workflows/jlpkgbutler-ci-pr-workflow.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
runs-on: ${{ matrix.os }}
1010
strategy:
1111
matrix:
12-
julia-version: ['1.0', '1.1', '1.2', '1.3', '1.4']
12+
julia-version: ['1.0', '1.1', '1.2', '1.3', '1.4', '1.5']
1313
julia-arch: [x64, x86]
1414
os: [ubuntu-latest, windows-latest, macOS-latest]
1515
exclude:

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "JSONRPC"
22
uuid = "b9b8584e-8fd3-41f9-ad0c-7255d428e418"
33
authors = ["David Anthoff <anthoff@berkeley.edu>"]
4-
version = "1.1.1-DEV"
4+
version = "1.3.1-DEV"
55

66
[deps]
77
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"

src/core.jl

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ function Base.showerror(io::IO, ex::JSONRPCError)
4141
end
4242
end
4343

44-
mutable struct JSONRPCEndpoint
45-
pipe_in
46-
pipe_out
44+
mutable struct JSONRPCEndpoint{IOIn <: IO,IOOut <: IO}
45+
pipe_in::IOIn
46+
pipe_out::IOOut
4747

4848
out_msg_queue::Channel{Any}
4949
in_msg_queue::Channel{Any}
@@ -56,12 +56,11 @@ mutable struct JSONRPCEndpoint
5656

5757
read_task::Union{Nothing,Task}
5858
write_task::Union{Nothing,Task}
59-
60-
function JSONRPCEndpoint(pipe_in, pipe_out, err_handler = nothing)
61-
return new(pipe_in, pipe_out, Channel{Any}(Inf), Channel{Any}(Inf), Dict{String,Channel{Any}}(), err_handler, :idle, nothing, nothing)
62-
end
6359
end
6460

61+
JSONRPCEndpoint(pipe_in, pipe_out, err_handler = nothing) =
62+
JSONRPCEndpoint(pipe_in, pipe_out, Channel{Any}(Inf), Channel{Any}(Inf), Dict{String,Channel{Any}}(), err_handler, :idle, nothing, nothing)
63+
6564
function write_transport_layer(stream, response)
6665
response_utf8 = transcode(UInt8, response)
6766
n = length(response_utf8)
@@ -86,12 +85,19 @@ function read_transport_layer(stream)
8685
return message_str
8786
end
8887

88+
Base.isopen(x::JSONRPCEndpoint) = x.status != :closed && isopen(x.pipe_in) && isopen(x.pipe_out)
89+
8990
function Base.run(x::JSONRPCEndpoint)
9091
x.status == :idle || error("Endpoint is not idle.")
9192

9293
x.write_task = @async try
9394
for msg in x.out_msg_queue
94-
write_transport_layer(x.pipe_out, msg)
95+
if isopen(x.pipe_out)
96+
write_transport_layer(x.pipe_out, msg)
97+
else
98+
# TODO Reconsider at some point whether this should be treated as an error.
99+
break
100+
end
95101
end
96102
catch err
97103
bt = catch_backtrace()
@@ -143,7 +149,7 @@ function Base.run(x::JSONRPCEndpoint)
143149
end
144150

145151
function send_notification(x::JSONRPCEndpoint, method::AbstractString, params)
146-
x.status == :running || error("Endpoint is not running.")
152+
check_dead_endpoint!(x)
147153

148154
message = Dict("jsonrpc" => "2.0", "method" => method, "params" => params)
149155

@@ -155,7 +161,7 @@ function send_notification(x::JSONRPCEndpoint, method::AbstractString, params)
155161
end
156162

157163
function send_request(x::JSONRPCEndpoint, method::AbstractString, params)
158-
x.status == :running || error("Endpoint is not running.")
164+
check_dead_endpoint!(x)
159165

160166
id = string(UUIDs.uuid4())
161167
message = Dict("jsonrpc" => "2.0", "method" => method, "params" => params, "id" => id)
@@ -182,15 +188,15 @@ function send_request(x::JSONRPCEndpoint, method::AbstractString, params)
182188
end
183189

184190
function get_next_message(endpoint::JSONRPCEndpoint)
185-
endpoint.status == :running || error("Endpoint is not running.")
191+
check_dead_endpoint!(endpoint)
186192

187193
msg = take!(endpoint.in_msg_queue)
188194

189195
return msg
190196
end
191197

192198
function Base.iterate(endpoint::JSONRPCEndpoint, state = nothing)
193-
endpoint.status == :running || error("Endpoint is not running.")
199+
check_dead_endpoint!(endpoint)
194200

195201
try
196202
return take!(endpoint.in_msg_queue), nothing
@@ -204,7 +210,7 @@ function Base.iterate(endpoint::JSONRPCEndpoint, state = nothing)
204210
end
205211

206212
function send_success_response(endpoint, original_request, result)
207-
endpoint.status == :running || error("Endpoint is not running.")
213+
check_dead_endpoint!(endpoint)
208214

209215
response = Dict("jsonrpc" => "2.0", "id" => original_request["id"], "result" => result)
210216

@@ -214,7 +220,7 @@ function send_success_response(endpoint, original_request, result)
214220
end
215221

216222
function send_error_response(endpoint, original_request, code, message, data)
217-
endpoint.status == :running || error("Endpoint is not running.")
223+
check_dead_endpoint!(endpoint)
218224

219225
response = Dict("jsonrpc" => "2.0", "id" => original_request["id"], "error" => Dict("code" => code, "message" => message, "data" => data))
220226

@@ -224,11 +230,7 @@ function send_error_response(endpoint, original_request, code, message, data)
224230
end
225231

226232
function Base.close(endpoint::JSONRPCEndpoint)
227-
endpoint.status == :running || error("Endpoint is not running.")
228-
229-
while isready(endpoint.out_msg_queue)
230-
yield()
231-
end
233+
flush(endpoint)
232234

233235
endpoint.status = :closed
234236
close(endpoint.in_msg_queue)
@@ -240,3 +242,17 @@ function Base.close(endpoint::JSONRPCEndpoint)
240242
# the socket, which we don't want to do
241243
# fetch(endpoint.read_task)
242244
end
245+
246+
function Base.flush(endpoint::JSONRPCEndpoint)
247+
check_dead_endpoint!(endpoint)
248+
249+
while isready(endpoint.out_msg_queue)
250+
yield()
251+
end
252+
end
253+
254+
function check_dead_endpoint!(endpoint)
255+
status = endpoint.status
256+
status === :running && return
257+
error("Endpoint is not running, the current state is $(status).")
258+
end

src/interface_def.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ end
1616
function field_allows_missing(field::Expr)
1717
field.head == :(::) && field.args[2] isa Expr &&
1818
field.args[2].head == :curly && field.args[2].args[1] == :Union &&
19-
any(i->i == :Missing, field.args[2].args)
19+
any(i -> i == :Missing, field.args[2].args)
2020
end
2121

2222
function field_type(field::Expr, typename::String)
@@ -42,7 +42,7 @@ end
4242

4343
macro dict_readable(arg)
4444
tname = arg.args[2] isa Expr ? arg.args[2].args[1] : arg.args[2]
45-
count_real_fields = count(field->!(field isa LineNumberNode), arg.args[3].args)
45+
count_real_fields = count(field -> !(field isa LineNumberNode), arg.args[3].args)
4646
ex = quote
4747
$((arg))
4848

src/typed.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function dispatch_msg(x::JSONRPCEndpoint, dispatcher::MsgDispatcher, msg)
6262
handler = get(dispatcher._handlers, method_name, nothing)
6363
if handler !== nothing
6464
param_type = get_param_type(handler.message_type)
65-
params = param_type === Nothing ? nothing : param_type(msg["params"])
65+
params = param_type === Nothing ? nothing : param_type <: NamedTuple ? convert(param_type,(;(Symbol(i[1])=>i[2] for i in msg["params"])...)) : param_type(msg["params"])
6666

6767
res = handler.func(x, params)
6868

test/test_interface_def.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
@test_throws ErrorException Foo()
44

5-
a = Foo(fieldA = 1, fieldB = "A")
5+
a = Foo(fieldA=1, fieldB="A")
66

77
@test a.fieldA == 1
88
@test a.fieldB == "A"
99
@test a.fieldC === missing
1010
@test a.fieldD === missing
1111

12-
b = Foo(fieldA = 1, fieldB = "A", fieldC = "B", fieldD = "C")
12+
b = Foo(fieldA=1, fieldB="A", fieldC="B", fieldD="C")
1313

1414
@test b.fieldA == 1
1515
@test b.fieldB == "A"
@@ -19,13 +19,13 @@
1919
@test Foo(JSON.parse(JSON.json(a))) == a
2020
@test Foo(JSON.parse(JSON.json(b))) == b
2121

22-
c = Foo2(fieldA = nothing, fieldB = [1,2])
22+
c = Foo2(fieldA=nothing, fieldB=[1,2])
2323

2424
@test c.fieldA === nothing
2525
@test c.fieldB == [1,2]
2626
@test Foo2(JSON.parse(JSON.json(c))) == c
2727

28-
d = Foo2(fieldA = 3, fieldB = [1,2])
28+
d = Foo2(fieldA=3, fieldB=[1,2])
2929
@test d.fieldA === 3
3030
@test d.fieldB == [1,2]
3131
@test Foo2(JSON.parse(JSON.json(d))) == d

test/test_typed.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
global conn = JSONRPC.JSONRPCEndpoint(sock, sock)
2424
global msg_dispatcher = JSONRPC.MsgDispatcher()
2525

26-
msg_dispatcher[request1_type] = (conn, params)->begin
26+
msg_dispatcher[request1_type] = (conn, params) -> begin
2727
@test JSONRPC.is_currently_handling_msg(msg_dispatcher)
2828
params.fieldA == 1 ? "YES" : "NO"
2929
end
30-
msg_dispatcher[request2_type] = (conn, params)->JSONRPC.JSONRPCError(-32600, "Our message", nothing)
31-
msg_dispatcher[notify1_type] = (conn, params)->g_var = params
30+
msg_dispatcher[request2_type] = (conn, params) -> JSONRPC.JSONRPCError(-32600, "Our message", nothing)
31+
msg_dispatcher[notify1_type] = (conn, params) -> g_var = params
3232

3333
run(conn)
3434

@@ -46,7 +46,7 @@
4646

4747
JSONRPC.send(conn2, notify1_type, "TEST")
4848

49-
res = JSONRPC.send(conn2, request1_type, Foo(fieldA = 1, fieldB = "FOO"))
49+
res = JSONRPC.send(conn2, request1_type, Foo(fieldA=1, fieldB="FOO"))
5050

5151
@test res == "YES"
5252
@test g_var == "TEST"

0 commit comments

Comments
 (0)