Packages

Work with JSON RPC (jsonrpc 2.0)

Current section

Files

Jump to
pollux src pollux.erl
Raw

src/pollux.erl

-module(pollux).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/pollux.gleam").
-export([id_to_string/1, request_decoder/3, request_encode/3, response_decoder/1, response_encode/2, request/2, notification/1, response/2, result/2]).
-export_type([id/0, request/2, error_object/0, response/1]).
-type id() :: {string_id, binary()} | {number_id, integer()}.
-type request(FHH, FHI) :: {request, binary(), id(), FHH} |
{notification, binary(), FHI}.
-type error_object() :: {error_object,
integer(),
binary(),
oas@generator@utils:any_()}.
-type response(FHJ) :: {response,
binary(),
id(),
{ok, FHJ} | {error, error_object()}}.
-file("src/pollux.gleam", 17).
-spec id_decoder() -> gleam@dynamic@decode:decoder(id()).
id_decoder() ->
gleam@dynamic@decode:one_of(
begin
_pipe = {decoder, fun gleam@dynamic@decode:decode_string/1},
gleam@dynamic@decode:map(
_pipe,
fun(Field@0) -> {string_id, Field@0} end
)
end,
[begin
_pipe@1 = {decoder, fun gleam@dynamic@decode:decode_int/1},
gleam@dynamic@decode:map(
_pipe@1,
fun(Field@0) -> {number_id, Field@0} end
)
end]
).
-file("src/pollux.gleam", 23).
-spec id_encode(id()) -> gleam@json:json().
id_encode(Id) ->
case Id of
{string_id, String} ->
gleam@json:string(String);
{number_id, Number} ->
gleam@json:int(Number)
end.
-file("src/pollux.gleam", 30).
-spec id_to_string(id()) -> binary().
id_to_string(Id) ->
case Id of
{number_id, Id@1} ->
erlang:integer_to_binary(Id@1);
{string_id, Id@2} ->
Id@2
end.
-file("src/pollux.gleam", 51).
-spec request_decoder(
list({binary(), gleam@dynamic@decode:decoder(FLS)}),
list({binary(), gleam@dynamic@decode:decoder(FHW)}),
FHW
) -> gleam@dynamic@decode:decoder(request(FLS, FHW)).
request_decoder(Request_decoders, Notification_decoders, Zero) ->
gleam@dynamic@decode:field(
<<"jsonrpc"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Version) ->
pollux@decodex:optional_field(
<<"id"/utf8>>,
id_decoder(),
fun(Id) ->
gleam@dynamic@decode:field(
<<"method"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Method) -> case Id of
{some, Id@1} ->
case gleam@list:key_find(
Request_decoders,
Method
) of
{ok, Decoder} ->
pollux@decodex:optional_field(
<<"params"/utf8>>,
pollux@decodex:any(),
fun(Maybe) ->
Params = gleam@option:unwrap(
Maybe,
gleam@dynamic:properties(
[]
)
),
case gleam@dynamic@decode:run(
Params,
Decoder
) of
{ok, Value} ->
gleam@dynamic@decode:success(
{request,
Version,
Id@1,
Value}
);
{error, _} ->
gleam@dynamic@decode:failure(
{notification,
Version,
Zero},
<<"params"/utf8>>
)
end
end
);
{error, nil} ->
gleam@dynamic@decode:failure(
{notification, Version, Zero},
<<"missing decoder "/utf8,
Method/binary>>
)
end;
none ->
case gleam@list:key_find(
Notification_decoders,
Method
) of
{ok, Decoder@1} ->
pollux@decodex:optional_field(
<<"params"/utf8>>,
pollux@decodex:any(),
fun(Maybe@1) ->
Params@1 = gleam@option:unwrap(
Maybe@1,
gleam@dynamic:properties(
[]
)
),
case gleam@dynamic@decode:run(
Params@1,
Decoder@1
) of
{ok, Value@1} ->
gleam@dynamic@decode:success(
{notification,
Version,
Value@1}
);
{error, _} ->
gleam@dynamic@decode:failure(
{notification,
Version,
Zero},
<<"params"/utf8>>
)
end
end
);
{error, nil} ->
gleam@dynamic@decode:failure(
{notification, Version, Zero},
<<"missing decoder "/utf8,
Method/binary>>
)
end
end end
)
end
)
end
).
-file("src/pollux.gleam", 95).
-spec request_encode(
request(FMH, FMG),
fun((FMH) -> {binary(), gleam@option:option(gleam@json:json())}),
fun((FMG) -> {binary(), gleam@option:option(gleam@json:json())})
) -> gleam@json:json().
request_encode(Request, Request_encode, Notification_encode) ->
case Request of
{request, Version, Id, Value} ->
{Method, Params} = Request_encode(Value),
gleam@json:object(
[{<<"jsonrpc"/utf8>>, gleam@json:string(Version)},
{<<"id"/utf8>>, id_encode(Id)},
{<<"method"/utf8>>, gleam@json:string(Method)} |
case Params of
none ->
[];
{some, Params@1} ->
[{<<"params"/utf8>>, Params@1}]
end]
);
{notification, Version@1, Value@1} ->
{Method@1, Params@2} = Notification_encode(Value@1),
gleam@json:object(
[{<<"jsonrpc"/utf8>>, gleam@json:string(Version@1)},
{<<"method"/utf8>>, gleam@json:string(Method@1)} |
case Params@2 of
none ->
[];
{some, Params@3} ->
[{<<"params"/utf8>>, Params@3}]
end]
)
end.
-file("src/pollux.gleam", 131).
-spec error_object_decoder() -> gleam@dynamic@decode:decoder(error_object()).
error_object_decoder() ->
gleam@dynamic@decode:field(
<<"code"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Code) ->
gleam@dynamic@decode:field(
<<"message"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Message) ->
gleam@dynamic@decode:optional_field(
<<"data"/utf8>>,
null,
oas@generator@utils:any_decoder(),
fun(Data) ->
gleam@dynamic@decode:success(
{error_object, Code, Message, Data}
)
end
)
end
)
end
).
-file("src/pollux.gleam", 138).
-spec error_object_encode(error_object()) -> gleam@json:json().
error_object_encode(Obj) ->
{error_object, Code, Message, Data} = Obj,
gleam@json:object(
[{<<"code"/utf8>>, gleam@json:int(Code)},
{<<"message"/utf8>>, gleam@json:string(Message)},
{<<"data"/utf8>>, oas@generator@utils:any_to_json(Data)}]
).
-file("src/pollux.gleam", 159).
-spec response_decoder(gleam@dynamic@decode:decoder(FOF)) -> gleam@dynamic@decode:decoder(response(FOF)).
response_decoder(Return_decoder) ->
gleam@dynamic@decode:field(
<<"jsonrpc"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Version) ->
gleam@dynamic@decode:field(
<<"id"/utf8>>,
id_decoder(),
fun(Id) ->
gleam@dynamic@decode:then(
gleam@dynamic@decode:one_of(
begin
gleam@dynamic@decode:field(
<<"result"/utf8>>,
Return_decoder,
fun(Value) ->
gleam@dynamic@decode:success(
{ok, Value}
)
end
)
end,
[begin
gleam@dynamic@decode:field(
<<"error"/utf8>>,
error_object_decoder(),
fun(Reason) ->
gleam@dynamic@decode:success(
{error, Reason}
)
end
)
end]
),
fun(Return) ->
gleam@dynamic@decode:success(
{response, Version, Id, Return}
)
end
)
end
)
end
).
-file("src/pollux.gleam", 179).
-spec response_encode(response(FOR), fun((FOR) -> gleam@json:json())) -> gleam@json:json().
response_encode(Response, Return_encode) ->
{response, Version, Id, Return} = Response,
gleam@json:object(
[{<<"jsonrpc"/utf8>>, gleam@json:string(Version)},
{<<"id"/utf8>>, id_encode(Id)},
case Return of
{ok, Value} ->
{<<"result"/utf8>>, Return_encode(Value)};
{error, Obj} ->
{<<"error"/utf8>>, error_object_encode(Obj)}
end]
).
-file("src/pollux.gleam", 43).
-spec request(id(), FHQ) -> request(FHQ, any()).
request(Id, Value) ->
{request, <<"2.0"/utf8>>, Id, Value}.
-file("src/pollux.gleam", 47).
-spec notification(FHS) -> request(any(), FHS).
notification(Value) ->
{notification, <<"2.0"/utf8>>, Value}.
-file("src/pollux.gleam", 151).
-spec response(id(), {ok, FOW} | {error, error_object()}) -> response(FOW).
response(Id, Result) ->
{response, <<"2.0"/utf8>>, Id, Result}.
-file("src/pollux.gleam", 155).
-spec result(id(), FIJ) -> response(FIJ).
result(Id, Result) ->
{response, <<"2.0"/utf8>>, Id, {ok, Result}}.