Fix Response type info, make response 'body' field optional
The `body` field of the Response is optional. Do not error if it is missing. If the typeinfo of the response. This was incorrectly using the Request type. Authored by kuafuwang, squashed by ben-clayton.
This commit is contained in:
parent
eab43f3518
commit
73d697eac4
@ -201,7 +201,8 @@ class Session {
|
|||||||
virtual void registerHandler(const TypeInfo* typeinfo,
|
virtual void registerHandler(const TypeInfo* typeinfo,
|
||||||
const GenericResponseSentHandler& handler) = 0;
|
const GenericResponseSentHandler& handler) = 0;
|
||||||
|
|
||||||
virtual bool send(const dap::TypeInfo* typeinfo,
|
virtual bool send(const dap::TypeInfo* requestTypeInfo,
|
||||||
|
const dap::TypeInfo* responseTypeInfo,
|
||||||
const void* request,
|
const void* request,
|
||||||
const GenericResponseHandler& responseHandler) = 0;
|
const GenericResponseHandler& responseHandler) = 0;
|
||||||
|
|
||||||
@ -251,9 +252,9 @@ template <typename T, typename>
|
|||||||
future<ResponseOrError<typename T::Response>> Session::send(const T& request) {
|
future<ResponseOrError<typename T::Response>> Session::send(const T& request) {
|
||||||
using Response = typename T::Response;
|
using Response = typename T::Response;
|
||||||
promise<ResponseOrError<Response>> promise;
|
promise<ResponseOrError<Response>> promise;
|
||||||
const TypeInfo* typeinfo = TypeOf<T>::type();
|
auto sent = send(
|
||||||
auto sent =
|
TypeOf<T>::type(), TypeOf<Response>::type(), &request,
|
||||||
send(typeinfo, &request, [=](const void* result, const Error* error) {
|
[=](const void* result, const Error* error) {
|
||||||
if (error != nullptr) {
|
if (error != nullptr) {
|
||||||
promise.set_value(ResponseOrError<Response>(*error));
|
promise.set_value(ResponseOrError<Response>(*error));
|
||||||
} else {
|
} else {
|
||||||
|
@ -80,19 +80,20 @@ class Impl : public dap::Session {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
bool send(const dap::TypeInfo* typeinfo,
|
bool send(const dap::TypeInfo* requestTypeInfo,
|
||||||
|
const dap::TypeInfo* responseTypeInfo,
|
||||||
const void* request,
|
const void* request,
|
||||||
const GenericResponseHandler& responseHandler) override {
|
const GenericResponseHandler& responseHandler) override {
|
||||||
int seq = nextSeq++;
|
int seq = nextSeq++;
|
||||||
|
|
||||||
handlers.put(seq, typeinfo, responseHandler);
|
handlers.put(seq, responseTypeInfo, responseHandler);
|
||||||
|
|
||||||
dap::json::Serializer s;
|
dap::json::Serializer s;
|
||||||
s.field("seq", dap::integer(seq));
|
s.field("seq", dap::integer(seq));
|
||||||
s.field("type", "request");
|
s.field("type", "request");
|
||||||
s.field("command", typeinfo->name());
|
s.field("command", requestTypeInfo->name());
|
||||||
s.field("arguments", [&](dap::Serializer* s) {
|
s.field("arguments", [&](dap::Serializer* s) {
|
||||||
return typeinfo->serialize(s, request);
|
return requestTypeInfo->serialize(s, request);
|
||||||
});
|
});
|
||||||
return send(s.dump());
|
return send(s.dump());
|
||||||
}
|
}
|
||||||
@ -411,12 +412,11 @@ class Impl : public dap::Session {
|
|||||||
auto data = std::unique_ptr<uint8_t[]>(new uint8_t[typeinfo->size()]);
|
auto data = std::unique_ptr<uint8_t[]>(new uint8_t[typeinfo->size()]);
|
||||||
typeinfo->construct(data.get());
|
typeinfo->construct(data.get());
|
||||||
|
|
||||||
if (!d->field("body", [&](const dap::Deserializer* d) {
|
// "body" field in Response is an optional field.
|
||||||
return typeinfo->deserialize(d, data.get());
|
d->field("body", [&](const dap::Deserializer* d) {
|
||||||
})) {
|
return typeinfo->deserialize(d, data.get());
|
||||||
handlers.error("Failed to deserialize request");
|
});
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
handler(data.get(), nullptr);
|
handler(data.get(), nullptr);
|
||||||
typeinfo->destruct(data.get());
|
typeinfo->destruct(data.get());
|
||||||
|
@ -54,7 +54,7 @@ DAP_STRUCT_TYPEINFO(TestResponse,
|
|||||||
struct TestRequest : public Request {
|
struct TestRequest : public Request {
|
||||||
using Response = TestResponse;
|
using Response = TestResponse;
|
||||||
|
|
||||||
boolean b;
|
boolean req_b;
|
||||||
integer i;
|
integer i;
|
||||||
number n;
|
number n;
|
||||||
array<integer> a;
|
array<integer> a;
|
||||||
@ -66,7 +66,7 @@ struct TestRequest : public Request {
|
|||||||
|
|
||||||
DAP_STRUCT_TYPEINFO(TestRequest,
|
DAP_STRUCT_TYPEINFO(TestRequest,
|
||||||
"test-request",
|
"test-request",
|
||||||
DAP_FIELD(b, "b"),
|
DAP_FIELD(req_b, "req_b"),
|
||||||
DAP_FIELD(i, "i"),
|
DAP_FIELD(i, "i"),
|
||||||
DAP_FIELD(n, "n"),
|
DAP_FIELD(n, "n"),
|
||||||
DAP_FIELD(a, "a"),
|
DAP_FIELD(a, "a"),
|
||||||
@ -103,7 +103,7 @@ namespace {
|
|||||||
|
|
||||||
dap::TestRequest createRequest() {
|
dap::TestRequest createRequest() {
|
||||||
dap::TestRequest request;
|
dap::TestRequest request;
|
||||||
request.b = false;
|
request.req_b = false;
|
||||||
request.i = 72;
|
request.i = 72;
|
||||||
request.n = 9.87;
|
request.n = 9.87;
|
||||||
request.a = {2, 5, 7, 8};
|
request.a = {2, 5, 7, 8};
|
||||||
@ -177,7 +177,7 @@ TEST_F(SessionTest, Request) {
|
|||||||
client->send(request).get();
|
client->send(request).get();
|
||||||
|
|
||||||
// Check request was received correctly.
|
// Check request was received correctly.
|
||||||
ASSERT_EQ(received.b, request.b);
|
ASSERT_EQ(received.req_b, request.req_b);
|
||||||
ASSERT_EQ(received.i, request.i);
|
ASSERT_EQ(received.i, request.i);
|
||||||
ASSERT_EQ(received.n, request.n);
|
ASSERT_EQ(received.n, request.n);
|
||||||
ASSERT_EQ(received.a, request.a);
|
ASSERT_EQ(received.a, request.a);
|
||||||
@ -219,6 +219,30 @@ TEST_F(SessionTest, RequestResponseSuccess) {
|
|||||||
ASSERT_FALSE(got.response.o2.has_value());
|
ASSERT_FALSE(got.response.o2.has_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(SessionTest, BreakPointRequestResponseSuccess) {
|
||||||
|
server->registerHandler(
|
||||||
|
[&](const dap::SetBreakpointsRequest&) {
|
||||||
|
dap::SetBreakpointsResponse rsp;
|
||||||
|
dap::array<dap::Breakpoint> breakpoints;
|
||||||
|
dap::Breakpoint bpItem;
|
||||||
|
bpItem.line = 2;
|
||||||
|
breakpoints.emplace_back(std::move(bpItem));
|
||||||
|
rsp.breakpoints.swap(breakpoints);
|
||||||
|
return rsp;
|
||||||
|
});
|
||||||
|
|
||||||
|
bind();
|
||||||
|
|
||||||
|
dap::SetBreakpointsRequest request;
|
||||||
|
auto response = client->send(request);
|
||||||
|
|
||||||
|
auto got = response.get();
|
||||||
|
|
||||||
|
// Check response was received correctly.
|
||||||
|
ASSERT_EQ(got.error, false);
|
||||||
|
ASSERT_EQ(got.response.breakpoints.size(), 1);
|
||||||
|
|
||||||
|
}
|
||||||
TEST_F(SessionTest, RequestResponseOrError) {
|
TEST_F(SessionTest, RequestResponseOrError) {
|
||||||
server->registerHandler(
|
server->registerHandler(
|
||||||
[&](const dap::TestRequest&) -> dap::ResponseOrError<dap::TestResponse> {
|
[&](const dap::TestRequest&) -> dap::ResponseOrError<dap::TestResponse> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user