Skip to content

[AUTO] Format files using DocumentFormat #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const RPCErrorStrings = Base.IdDict(
METHOD_NOT_FOUND => "MethodNotFound",
INVALID_PARAMS => "InvalidParams",
INTERNAL_ERROR => "InternalError",
[ i => "ServerError" for i in SERVER_ERROR_START:SERVER_ERROR_END]...,
[i => "ServerError" for i in SERVER_ERROR_START:SERVER_ERROR_END]...,
-32002 => "ServerNotInitialized",
-32001 => "UnknownErrorCode",
)
Expand All @@ -105,7 +105,7 @@ function Base.showerror(io::IO, ex::JSONRPCError)
end
end

mutable struct JSONRPCEndpoint{IOIn <: IO,IOOut <: IO}
mutable struct JSONRPCEndpoint{IOIn<:IO,IOOut<:IO}
pipe_in::IOIn
pipe_out::IOOut

Expand All @@ -122,7 +122,7 @@ mutable struct JSONRPCEndpoint{IOIn <: IO,IOOut <: IO}
write_task::Union{Nothing,Task}
end

JSONRPCEndpoint(pipe_in, pipe_out, err_handler = nothing) =
JSONRPCEndpoint(pipe_in, pipe_out, err_handler=nothing) =
JSONRPCEndpoint(pipe_in, pipe_out, Channel{Any}(Inf), Channel{Any}(Inf), Dict{String,Channel{Any}}(), err_handler, :idle, nothing, nothing)

function write_transport_layer(stream, response)
Expand Down Expand Up @@ -213,7 +213,7 @@ function Base.run(x::JSONRPCEndpoint)
put!(channel_for_response, message_dict)
end
end

close(x.in_msg_queue)

for i in values(x.outstanding_requests)
Expand Down Expand Up @@ -280,7 +280,7 @@ function get_next_message(endpoint::JSONRPCEndpoint)
return msg
end

function Base.iterate(endpoint::JSONRPCEndpoint, state = nothing)
function Base.iterate(endpoint::JSONRPCEndpoint, state=nothing)
check_dead_endpoint!(endpoint)

try
Expand Down
12 changes: 6 additions & 6 deletions src/interface_def.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ end

function field_allows_missing(field::Expr)
field.head == :(::) && field.args[2] isa Expr &&
field.args[2].head == :curly && field.args[2].args[1] == :Union &&
any(i -> i == :Missing, field.args[2].args)
field.args[2].head == :curly && field.args[2].args[1] == :Union &&
any(i -> i == :Missing, field.args[2].args)
end

function field_type(field::Expr, typename::String)
Expand Down Expand Up @@ -55,9 +55,9 @@ macro dict_readable(arg)
$((arg))

$(count_real_fields > 0 ? :(
function $tname(; $((get_kwsignature_for_field(field) for field in arg.args[3].args if !(field isa LineNumberNode))...))
$tname($((field.args[1] for field in arg.args[3].args if !(field isa LineNumberNode))...))
end
function $tname(; $((get_kwsignature_for_field(field) for field in arg.args[3].args if !(field isa LineNumberNode))...))
$tname($((field.args[1] for field in arg.args[3].args if !(field isa LineNumberNode))...))
end
) : nothing)

function $tname(dict::Dict)
Expand All @@ -69,7 +69,7 @@ macro dict_readable(arg)
if !(field isa LineNumberNode)
fieldname = string(field.args[1])
fieldtype = field_type(field, string(tname))
if fieldtype isa Expr && fieldtype.head == :curly && length(fieldtype.args)==3 && fieldtype.args[1]==:Dict
if fieldtype isa Expr && fieldtype.head == :curly && length(fieldtype.args) == 3 && fieldtype.args[1] == :Dict
f = :($(fieldtype)(i for i in pairs(dict[$fieldname])))
elseif fieldtype isa Expr && fieldtype.head == :curly && fieldtype.args[2] != :Any
f = :($(fieldtype.args[2]).(dict[$fieldname]))
Expand Down
14 changes: 7 additions & 7 deletions src/typed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ end
# so that we get an error in the typecast at the end of `send`
# if that is not the case.
typed_res(res, TR::Type{Nothing}) = res
typed_res(res, TR::Type{<:T}) where {T <: AbstractArray{Any}} = T(res)
typed_res(res, TR::Type{<:T}) where {T<:AbstractArray{Any}} = T(res)
typed_res(res, TR::Type{<:AbstractArray{T}}) where T = T.(res)
typed_res(res, TR::Type) = TR(res)

Expand Down Expand Up @@ -62,7 +62,7 @@ function dispatch_msg(x::JSONRPCEndpoint, dispatcher::MsgDispatcher, msg)
handler = get(dispatcher._handlers, method_name, nothing)
if handler !== nothing
param_type = get_param_type(handler.message_type)
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"])
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"])

res = handler.func(x, params)

Expand All @@ -73,7 +73,7 @@ function dispatch_msg(x::JSONRPCEndpoint, dispatcher::MsgDispatcher, msg)
send_success_response(x, msg, res)
else
error_msg = "The handler for the '$method_name' request returned a value of type $(typeof(res)), which is not a valid return type according to the request definition."
send_error_response(x, msg, -32603, error_msg, nothing)
send_error_response(x, msg, -32603, error_msg, nothing)
error(error_msg)
end
end
Expand All @@ -97,9 +97,9 @@ macro message_dispatcher(name, body)
:(
if method_name == $(esc(i.args[2])).method
param_type = get_param_type($(esc(i.args[2])))
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"])
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"])

if context===nothing
if context === nothing
res = $(esc(i.args[3]))(x, params)
else
res = $(esc(i.args[3]))(x, params, context)
Expand All @@ -112,14 +112,14 @@ macro message_dispatcher(name, body)
send_success_response(x, msg, res)
else
error_msg = "The handler for the '$method_name' request returned a value of type $(typeof(res)), which is not a valid return type according to the request definition."
send_error_response(x, msg, -32603, error_msg, nothing)
send_error_response(x, msg, -32603, error_msg, nothing)
error(error_msg)
end
end

return
end
) for i in filter(i->i isa Expr, body.args)
) for i in filter(i -> i isa Expr, body.args)
)...
)

Expand Down
2 changes: 1 addition & 1 deletion test/shared_test_code.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
fieldB::Vector{Int}
end

Base.:(==)(a::Foo2,b::Foo2) = a.fieldA == b.fieldA && a.fieldB == b.fieldB
Base.:(==)(a::Foo2, b::Foo2) = a.fieldA == b.fieldA && a.fieldB == b.fieldB

end
12 changes: 6 additions & 6 deletions test/test_interface_def.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@testitem "Interface Definition" setup=[TestStructs] begin
@testitem "Interface Definition" setup = [TestStructs] begin
using JSON
using .TestStructs: Foo, Foo2

@test_throws ErrorException Foo()

a = Foo(fieldA=1, fieldB="A")
Expand All @@ -21,14 +21,14 @@
@test Foo(JSON.parse(JSON.json(a))) == a
@test Foo(JSON.parse(JSON.json(b))) == b

c = Foo2(fieldA=nothing, fieldB=[1,2])
c = Foo2(fieldA=nothing, fieldB=[1, 2])

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

d = Foo2(fieldA=3, fieldB=[1,2])
d = Foo2(fieldA=3, fieldB=[1, 2])
@test d.fieldA === 3
@test d.fieldB == [1,2]
@test d.fieldB == [1, 2]
@test Foo2(JSON.parse(JSON.json(d))) == d
end
18 changes: 9 additions & 9 deletions test/test_typed.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@testitem "Dynamic message dispatcher" setup=[TestStructs] begin
@testitem "Dynamic message dispatcher" setup = [TestStructs] begin
using Sockets
using .TestStructs: Foo, Foo2

Expand Down Expand Up @@ -70,7 +70,7 @@
global conn = JSONRPC.JSONRPCEndpoint(sock, sock)
global msg_dispatcher = JSONRPC.MsgDispatcher()

msg_dispatcher[request2_type] = (conn, params)->34 # The request type requires a `String` return, so this tests whether we get an error.
msg_dispatcher[request2_type] = (conn, params) -> 34 # The request type requires a `String` return, so this tests whether we get an error.

run(conn)

Expand Down Expand Up @@ -100,19 +100,19 @@ end

@testitem "check response type" begin
using JSONRPC: typed_res

@test typed_res(nothing, Nothing) isa Nothing
@test typed_res([1,"2",3], Vector{Any}) isa Vector{Any}
@test typed_res([1,2,3], Vector{Int}) isa Vector{Int}
@test typed_res([1,2,3], Vector{Float64}) isa Vector{Float64}
@test typed_res(['f','o','o'], String) isa String
@test typed_res([1, "2", 3], Vector{Any}) isa Vector{Any}
@test typed_res([1, 2, 3], Vector{Int}) isa Vector{Int}
@test typed_res([1, 2, 3], Vector{Float64}) isa Vector{Float64}
@test typed_res(['f', 'o', 'o'], String) isa String
@test typed_res("foo", String) isa String
end

@testitem "Static message dispatcher" setup=[TestStructs] begin
@testitem "Static message dispatcher" setup = [TestStructs] begin
using Sockets
using .TestStructs: Foo, Foo2

global_socket_name1 = JSONRPC.generate_pipe_name()

request1_type = JSONRPC.RequestType("request1", Foo, String)
Expand Down