object support
This commit is contained in:
parent
8fbcd12284
commit
812b4bdb41
29 changed files with 142 additions and 155 deletions
|
@ -21,12 +21,14 @@ namespace mstch {
|
|||
}
|
||||
protected:
|
||||
template<class S>
|
||||
void register_methods(S* sub, std::map<std::string,N(S::*)()> methods) {
|
||||
for(auto& m: methods)
|
||||
this->methods.insert(m.first, std::bind(m.second, sub));
|
||||
void register_method(std::string name, S* sub, N(S::*method)()) {
|
||||
this->methods.insert({name, std::bind(method, sub)});
|
||||
}
|
||||
void register_method(std::string name, const N& node) {
|
||||
this->methods.insert({name, [node](){return node;}});
|
||||
}
|
||||
private:
|
||||
const std::map<std::string, std::function<N()>> methods;
|
||||
std::map<std::string, std::function<N()>> methods;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -10,7 +10,6 @@ set(SRC
|
|||
state/in_section.cpp
|
||||
state/outside_section.cpp
|
||||
visitor/get_token.cpp
|
||||
visitor/has_token.cpp
|
||||
visitor/is_node_empty.cpp
|
||||
visitor/render_node.cpp
|
||||
visitor/render_section.cpp
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
#include "render_context.hpp"
|
||||
#include "utils.hpp"
|
||||
#include "state/outside_section.hpp"
|
||||
#include "visitor/has_token.hpp"
|
||||
#include "visitor/get_token.hpp"
|
||||
|
||||
using namespace mstch;
|
||||
|
@ -44,9 +43,10 @@ mstch::node render_context::find_node(
|
|||
token.substr(token.rfind('.') + 1),
|
||||
{find_node(token.substr(0, token.rfind('.')), current_nodes)});
|
||||
else
|
||||
for (auto& node: current_nodes)
|
||||
if (boost::apply_visitor(visitor::has_token(token), node))
|
||||
return boost::apply_visitor(visitor::get_token(token, node), node);
|
||||
for (auto& node: current_nodes) {
|
||||
auto ret = boost::apply_visitor(visitor::get_token(token, node), node);
|
||||
if(ret.first) return ret.second;
|
||||
}
|
||||
return null_node;
|
||||
}
|
||||
|
||||
|
|
|
@ -19,9 +19,10 @@ std::string state::outside_section::render(
|
|||
break;
|
||||
case token::type::variable:
|
||||
case token::type::unescaped_variable: {
|
||||
return boost::apply_visitor(visitor::render_node((token.token_type() ==
|
||||
token::type::variable)?flag::escape_html:flag::none),
|
||||
ctx.get_node(token.content()));
|
||||
auto visitor = visitor::render_node((token.token_type() ==
|
||||
token::type::variable)?flag::escape_html:flag::none);
|
||||
auto node = ctx.get_node(token.content());
|
||||
return boost::apply_visitor(visitor, node);
|
||||
}
|
||||
case token::type::comment: break;
|
||||
case token::type::text:
|
||||
|
|
|
@ -8,30 +8,36 @@ get_token::get_token(const std::string& token, const mstch::node& node):
|
|||
{
|
||||
}
|
||||
|
||||
mstch::node get_token::operator()(const boost::blank& blank) const {
|
||||
return node;
|
||||
std::pair<bool,node> get_token::operator()(const boost::blank& blank) const {
|
||||
return {token == ".", node};
|
||||
}
|
||||
|
||||
mstch::node get_token::operator()(const int& i) const {
|
||||
return node;
|
||||
std::pair<bool,node> get_token::operator()(const int& i) const {
|
||||
return {token == ".", node};
|
||||
}
|
||||
|
||||
mstch::node get_token::operator()(const bool& b) const {
|
||||
return node;
|
||||
std::pair<bool,node> get_token::operator()(const bool& b) const {
|
||||
return {token == ".", node};
|
||||
}
|
||||
|
||||
mstch::node get_token::operator()(const std::string& str) const {
|
||||
return node;
|
||||
std::pair<bool,node> get_token::operator()(const std::string& str) const {
|
||||
return {token == ".", node};
|
||||
}
|
||||
|
||||
mstch::node get_token::operator()(const array& arr) const {
|
||||
return node;
|
||||
std::pair<bool,node> get_token::operator()(const array& arr) const {
|
||||
return {token == ".", node};
|
||||
}
|
||||
|
||||
mstch::node get_token::operator()(const map& map) const {
|
||||
return map.at(token);
|
||||
std::pair<bool,node> get_token::operator()(const map& map) const {
|
||||
if(map.count(token) == 1)
|
||||
return {true, map.at(token)};
|
||||
else
|
||||
return {false, node};
|
||||
}
|
||||
|
||||
mstch::node get_token::operator()(const std::shared_ptr<object>& obj) const {
|
||||
return obj->at(token);
|
||||
std::pair<bool,node> get_token::operator()(const std::shared_ptr<object>& obj) const {
|
||||
if (obj->has(token))
|
||||
return {true, obj->at(token)};
|
||||
else
|
||||
return {false, node};
|
||||
}
|
||||
|
|
|
@ -6,16 +6,16 @@
|
|||
|
||||
namespace mstch {
|
||||
namespace visitor {
|
||||
class get_token: public boost::static_visitor<node> {
|
||||
class get_token: public boost::static_visitor<std::pair<bool,mstch::node>> {
|
||||
public:
|
||||
get_token(const std::string& token, const mstch::node& node);
|
||||
mstch::node operator()(const boost::blank& blank) const;
|
||||
mstch::node operator()(const int& i) const;
|
||||
mstch::node operator()(const bool& b) const;
|
||||
mstch::node operator()(const std::string& str) const;
|
||||
mstch::node operator()(const array& arr) const;
|
||||
mstch::node operator()(const map& map) const;
|
||||
mstch::node operator()(const std::shared_ptr<object>& obj) const;
|
||||
std::pair<bool,mstch::node> operator()(const boost::blank& blank) const;
|
||||
std::pair<bool,mstch::node> operator()(const int& i) const;
|
||||
std::pair<bool,mstch::node> operator()(const bool& b) const;
|
||||
std::pair<bool,mstch::node> operator()(const std::string& str) const;
|
||||
std::pair<bool,mstch::node> operator()(const array& arr) const;
|
||||
std::pair<bool,mstch::node> operator()(const map& map) const;
|
||||
std::pair<bool,mstch::node> operator()(const std::shared_ptr<object>& obj) const;
|
||||
private:
|
||||
const std::string& token;
|
||||
mstch::node node;
|
||||
|
|
|
@ -1,35 +0,0 @@
|
|||
#include "has_token.hpp"
|
||||
|
||||
using namespace mstch;
|
||||
using namespace mstch::visitor;
|
||||
|
||||
has_token::has_token(const std::string& token): token(token) {
|
||||
}
|
||||
|
||||
bool has_token::operator()(const boost::blank& blank) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool has_token::operator()(const int& i) const {
|
||||
return token == ".";
|
||||
}
|
||||
|
||||
bool has_token::operator()(const bool& b) const {
|
||||
return token == ".";
|
||||
}
|
||||
|
||||
bool has_token::operator()(const std::string& str) const {
|
||||
return token == ".";
|
||||
}
|
||||
|
||||
bool has_token::operator()(const array& arr) const {
|
||||
return token == ".";
|
||||
}
|
||||
|
||||
bool has_token::operator()(const map& map) const {
|
||||
return map.count(token) == 1;
|
||||
}
|
||||
|
||||
bool has_token::operator()(const std::shared_ptr<object>& obj) const {
|
||||
return obj->has(token);
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <boost/variant/static_visitor.hpp>
|
||||
#include <boost/blank.hpp>
|
||||
#include "mstch/mstch.hpp"
|
||||
|
||||
namespace mstch {
|
||||
namespace visitor {
|
||||
class has_token: public boost::static_visitor<bool> {
|
||||
public:
|
||||
has_token(const std::string& token);
|
||||
bool operator()(const boost::blank& blank) const;
|
||||
bool operator()(const int& i) const;
|
||||
bool operator()(const bool& b) const;
|
||||
bool operator()(const std::string& str) const;
|
||||
bool operator()(const array& arr) const;
|
||||
bool operator()(const map& map) const;
|
||||
bool operator()(const std::shared_ptr<object>& obj) const;
|
||||
private:
|
||||
const std::string& token;
|
||||
};
|
||||
}
|
||||
}
|
|
@ -3,12 +3,15 @@
|
|||
#include <boost/variant/static_visitor.hpp>
|
||||
#include <boost/blank.hpp>
|
||||
#include "mstch/mstch.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
namespace mstch {
|
||||
namespace visitor {
|
||||
class render_node: public boost::static_visitor<std::string> {
|
||||
class render_node : public boost::static_visitor<std::string> {
|
||||
public:
|
||||
enum class flag { none, escape_html };
|
||||
enum class flag {
|
||||
none, escape_html
|
||||
};
|
||||
render_node(flag p_flag = flag::none);
|
||||
std::string operator()(const boost::blank& blank) const;
|
||||
std::string operator()(const int& i) const;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
class comments: public mstch::object {
|
||||
public:
|
||||
comments() {
|
||||
register_methods(this, {{"title", &title}});
|
||||
register_method("title", this, &comments::title);
|
||||
}
|
||||
|
||||
mstch::node title() {
|
||||
|
|
49
test/data/complex.hpp
Normal file
49
test/data/complex.hpp
Normal file
|
@ -0,0 +1,49 @@
|
|||
class complex_item: public mstch::object {
|
||||
private:
|
||||
const std::string name;
|
||||
const bool current;
|
||||
const std::string url;
|
||||
public:
|
||||
complex_item(const std::string& name, bool current, const std::string& url):
|
||||
name{name}, current{current}, url{url}
|
||||
{
|
||||
register_method("name", {name});
|
||||
register_method("current", {current});
|
||||
register_method("url", {url});
|
||||
register_method("link", this, &complex_item::link);
|
||||
}
|
||||
|
||||
mstch::node link() {
|
||||
return !current;
|
||||
}
|
||||
};
|
||||
|
||||
class complex: public mstch::object {
|
||||
private:
|
||||
const std::string header;
|
||||
const mstch::array item;
|
||||
public:
|
||||
complex():
|
||||
header{"Colors"},
|
||||
item{
|
||||
std::make_shared<complex_item>("red", true, "#Red"),
|
||||
std::make_shared<complex_item>("green", false, "#Green"),
|
||||
std::make_shared<complex_item>("blue", false, "#Blue")
|
||||
}
|
||||
{
|
||||
register_method("header", {header});
|
||||
register_method("item", {item});
|
||||
register_method("list", this, &complex::list);
|
||||
register_method("empty", this, &complex::empty);
|
||||
}
|
||||
|
||||
mstch::node list() {
|
||||
return item.size() != 0;
|
||||
}
|
||||
|
||||
mstch::node empty() {
|
||||
return item.size() == 0;
|
||||
}
|
||||
};
|
||||
|
||||
const auto complex_data = std::make_shared<complex>();
|
25
test/data/dot_notation.hpp
Normal file
25
test/data/dot_notation.hpp
Normal file
|
@ -0,0 +1,25 @@
|
|||
class dot_notation_price: public mstch::object {
|
||||
private:
|
||||
const int value;
|
||||
const mstch::map currency;
|
||||
public:
|
||||
dot_notation_price():
|
||||
value{200}, currency{{"symbol", std::string{"$"}}, {"name", std::string{"USD"}}}
|
||||
{
|
||||
register_method("value", {value});
|
||||
register_method("vat", this, &dot_notation_price::vat);
|
||||
register_method("currency", {currency});
|
||||
}
|
||||
|
||||
mstch::node vat() {
|
||||
return static_cast<int>(value * 0.2);
|
||||
}
|
||||
};
|
||||
|
||||
const auto dot_notation_data = mstch::map{
|
||||
{"name", std::string{"A Book"}},
|
||||
{"authors", mstch::array{std::string{"John Power"}, std::string{"Jamie Walsh"}}},
|
||||
{"price", std::make_shared<dot_notation_price>()},
|
||||
{"availability", mstch::map{{"status", true}, {"text", std::string{"In Stock"}}}},
|
||||
{"truthy", mstch::map{{"zero", 0}, {"notTrue", false}}}
|
||||
};
|
|
@ -1,4 +1,4 @@
|
|||
const auto empty_sections_data = mstch::map{
|
||||
const auto empty_string_data = mstch::map{
|
||||
{"description", std::string{"That is all!"}},
|
||||
{"child", mstch::map{
|
||||
{"description", std::string{""}}
|
||||
|
|
|
@ -1 +1 @@
|
|||
const auto empty_sections_data = mstch::map{};
|
||||
const auto empty_template_data = mstch::map{};
|
|
@ -1,3 +1,3 @@
|
|||
const auto empty_sections_data = mstch::map{
|
||||
const auto error_not_found_data = mstch::map{
|
||||
{"bar", 2}
|
||||
};
|
|
@ -1,7 +1,8 @@
|
|||
class escaped: public mstch::object {
|
||||
public:
|
||||
escaped() {
|
||||
register_methods(this, {{"title", &title}, {"entities", &entities}});
|
||||
register_method("title", this, &escaped::title);
|
||||
register_method("entities", this, &escaped::entities);
|
||||
}
|
||||
|
||||
mstch::node title() {
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
const auto inlcuded_tag_data = mstch::map{
|
||||
const auto included_tag_data = mstch::map{
|
||||
{"html", std::string{"I like {{mustache}}"}}
|
||||
};
|
|
@ -1,19 +0,0 @@
|
|||
({
|
||||
header: function () {
|
||||
return "Colors";
|
||||
},
|
||||
item: [
|
||||
{name: "red", current: true, url: "#Red"},
|
||||
{name: "green", current: false, url: "#Green"},
|
||||
{name: "blue", current: false, url: "#Blue"}
|
||||
],
|
||||
link: function () {
|
||||
return this["current"] !== true;
|
||||
},
|
||||
list: function () {
|
||||
return this.item.length !== 0;
|
||||
},
|
||||
empty: function () {
|
||||
return this.item.length === 0;
|
||||
}
|
||||
})
|
|
@ -1,23 +0,0 @@
|
|||
({
|
||||
name: "A Book",
|
||||
authors: ["John Power", "Jamie Walsh"],
|
||||
price: {
|
||||
value: 200,
|
||||
vat: function () {
|
||||
return this.value * 0.2;
|
||||
},
|
||||
currency: {
|
||||
symbol: '$',
|
||||
name: 'USD'
|
||||
}
|
||||
},
|
||||
availability: {
|
||||
status: true,
|
||||
text: "In Stock"
|
||||
},
|
||||
// And now, some truthy false values
|
||||
truthy: {
|
||||
zero: 0,
|
||||
notTrue: false
|
||||
}
|
||||
})
|
|
@ -1,9 +1,8 @@
|
|||
class nested_higher_order_sections: public mstch::object {
|
||||
public:
|
||||
nested_higher_order_sections() {
|
||||
register_methods(this, {
|
||||
{"bold", &nested_higher_order_sections::bold},
|
||||
{"person", &nested_higher_order_sections::person}});
|
||||
register_method("bold", this, &nested_higher_order_sections::bold);
|
||||
register_method("person", this, &nested_higher_order_sections::person);
|
||||
}
|
||||
|
||||
mstch::node bold() {
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
class partial_template: public mstch::object {
|
||||
public:
|
||||
partial_template() {
|
||||
register_methods(this, {
|
||||
{"title", &partial_template::title},
|
||||
{"again", &partial_template::again}});
|
||||
register_method("title", this, &partial_template::title);
|
||||
register_method("again", this, &partial_template::again);
|
||||
}
|
||||
|
||||
mstch::node title() {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
class section_functions_in_partials: public mstch::object {
|
||||
public:
|
||||
section_functions_in_partials() {
|
||||
register_methods(this, {{"bold"}, §ion_functions_in_partials::bold});
|
||||
register_method("bold", this, §ion_functions_in_partials::bold);
|
||||
}
|
||||
|
||||
mstch::node bold() {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
class unescaped: public mstch::object {
|
||||
public:
|
||||
unescaped() {
|
||||
register_methods(this, {{"title", &unescaped::title}});
|
||||
register_method("title", this, &unescaped::title);
|
||||
}
|
||||
|
||||
mstch::node title() {
|
||||
|
@ -9,4 +9,4 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
const auto unescped_data = std::make_shared<unescaped>();
|
||||
const auto unescaped_data = std::make_shared<unescaped>();
|
|
@ -12,6 +12,7 @@ void wrap_code(std::istream& input, std::ostream& output) {
|
|||
output << line;
|
||||
if(!input.eof()) output << std::endl;
|
||||
}
|
||||
output << std::endl;
|
||||
}
|
||||
|
||||
void wrap_string(std::istream& input, std::ostream& output, const std::string& variable_name) {
|
||||
|
|
|
@ -20,16 +20,18 @@ MSTCH_TEST(array_of_strings)
|
|||
MSTCH_TEST(backslashes)
|
||||
MSTCH_TEST(bug_11_eating_whitespace)
|
||||
MSTCH_TEST(bug_length_property)
|
||||
//MSTCH_TEST(comments)
|
||||
MSTCH_TEST(comments)
|
||||
MSTCH_TEST(complex)
|
||||
MSTCH_TEST(context_lookup)
|
||||
MSTCH_TEST(disappearing_whitespace)
|
||||
MSTCH_TEST(dot_notation)
|
||||
MSTCH_TEST(double_render)
|
||||
MSTCH_TEST(empty_list)
|
||||
MSTCH_TEST(empty_sections)
|
||||
MSTCH_TEST(empty_string)
|
||||
MSTCH_TEST(empty_template)
|
||||
MSTCH_TEST(error_not_found)
|
||||
//MSTCH_TEST(escaped)
|
||||
MSTCH_TEST(escaped)
|
||||
MSTCH_TEST(falsy)
|
||||
MSTCH_TEST(falsy_array)
|
||||
MSTCH_TEST(grandparent_context)
|
||||
|
@ -50,7 +52,7 @@ MSTCH_PARTIAL_TEST(partial_array)
|
|||
MSTCH_PARTIAL_TEST(partial_array_of_partials)
|
||||
MSTCH_PARTIAL_TEST(partial_array_of_partials_implicit)
|
||||
MSTCH_PARTIAL_TEST(partial_empty)
|
||||
//MSTCH_PARTIAL_TEST(partial_template)
|
||||
MSTCH_PARTIAL_TEST(partial_template)
|
||||
MSTCH_TEST(recursion_with_same_names)
|
||||
MSTCH_TEST(reuse_of_enumerables)
|
||||
MSTCH_TEST(section_as_context)
|
||||
|
@ -58,6 +60,6 @@ MSTCH_TEST(section_as_context)
|
|||
MSTCH_TEST(string_as_context)
|
||||
MSTCH_TEST(two_in_a_row)
|
||||
MSTCH_TEST(two_sections)
|
||||
//MSTCH_TEST(unescaped)
|
||||
MSTCH_TEST(unescaped)
|
||||
MSTCH_TEST(whitespace)
|
||||
MSTCH_TEST(zero_view)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue