mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #6688 from ethereum/whiskersCond
Conditionals for Whiskers
This commit is contained in:
commit
815a272238
@ -30,64 +30,73 @@
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace dev;
|
using namespace dev;
|
||||||
|
|
||||||
Whiskers::Whiskers(string const& _template):
|
Whiskers::Whiskers(string _template):
|
||||||
m_template(_template)
|
m_template(move(_template))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
Whiskers& Whiskers::operator ()(string const& _parameter, string const& _value)
|
Whiskers& Whiskers::operator()(string _parameter, string _value)
|
||||||
{
|
{
|
||||||
assertThrow(
|
checkParameterUnknown(_parameter);
|
||||||
m_parameters.count(_parameter) == 0,
|
m_parameters[move(_parameter)] = move(_value);
|
||||||
WhiskersError,
|
|
||||||
_parameter + " already set."
|
|
||||||
);
|
|
||||||
assertThrow(
|
|
||||||
m_listParameters.count(_parameter) == 0,
|
|
||||||
WhiskersError,
|
|
||||||
_parameter + " already set as list parameter."
|
|
||||||
);
|
|
||||||
m_parameters[_parameter] = _value;
|
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Whiskers& Whiskers::operator ()(
|
Whiskers& Whiskers::operator()(string _parameter, bool _value)
|
||||||
string const& _listParameter,
|
{
|
||||||
vector<map<string, string>> const& _values
|
checkParameterUnknown(_parameter);
|
||||||
|
m_conditions[move(_parameter)] = _value;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Whiskers& Whiskers::operator()(
|
||||||
|
string _listParameter,
|
||||||
|
vector<map<string, string>> _values
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
assertThrow(
|
checkParameterUnknown(_listParameter);
|
||||||
m_listParameters.count(_listParameter) == 0,
|
m_listParameters[move(_listParameter)] = move(_values);
|
||||||
WhiskersError,
|
|
||||||
_listParameter + " already set."
|
|
||||||
);
|
|
||||||
assertThrow(
|
|
||||||
m_parameters.count(_listParameter) == 0,
|
|
||||||
WhiskersError,
|
|
||||||
_listParameter + " already set as value parameter."
|
|
||||||
);
|
|
||||||
m_listParameters[_listParameter] = _values;
|
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
string Whiskers::render() const
|
string Whiskers::render() const
|
||||||
{
|
{
|
||||||
return replace(m_template, m_parameters, m_listParameters);
|
return replace(m_template, m_parameters, m_conditions, m_listParameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Whiskers::checkParameterUnknown(string const& _parameter)
|
||||||
|
{
|
||||||
|
assertThrow(
|
||||||
|
!m_parameters.count(_parameter),
|
||||||
|
WhiskersError,
|
||||||
|
_parameter + " already set as value parameter."
|
||||||
|
);
|
||||||
|
assertThrow(
|
||||||
|
!m_conditions.count(_parameter),
|
||||||
|
WhiskersError,
|
||||||
|
_parameter + " already set as condition parameter."
|
||||||
|
);
|
||||||
|
assertThrow(
|
||||||
|
!m_listParameters.count(_parameter),
|
||||||
|
WhiskersError,
|
||||||
|
_parameter + " already set as list parameter."
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
string Whiskers::replace(
|
string Whiskers::replace(
|
||||||
string const& _template,
|
string const& _template,
|
||||||
StringMap const& _parameters,
|
StringMap const& _parameters,
|
||||||
|
map<string, bool> const& _conditions,
|
||||||
map<string, vector<StringMap>> const& _listParameters
|
map<string, vector<StringMap>> const& _listParameters
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
using namespace boost;
|
using namespace boost;
|
||||||
static regex listOrTag("<([^#/>]+)>|<#([^>]+)>(.*?)</\\2>");
|
static regex listOrTag("<([^#/?!>]+)>|<#([^>]+)>(.*?)</\\2>|<\\?([^>]+)>(.*?)(<!\\4>(.*?))?</\\4>");
|
||||||
return regex_replace(_template, listOrTag, [&](match_results<string::const_iterator> _match) -> string
|
return regex_replace(_template, listOrTag, [&](match_results<string::const_iterator> _match) -> string
|
||||||
{
|
{
|
||||||
string tagName(_match[1]);
|
string tagName(_match[1]);
|
||||||
|
string listName(_match[2]);
|
||||||
|
string conditionName(_match[4]);
|
||||||
if (!tagName.empty())
|
if (!tagName.empty())
|
||||||
{
|
{
|
||||||
assertThrow(
|
assertThrow(
|
||||||
@ -99,20 +108,32 @@ string Whiskers::replace(
|
|||||||
);
|
);
|
||||||
return _parameters.at(tagName);
|
return _parameters.at(tagName);
|
||||||
}
|
}
|
||||||
else
|
else if (!listName.empty())
|
||||||
{
|
{
|
||||||
string listName(_match[2]);
|
|
||||||
string templ(_match[3]);
|
string templ(_match[3]);
|
||||||
assertThrow(!listName.empty(), WhiskersError, "");
|
|
||||||
assertThrow(
|
assertThrow(
|
||||||
_listParameters.count(listName),
|
_listParameters.count(listName),
|
||||||
WhiskersError, "List parameter " + listName + " not set."
|
WhiskersError, "List parameter " + listName + " not set."
|
||||||
);
|
);
|
||||||
string replacement;
|
string replacement;
|
||||||
for (auto const& parameters: _listParameters.at(listName))
|
for (auto const& parameters: _listParameters.at(listName))
|
||||||
replacement += replace(templ, joinMaps(_parameters, parameters));
|
replacement += replace(templ, joinMaps(_parameters, parameters), _conditions);
|
||||||
return replacement;
|
return replacement;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
assertThrow(!conditionName.empty(), WhiskersError, "");
|
||||||
|
assertThrow(
|
||||||
|
_conditions.count(conditionName),
|
||||||
|
WhiskersError, "Condition parameter " + conditionName + " not set."
|
||||||
|
);
|
||||||
|
return replace(
|
||||||
|
_conditions.at(conditionName) ? _match[5] : _match[7],
|
||||||
|
_parameters,
|
||||||
|
_conditions,
|
||||||
|
_listParameters
|
||||||
|
);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,45 +34,63 @@ namespace dev
|
|||||||
|
|
||||||
DEV_SIMPLE_EXCEPTION(WhiskersError);
|
DEV_SIMPLE_EXCEPTION(WhiskersError);
|
||||||
|
|
||||||
///
|
/**
|
||||||
/// Moustache-like templates.
|
* Moustache-like templates.
|
||||||
///
|
*
|
||||||
/// Usage:
|
* Usage:
|
||||||
/// std::vector<std::map<std::string, std::string>> listValues(2);
|
* std::vector<std::map<std::string, std::string>> listValues(2);
|
||||||
/// listValues[0]["k"] = "key1";
|
* listValues[0]["k"] = "key1";
|
||||||
/// listValues[0]["v"] = "value1";
|
* listValues[0]["v"] = "value1";
|
||||||
/// listValues[1]["k"] = "key2";
|
* listValues[1]["k"] = "key2";
|
||||||
/// listValues[1]["v"] = "value2";
|
* listValues[1]["v"] = "value2";
|
||||||
/// auto s = Whiskers("<p1>\n<#list><k> -> <v>\n</list>")
|
* auto s = Whiskers("<?c><p1><!c>y</c>\n<#list><k> -> <v>\n</list>")
|
||||||
/// ("p1", "HEAD")
|
* ("p1", "HEAD")
|
||||||
/// ("list", listValues)
|
* ("c", true)
|
||||||
/// .render();
|
* ("list", listValues)
|
||||||
///
|
* .render();
|
||||||
/// results in s == "HEAD\nkey1 -> value1\nkey2 -> value2\n"
|
*
|
||||||
///
|
* results in s == "HEAD\nkey1 -> value1\nkey2 -> value2\n"
|
||||||
/// Note that lists cannot themselves contain lists - this would be a future feature.
|
*
|
||||||
|
* Note that lists cannot themselves contain lists - this would be a future feature.
|
||||||
|
*
|
||||||
|
* The elements are:
|
||||||
|
* - Regular parameter: <name>
|
||||||
|
* just replaced
|
||||||
|
* - Condition parameter: <?name>...<!name>...</name>, where "<!name>" is optional
|
||||||
|
* replaced (and recursively expanded) by the first part if the condition is true
|
||||||
|
* and by the second (or empty string if missing) if the condition is false
|
||||||
|
* - List parameter: <#list>...</list>
|
||||||
|
* The part between the tags is repeated as often as values are provided
|
||||||
|
* in the mapping. Each list element can have its own parameter -> value mapping.
|
||||||
|
*/
|
||||||
class Whiskers
|
class Whiskers
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using StringMap = std::map<std::string, std::string>;
|
using StringMap = std::map<std::string, std::string>;
|
||||||
using StringListMap = std::map<std::string, std::vector<StringMap>>;
|
using StringListMap = std::map<std::string, std::vector<StringMap>>;
|
||||||
|
|
||||||
explicit Whiskers(std::string const& _template);
|
explicit Whiskers(std::string _template);
|
||||||
|
|
||||||
/// Sets a single parameter, <paramName>.
|
/// Sets a single regular parameter, <paramName>.
|
||||||
Whiskers& operator()(std::string const& _parameter, std::string const& _value);
|
Whiskers& operator()(std::string _parameter, std::string _value);
|
||||||
|
Whiskers& operator()(std::string _parameter, char const* _value) { return (*this)(_parameter, std::string{_value}); }
|
||||||
|
/// Sets a condition parameter, <?paramName>...<!paramName>...</paramName>
|
||||||
|
Whiskers& operator()(std::string _parameter, bool _value);
|
||||||
/// Sets a list parameter, <#listName> </listName>.
|
/// Sets a list parameter, <#listName> </listName>.
|
||||||
Whiskers& operator()(
|
Whiskers& operator()(
|
||||||
std::string const& _listParameter,
|
std::string _listParameter,
|
||||||
std::vector<StringMap> const& _values
|
std::vector<StringMap> _values
|
||||||
);
|
);
|
||||||
|
|
||||||
std::string render() const;
|
std::string render() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void checkParameterUnknown(std::string const& _parameter);
|
||||||
|
|
||||||
static std::string replace(
|
static std::string replace(
|
||||||
std::string const& _template,
|
std::string const& _template,
|
||||||
StringMap const& _parameters,
|
StringMap const& _parameters,
|
||||||
|
std::map<std::string, bool> const& _conditions,
|
||||||
StringListMap const& _listParameters = StringListMap()
|
StringListMap const& _listParameters = StringListMap()
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -81,6 +99,7 @@ private:
|
|||||||
|
|
||||||
std::string m_template;
|
std::string m_template;
|
||||||
StringMap m_parameters;
|
StringMap m_parameters;
|
||||||
|
std::map<std::string, bool> m_conditions;
|
||||||
StringListMap m_listParameters;
|
StringListMap m_listParameters;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -55,6 +55,65 @@ BOOST_AUTO_TEST_CASE(tag_unavailable)
|
|||||||
BOOST_CHECK_THROW(m.render(), WhiskersError);
|
BOOST_CHECK_THROW(m.render(), WhiskersError);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(list_unavailable)
|
||||||
|
{
|
||||||
|
string templ = "<#b></b>";
|
||||||
|
Whiskers m(templ);
|
||||||
|
BOOST_CHECK_THROW(m.render(), WhiskersError);
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(name_type_collision)
|
||||||
|
{
|
||||||
|
string templ = "<b><#b></b>";
|
||||||
|
Whiskers m(templ);
|
||||||
|
m("b", "x");
|
||||||
|
BOOST_CHECK_THROW(m("b", vector<map<string, string>>{}), WhiskersError);
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(conditional)
|
||||||
|
{
|
||||||
|
string templ = "<?b>X</b>";
|
||||||
|
BOOST_CHECK_EQUAL(Whiskers(templ)("b", true).render(), "X");
|
||||||
|
BOOST_CHECK_EQUAL(Whiskers(templ)("b", false).render(), "");
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(conditional_with_else)
|
||||||
|
{
|
||||||
|
string templ = "<?b>X<!b>Y</b>";
|
||||||
|
BOOST_CHECK_EQUAL(Whiskers(templ)("b", true).render(), "X");
|
||||||
|
BOOST_CHECK_EQUAL(Whiskers(templ)("b", false).render(), "Y");
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(conditional_plus_params)
|
||||||
|
{
|
||||||
|
string templ = " - <?b>_<r><!b>^<t></b> - ";
|
||||||
|
Whiskers m1(templ);
|
||||||
|
m1("b", true);
|
||||||
|
m1("r", "R");
|
||||||
|
m1("t", "T");
|
||||||
|
BOOST_CHECK_EQUAL(m1.render(), " - _R - ");
|
||||||
|
|
||||||
|
Whiskers m2(templ);
|
||||||
|
m2("b", false);
|
||||||
|
m2("r", "R");
|
||||||
|
m2("t", "T");
|
||||||
|
BOOST_CHECK_EQUAL(m2.render(), " - ^T - ");
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(conditional_plus_list)
|
||||||
|
{
|
||||||
|
string templ = " - <?b>_<#l><x></l><!b><#l><y></l></b> - ";
|
||||||
|
Whiskers m(templ);
|
||||||
|
m("b", false);
|
||||||
|
vector<map<string, string>> list(2);
|
||||||
|
list[0]["x"] = "1";
|
||||||
|
list[0]["y"] = "a";
|
||||||
|
list[1]["x"] = "2";
|
||||||
|
list[1]["y"] = "b";
|
||||||
|
m("l", list);
|
||||||
|
BOOST_CHECK_EQUAL(m.render(), " - ab - ");
|
||||||
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(complicated_replacement)
|
BOOST_AUTO_TEST_CASE(complicated_replacement)
|
||||||
{
|
{
|
||||||
string templ = "a <b> x <complicated> \n <nes<ted>>.";
|
string templ = "a <b> x <complicated> \n <nes<ted>>.";
|
||||||
|
Loading…
Reference in New Issue
Block a user