mirror of
https://github.com/pantor/inja.git
synced 2026-04-23 00:09:27 +00:00
add join function (#204)
* add join function * fix formatting to match single include * add join test * add join to documentation * fix MSVC warning: signed/unsigned mismatch Co-authored-by: Wim Leflere <wleflere@cochlear.com>
This commit is contained in:
@@ -65,6 +65,7 @@ public:
|
||||
Sort,
|
||||
Upper,
|
||||
Super,
|
||||
Join,
|
||||
Callback,
|
||||
ParenLeft,
|
||||
ParenRight,
|
||||
@@ -109,6 +110,7 @@ private:
|
||||
{std::make_pair("upper", 1), FunctionData { Operation::Upper }},
|
||||
{std::make_pair("super", 0), FunctionData { Operation::Super }},
|
||||
{std::make_pair("super", 1), FunctionData { Operation::Super }},
|
||||
{std::make_pair("join", 2), FunctionData { Operation::Join }},
|
||||
};
|
||||
|
||||
public:
|
||||
|
||||
@@ -528,6 +528,24 @@ class Renderer : public NodeVisitor {
|
||||
json_tmp_stack.push_back(result_ptr);
|
||||
json_eval_stack.push(result_ptr.get());
|
||||
} break;
|
||||
case Op::Join: {
|
||||
const auto args = get_arguments<2>(node);
|
||||
const auto separator = args[1]->get<std::string>();
|
||||
std::ostringstream os;
|
||||
std::string sep;
|
||||
for (const auto& value : *args[0]) {
|
||||
os << sep;
|
||||
if (value.is_string()) {
|
||||
os << value.get<std::string>(); // otherwise the value is surrounded with ""
|
||||
} else {
|
||||
os << value;
|
||||
}
|
||||
sep = separator;
|
||||
}
|
||||
result_ptr = std::make_shared<json>(os.str());
|
||||
json_tmp_stack.push_back(result_ptr);
|
||||
json_eval_stack.push(result_ptr.get());
|
||||
} break;
|
||||
case Op::ParenLeft:
|
||||
case Op::ParenRight:
|
||||
case Op::None:
|
||||
|
||||
Reference in New Issue
Block a user