[core] Auto-convert concat arguments to strings

This commit is contained in:
John Firebaugh
2018-09-13 14:27:57 -07:00
committed by Chris Loer
parent a991eec597
commit 89b075f592
3 changed files with 15 additions and 9 deletions

View File

@@ -46,6 +46,8 @@ struct Value : ValueBase {
constexpr NullValue Null = NullValue();
type::Type typeOf(const Value& value);
std::string toString(const Value& value);
std::string stringify(const Value& value);
/*

View File

@@ -310,12 +310,7 @@ std::unordered_map<std::string, CompoundExpressionRegistry::Definition> initiali
define("typeof", [](const Value& v) -> Result<std::string> { return toString(typeOf(v)); });
define("to-string", [](const Value& value) -> Result<std::string> {
return value.match(
[](const NullValue&) -> Result<std::string> { return std::string(); },
[](const Color& c) -> Result<std::string> { return c.stringify(); }, // avoid quoting
[](const std::string& s) -> Result<std::string> { return s; }, // avoid quoting
[](const auto& v) -> Result<std::string> { return stringify(v); }
);
return toString(value);
});
define("to-boolean", [](const Value& v) -> Result<bool> {
@@ -507,10 +502,10 @@ std::unordered_map<std::string, CompoundExpressionRegistry::Definition> initiali
define("downcase", [](const std::string& input) -> Result<std::string> {
return platform::lowercase(input);
});
define("concat", [](const Varargs<std::string>& args) -> Result<std::string> {
define("concat", [](const Varargs<Value>& args) -> Result<std::string> {
std::string s;
for (const std::string& arg : args) {
s += arg;
for (const Value& arg : args) {
s += toString(arg);
}
return s;
});

View File

@@ -34,6 +34,15 @@ type::Type typeOf(const Value& value) {
);
}
std::string toString(const Value& value) {
return value.match(
[](const NullValue&) { return std::string(); },
[](const Color& c) { return c.stringify(); }, // avoid quoting
[](const std::string& s) { return s; }, // avoid quoting
[](const auto& v_) { return stringify(v_); }
);
}
void writeJSON(rapidjson::Writer<rapidjson::StringBuffer>& writer, const Value& value) {
value.match(
[&] (const NullValue&) { writer.Null(); },