Skip to content

Commit

Permalink
Allow enum as top level parameter in procedure calls (fixes #10)
Browse files Browse the repository at this point in the history
  • Loading branch information
cinemast committed Oct 21, 2020
1 parent 4aa83f1 commit fa878e6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.2.1] - 2020-10-21
### Changed
- Updated Catch to version 2.13.2
- Updated nlohmann_json to 3.9.1

### Fixed
- Typemapper failed to convert enum parameters on top-level (#10)

## [0.2.0] - 2020-10-14

### Added
Expand Down
3 changes: 3 additions & 0 deletions include/jsonrpccxx/typemapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ namespace jsonrpccxx {
}
template <typename T>
constexpr json::value_t GetType(type<T>) {
if (std::is_enum<T>::value) {
return json::value_t::string;
}
return json::value_t::object;
}
constexpr json::value_t GetType(type<void>) { return json::value_t::null; }
Expand Down
15 changes: 15 additions & 0 deletions test/typemapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,21 @@ bool add_products(const vector<product> &products) {
return true;
}

string enumToString(const category& category) {
switch (category) {
case category::cash_carry: return "cash&carry";
case category::order: return "online-order";
default: return "unknown category";
}
}

TEST_CASE("test with enum as top level parameter", TEST_MODULE) {
MethodHandle mh = GetHandle(&enumToString);

json params = R"(["cc"])"_json;
CHECK(mh(params) == "cash&carry");
}

TEST_CASE("test with custom params", TEST_MODULE) {
MethodHandle mh = GetHandle(&add_products);
catalog.clear();
Expand Down

0 comments on commit fa878e6

Please sign in to comment.