|
| 1 | +#include "prepared_template_rewriter.hpp" |
| 2 | + |
| 3 | +#include <algorithm> |
| 4 | + |
| 5 | +#include "config_manager.hpp" |
| 6 | + |
| 7 | +namespace flapi { |
| 8 | + |
| 9 | +namespace { |
| 10 | + |
| 11 | +// Tag types we recognise while scanning the template. `Sentinel` keeps |
| 12 | +// the lexer code small without sprinkling magic chars. |
| 13 | +enum class TagKind { |
| 14 | + OpenSection, // {{#name}} |
| 15 | + OpenInvertedSection, // {{^name}} |
| 16 | + CloseSection, // {{/name}} |
| 17 | + TripleBrace, // {{{ ... }}} |
| 18 | + DoubleBrace, // {{ ... }} |
| 19 | + NoTag, |
| 20 | +}; |
| 21 | + |
| 22 | +struct TagScan { |
| 23 | + TagKind kind = TagKind::NoTag; |
| 24 | + std::size_t start = 0; // index of the first `{` |
| 25 | + std::size_t end = 0; // one past the last `}` |
| 26 | + std::string inner; // trimmed content between braces |
| 27 | +}; |
| 28 | + |
| 29 | +void appendRange(std::string& out, const std::string& src, std::size_t a, std::size_t b) { |
| 30 | + out.append(src, a, b - a); |
| 31 | +} |
| 32 | + |
| 33 | +std::string trim(std::string s) { |
| 34 | + auto begin = std::find_if_not(s.begin(), s.end(), [](char c) { return c == ' ' || c == '\t'; }); |
| 35 | + s.erase(s.begin(), begin); |
| 36 | + auto rend = std::find_if_not(s.rbegin(), s.rend(), [](char c) { return c == ' ' || c == '\t'; }); |
| 37 | + s.erase(rend.base(), s.end()); |
| 38 | + return s; |
| 39 | +} |
| 40 | + |
| 41 | +bool startsWith(const std::string& s, std::size_t from, const char* prefix) { |
| 42 | + std::size_t n = 0; |
| 43 | + while (prefix[n] != '\0') ++n; |
| 44 | + if (s.size() < from + n) { |
| 45 | + return false; |
| 46 | + } |
| 47 | + return s.compare(from, n, prefix) == 0; |
| 48 | +} |
| 49 | + |
| 50 | +// Find the next Mustache-ish tag starting at `from`. Returns NoTag when |
| 51 | +// no further `{{` appears in the template. |
| 52 | +TagScan nextTag(const std::string& s, std::size_t from) { |
| 53 | + TagScan out; |
| 54 | + const std::size_t open = s.find("{{", from); |
| 55 | + if (open == std::string::npos) { |
| 56 | + return out; |
| 57 | + } |
| 58 | + out.start = open; |
| 59 | + |
| 60 | + // Triple-brace? |
| 61 | + if (startsWith(s, open, "{{{")) { |
| 62 | + const std::size_t close = s.find("}}}", open + 3); |
| 63 | + if (close == std::string::npos) { |
| 64 | + return out; // unterminated; treat as no-tag |
| 65 | + } |
| 66 | + out.kind = TagKind::TripleBrace; |
| 67 | + out.end = close + 3; |
| 68 | + out.inner = trim(s.substr(open + 3, close - (open + 3))); |
| 69 | + return out; |
| 70 | + } |
| 71 | + |
| 72 | + const std::size_t close = s.find("}}", open + 2); |
| 73 | + if (close == std::string::npos) { |
| 74 | + return out; |
| 75 | + } |
| 76 | + out.end = close + 2; |
| 77 | + std::string raw = s.substr(open + 2, close - (open + 2)); |
| 78 | + if (!raw.empty() && raw.front() == '#') { |
| 79 | + out.kind = TagKind::OpenSection; |
| 80 | + out.inner = trim(raw.substr(1)); |
| 81 | + } else if (!raw.empty() && raw.front() == '^') { |
| 82 | + out.kind = TagKind::OpenInvertedSection; |
| 83 | + out.inner = trim(raw.substr(1)); |
| 84 | + } else if (!raw.empty() && raw.front() == '/') { |
| 85 | + out.kind = TagKind::CloseSection; |
| 86 | + out.inner = trim(raw.substr(1)); |
| 87 | + } else { |
| 88 | + out.kind = TagKind::DoubleBrace; |
| 89 | + out.inner = trim(raw); |
| 90 | + } |
| 91 | + return out; |
| 92 | +} |
| 93 | + |
| 94 | +// Extract X from "params.X". Returns empty string when the expression |
| 95 | +// doesn't have the expected shape. |
| 96 | +std::string paramName(const std::string& inner) { |
| 97 | + static const std::string kPrefix = "params."; |
| 98 | + if (inner.compare(0, kPrefix.size(), kPrefix) != 0) { |
| 99 | + return {}; |
| 100 | + } |
| 101 | + return inner.substr(kPrefix.size()); |
| 102 | +} |
| 103 | + |
| 104 | +const RequestFieldConfig* findField(const std::string& name, |
| 105 | + const std::vector<RequestFieldConfig>& fields) { |
| 106 | + for (const auto& f : fields) { |
| 107 | + if (f.fieldName == name) { |
| 108 | + return &f; |
| 109 | + } |
| 110 | + } |
| 111 | + return nullptr; |
| 112 | +} |
| 113 | + |
| 114 | +} // namespace |
| 115 | + |
| 116 | +PreparedRewriteResult PreparedTemplateRewriter::rewrite( |
| 117 | + const std::string& template_text, |
| 118 | + const std::vector<RequestFieldConfig>& request_fields, |
| 119 | + const SqlParameterClassifier& classifier) const { |
| 120 | + |
| 121 | + PreparedRewriteResult result; |
| 122 | + result.rewritten_template.reserve(template_text.size()); |
| 123 | + |
| 124 | + std::size_t cursor = 0; |
| 125 | + int section_depth = 0; |
| 126 | + |
| 127 | + while (cursor < template_text.size()) { |
| 128 | + const TagScan tag = nextTag(template_text, cursor); |
| 129 | + if (tag.kind == TagKind::NoTag) { |
| 130 | + appendRange(result.rewritten_template, template_text, cursor, template_text.size()); |
| 131 | + break; |
| 132 | + } |
| 133 | + |
| 134 | + // Copy untouched text up to the tag. |
| 135 | + appendRange(result.rewritten_template, template_text, cursor, tag.start); |
| 136 | + |
| 137 | + switch (tag.kind) { |
| 138 | + case TagKind::OpenSection: |
| 139 | + case TagKind::OpenInvertedSection: |
| 140 | + ++section_depth; |
| 141 | + appendRange(result.rewritten_template, template_text, tag.start, tag.end); |
| 142 | + break; |
| 143 | + case TagKind::CloseSection: |
| 144 | + if (section_depth > 0) { |
| 145 | + --section_depth; |
| 146 | + } |
| 147 | + appendRange(result.rewritten_template, template_text, tag.start, tag.end); |
| 148 | + break; |
| 149 | + case TagKind::TripleBrace: |
| 150 | + // Out of scope for PR A — operators migrate triple-brace |
| 151 | + // sites manually (drop surrounding quotes, switch to |
| 152 | + // double-brace) before they become bindable. |
| 153 | + appendRange(result.rewritten_template, template_text, tag.start, tag.end); |
| 154 | + break; |
| 155 | + case TagKind::DoubleBrace: { |
| 156 | + if (section_depth > 0) { |
| 157 | + appendRange(result.rewritten_template, template_text, tag.start, tag.end); |
| 158 | + break; |
| 159 | + } |
| 160 | + const std::string name = paramName(tag.inner); |
| 161 | + if (name.empty()) { |
| 162 | + appendRange(result.rewritten_template, template_text, tag.start, tag.end); |
| 163 | + break; |
| 164 | + } |
| 165 | + const RequestFieldConfig* field = findField(name, request_fields); |
| 166 | + if (field == nullptr) { |
| 167 | + appendRange(result.rewritten_template, template_text, tag.start, tag.end); |
| 168 | + break; |
| 169 | + } |
| 170 | + const auto bindability = classifier.classify(*field); |
| 171 | + if (!bindability.bindable) { |
| 172 | + appendRange(result.rewritten_template, template_text, tag.start, tag.end); |
| 173 | + break; |
| 174 | + } |
| 175 | + result.rewritten_template += '?'; |
| 176 | + PreparedBindingSpec spec; |
| 177 | + spec.field_name = name; |
| 178 | + spec.type = bindability.type; |
| 179 | + spec.position = result.bindings.size(); |
| 180 | + result.bindings.push_back(std::move(spec)); |
| 181 | + break; |
| 182 | + } |
| 183 | + case TagKind::NoTag: |
| 184 | + break; // unreachable; satisfied by the early break above |
| 185 | + } |
| 186 | + |
| 187 | + cursor = tag.end; |
| 188 | + } |
| 189 | + |
| 190 | + return result; |
| 191 | +} |
| 192 | + |
| 193 | +} // namespace flapi |
0 commit comments