improve array handling

This commit is contained in:
pantor
2017-11-18 21:05:47 +01:00
parent 09238ec1d7
commit 06d1f700c1
2 changed files with 4 additions and 8 deletions

View File

@@ -428,10 +428,7 @@ public:
template<typename T = json>
T eval_variable(const std::string& input, json data) {
const json var = eval_variable(input, data, true);
if (std::is_same<T, json>::value) {
return var;
}
if (std::is_same<T, json>::value) { return var; }
return var.get<T>();
}
@@ -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<json> list = eval_variable<std::vector<json>>(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<json> list = eval_variable<std::vector<json>>(list_name, data);
for (int i = 0; i < list.size(); i++) {
json data_loop = data;
data_loop[item_name] = list[i];

View File

@@ -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") {