From 06d1f700c1daf1db2bab90e1013dae29732a80a9 Mon Sep 17 00:00:00 2001 From: pantor Date: Sat, 18 Nov 2017 21:05:47 +0100 Subject: [PATCH] improve array handling --- src/inja.hpp | 10 +++------- test/src/unit-parser.cpp | 2 +- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/inja.hpp b/src/inja.hpp index 379bf1d..ebe8642 100644 --- a/src/inja.hpp +++ b/src/inja.hpp @@ -428,10 +428,7 @@ public: template T eval_variable(const std::string& input, json data) { const json var = eval_variable(input, data, true); - - if (std::is_same::value) { - return var; - } + if (std::is_same::value) { return var; } return var.get(); } @@ -458,8 +455,7 @@ public: return result; } case Parser::Function::Length: { - const json list = eval_variable(match_function.str(1), data); - if (not list.is_array()) { throw std::runtime_error("[inja.exception.type_error.302] type must be array"); } + const std::vector list = eval_variable>(match_function.str(1), data); return list.size(); } case Parser::Function::Round: { @@ -578,7 +574,7 @@ public: const std::string item_name = match_command.str(1); const std::string list_name = match_command.str(2); - json list = eval_variable(list_name, data); + std::vector list = eval_variable>(list_name, data); for (int i = 0; i < list.size(); i++) { json data_loop = data; data_loop[item_name] = list[i]; diff --git a/test/src/unit-parser.cpp b/test/src/unit-parser.cpp index 722bcba..75c6557 100644 --- a/test/src/unit-parser.cpp +++ b/test/src/unit-parser.cpp @@ -248,7 +248,7 @@ TEST_CASE("Parse functions") { SECTION("Length") { CHECK( env.eval_variable("length(names)", data) == 4 ); - CHECK_THROWS_WITH( env.eval_variable("length(5)", data), "[inja.exception.type_error.302] type must be array" ); + CHECK_THROWS_WITH( env.eval_variable("length(5)", data), "[json.exception.type_error.302] type must be array, but is number" ); } SECTION("Round") {