From fa878e68291e7a7711377b6b81cb2c97388ef5f1 Mon Sep 17 00:00:00 2001 From: Peter Spiess-Knafl Date: Wed, 21 Oct 2020 23:15:54 +0200 Subject: [PATCH] Allow enum as top level parameter in procedure calls (fixes #10) --- CHANGELOG.md | 8 ++++++++ include/jsonrpccxx/typemapper.hpp | 3 +++ test/typemapper.cpp | 15 +++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c20b725..8b7e5a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/include/jsonrpccxx/typemapper.hpp b/include/jsonrpccxx/typemapper.hpp index 74a3f5b..76f7c6d 100644 --- a/include/jsonrpccxx/typemapper.hpp +++ b/include/jsonrpccxx/typemapper.hpp @@ -22,6 +22,9 @@ namespace jsonrpccxx { } template constexpr json::value_t GetType(type) { + if (std::is_enum::value) { + return json::value_t::string; + } return json::value_t::object; } constexpr json::value_t GetType(type) { return json::value_t::null; } diff --git a/test/typemapper.cpp b/test/typemapper.cpp index 4b6e02f..3c54f61 100644 --- a/test/typemapper.cpp +++ b/test/typemapper.cpp @@ -146,6 +146,21 @@ bool add_products(const vector &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();