Add support for setting subobjects via JSON pointer in set statements. (#202)

E.g. `{% set x.y = 1 %}` sets the `y` member of `x` to 1.
This commit is contained in:
Bryce Adelstein Lelbach aka wash
2021-06-09 21:39:16 +02:00
committed by GitHub
parent 86f38f05d7
commit 798a0b92b1
5 changed files with 48 additions and 8 deletions
+6 -3
View File
@@ -184,10 +184,10 @@ class Renderer : public NodeVisitor {
void visit(const JsonNode& node) {
if (json_additional_data.contains(node.ptr)) {
json_eval_stack.push(&(json_additional_data[node.ptr]));
} else if (json_input->contains(node.ptr)) {
json_eval_stack.push(&(*json_input)[node.ptr]);
} else {
// Try to evaluate as a no-argument callback
const auto function_data = function_storage.find_function(node.name, 0);
@@ -662,7 +662,10 @@ class Renderer : public NodeVisitor {
}
void visit(const SetStatementNode& node) {
json_additional_data[node.key] = *eval_expression_list(node.expression);
std::string ptr = node.key;
replace_substring(ptr, ".", "/");
ptr = "/" + ptr;
json_additional_data[nlohmann::json::json_pointer(ptr)] = *eval_expression_list(node.expression);
}
public:
+11
View File
@@ -69,6 +69,17 @@ inline SourceLocation get_source_location(nonstd::string_view content, size_t po
return {count_lines + 1, sliced.length() - last_newline};
}
inline void replace_substring(std::string& s, const std::string& f,
const std::string& t)
{
if (f.empty()) return;
for (auto pos = s.find(f); // find first occurrence of f
pos != std::string::npos; // make sure f was found
s.replace(pos, f.size(), t), // replace with t, and
pos = s.find(f, pos + t.size())) // find next occurrence of f
{}
}
} // namespace inja
#endif // INCLUDE_INJA_UTILS_HPP_