Skip to content

Commit 08e0225

Browse files
authored
Feature/arbitrary json types (#25)
* Test for arbitrary json params (#24) * Allow unchecked RPC calls (#24) * Remove Test Modules * Update OSX Toolchain * Update OSX Toolchain
1 parent 180b1a9 commit 08e0225

File tree

8 files changed

+39
-25
lines changed

8 files changed

+39
-25
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ jobs:
4545
command: ./example-warehouse
4646
"build-osx-clang":
4747
macos:
48-
xcode: "10.2.1"
48+
xcode: "12.4.0"
4949
steps:
5050
- checkout
5151
- run:

include/jsonrpccxx/typemapper.hpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
#include <vector>
99

1010
namespace jsonrpccxx {
11-
typedef std::vector<json> Parameters;
12-
typedef std::function<json(const Parameters &)> MethodHandle;
13-
typedef std::function<void(const Parameters &)> NotificationHandle;
11+
typedef std::function<json(const json &)> MethodHandle;
12+
typedef std::function<void(const json &)> NotificationHandle;
1413

1514
// Workaround due to forbidden partial template function specialisation
1615
template <typename T>
@@ -89,12 +88,9 @@ namespace jsonrpccxx {
8988
}
9089
}
9190

92-
//
93-
// Mapping for methods
94-
//
9591
template <typename ReturnType, typename... ParamTypes, std::size_t... index>
9692
MethodHandle createMethodHandle(std::function<ReturnType(ParamTypes...)> method, std::index_sequence<index...>) {
97-
MethodHandle handle = [method](const Parameters &params) -> json {
93+
MethodHandle handle = [method](const json &params) -> json {
9894
size_t actualSize = params.size();
9995
size_t formalSize = sizeof...(ParamTypes);
10096
// TODO: add lenient mode for backwards compatible additional params
@@ -122,12 +118,19 @@ namespace jsonrpccxx {
122118
return GetHandle(std::function<ReturnType(ParamTypes...)>(f));
123119
}
124120

121+
inline MethodHandle GetUncheckedHandle(std::function<json(const json&)> f) {
122+
MethodHandle handle = [f](const json &params) -> json {
123+
return f(params);
124+
};
125+
return handle;
126+
}
127+
125128
//
126129
// Notification mapping
127130
//
128131
template <typename... ParamTypes, std::size_t... index>
129132
NotificationHandle createNotificationHandle(std::function<void(ParamTypes...)> method, std::index_sequence<index...>) {
130-
NotificationHandle handle = [method](const Parameters &params) -> void {
133+
NotificationHandle handle = [method](const json &params) -> void {
131134
size_t actualSize = params.size();
132135
size_t formalSize = sizeof...(ParamTypes);
133136
// TODO: add lenient mode for backwards compatible additional params
@@ -156,9 +159,13 @@ namespace jsonrpccxx {
156159
return GetHandle(std::function<void(ParamTypes...)>(f));
157160
}
158161

159-
//
160-
// Mapping for classes
161-
//
162+
inline NotificationHandle GetUncheckedNotificationHandle(std::function<void(const json&)> f) {
163+
NotificationHandle handle = [f](const json &params) -> void {
164+
f(params);
165+
};
166+
return handle;
167+
}
168+
162169
template <typename T, typename ReturnType, typename... ParamTypes>
163170
MethodHandle methodHandle(ReturnType (T::*method)(ParamTypes...), T &instance) {
164171
std::function<ReturnType(ParamTypes...)> function = [&instance, method](ParamTypes &&... params) -> ReturnType {
@@ -190,4 +197,4 @@ namespace jsonrpccxx {
190197
};
191198
return GetHandle(function);
192199
}
193-
} // namespace jsonrpccxx
200+
}

test/batchclient.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#include <iostream>
44
#include <jsonrpccxx/batchclient.hpp>
55

6-
#define TEST_MODULE "[batch]"
7-
86
using namespace std;
97
using namespace jsonrpccxx;
108

test/client.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#include <iostream>
44
#include <jsonrpccxx/client.hpp>
55

6-
#define TEST_MODULE "[client]"
7-
86
using namespace std;
97
using namespace jsonrpccxx;
108

test/dispatcher.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
using namespace jsonrpccxx;
66
using namespace std;
77

8-
#define TEST_MODULE "[dispatcher]"
9-
108
static string procCache;
119
unsigned int add_function(unsigned int a, unsigned int b) { return a + b; }
1210
void some_procedure(const string &param) { procCache = param; }

test/server.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
using namespace jsonrpccxx;
77
using namespace std;
88

9-
#define TEST_MODULE "[server]"
10-
119
struct Server2 {
1210
JsonRpc2Server server;
1311
TestServerConnector connector;

test/typemapper.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
using namespace jsonrpccxx;
77
using namespace std;
88

9-
#define TEST_MODULE "[typemapper]"
10-
119
static string notifyResult = "";
1210

1311
int add(int a, int b) { return a + b; }
@@ -209,4 +207,23 @@ TEST_CASE("test auto conversion of float to int passed to float method") {
209207
CHECK(mh(R"([3,3])"_json) == 6.0);
210208
CHECK(mh(R"([3.0,3.0])"_json) == 6.0);
211209
CHECK(mh(R"([3.1,3.2])"_json) == doctest::Approx(6.3));
210+
}
211+
212+
json arbitrary_json(const json& value) {
213+
return value;
214+
}
215+
216+
void arbitrary_json_notification(const json& value) {
217+
to_string(value);
218+
}
219+
220+
TEST_CASE("test json method handles without specific types") {
221+
MethodHandle mh = GetUncheckedHandle(&arbitrary_json);
222+
CHECK(mh(R"([3,"string"])"_json) == R"([3,"string"])"_json);
223+
auto param = R"({"a": "string"})"_json;
224+
CHECK(mh(param) == param);
225+
226+
NotificationHandle nh = GetUncheckedNotificationHandle(&arbitrary_json_notification);
227+
nh(R"([3,"string"])"_json);
228+
nh(R"({"3": "string"})"_json);
212229
}

test/warehouseapp.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#include "doctest/doctest.h"
44
#include "integrationtest.hpp"
55

6-
#define TEST_MODULE "[integration]"
7-
86
TEST_CASE_FIXTURE(IntegrationTest, "warehouse_test") {
97
WarehouseServer app;
108
rpcServer.Add("GetProduct", GetHandle(&WarehouseServer::GetProduct, app));

0 commit comments

Comments
 (0)