diff options
author | snaury <snaury@yandex-team.com> | 2024-10-16 12:16:48 +0300 |
---|---|---|
committer | snaury <snaury@yandex-team.com> | 2024-10-16 12:32:13 +0300 |
commit | e0fb25470a47f0c243091ed28bf54a186f732f6a (patch) | |
tree | e85dfe628401f4f21749ab95b9d711242e3b49cd /contrib/libs/jinja2cpp/src/value.cpp | |
parent | b3b4a0b9681eb0981f9958a426c95a53f79169a7 (diff) | |
download | ydb-e0fb25470a47f0c243091ed28bf54a186f732f6a.tar.gz |
ydblib: add jinja2cpp
commit_hash:f3563041f6f6f7443e75fc99acd2c967d0debb04
Diffstat (limited to 'contrib/libs/jinja2cpp/src/value.cpp')
-rw-r--r-- | contrib/libs/jinja2cpp/src/value.cpp | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/contrib/libs/jinja2cpp/src/value.cpp b/contrib/libs/jinja2cpp/src/value.cpp new file mode 100644 index 0000000000..3139b29ac2 --- /dev/null +++ b/contrib/libs/jinja2cpp/src/value.cpp @@ -0,0 +1,143 @@ +#if 0 +#include "jinja2cpp/value.h" +#include <sstream> + +namespace jinja2 +{ +template<typename T> +std::string toString(T val) +{ + std::ostringstream os; + os << val; + return os.str(); +} + +namespace +{ +struct ValueRenderer : boost::static_visitor<std::string> +{ + std::string operator() (bool val) const + { + return val ? "True" : "False"; + } + std::string operator() (const EmptyValue&) const + { + return std::string(); + } + std::string operator() (const std::wstring&) const + { + return std::string(); + } + + std::string operator() (const ValuesList& vals) const + { + std::string result = "{"; + bool isFirst = true; + for (auto& val : vals) + { + if (isFirst) + isFirst = false; + else + result += ", "; + + result += boost::apply_visitor(ValueRenderer(), val.data()); + } + result += "}"; + return result; + } + + std::string operator() (const ValuesMap& vals) const + { + std::string result = "{"; + bool isFirst = true; + for (auto& val : vals) + { + if (isFirst) + isFirst = false; + else + result += ", "; + + result += "{\"" + val.first + "\","; + result += boost::apply_visitor(ValueRenderer(), val.second.data()); + result += "}"; + } + result += "}"; + return result; + } + + std::string operator() (const GenericMap& /*val*/) const + { + return ""; + } + + std::string operator() (const GenericList& /*val*/) const + { + return ""; + } + + template<typename T> + std::string operator() (const T& val) const + { + return toString(val); + } +}; + +struct SubscriptionVisitor : public boost::static_visitor<InternalValue> +{ + InternalValue operator() (const ValuesMap& values, const std::string& field) const + { + auto p = values.find(field); + if (p == values.end()) + return InternalValue(); + + return p->second; + } + + InternalValue operator() (const GenericMap& values, const std::string& field) const + { + if (!values.HasValue(field)) + return InternalValue(); + + return values.GetValueByName(field); + } + + InternalValue operator() (const GenericMap& values, const int64_t index) const + { + if (index < 0 || static_cast<size_t>(index) >= values.GetSize()) + return InternalValue(); + + return values.GetValueByIndex(index); + } + + InternalValue operator() (const ValuesList& values, int64_t index) const + { + if (index < 0 || static_cast<size_t>(index) >= values.size()) + return InternalValue(); + + return values[static_cast<size_t>(index)]; + } + + InternalValue operator() (const GenericList& values, const int64_t index) const + { + if (index < 0 || static_cast<size_t>(index) >= values.GetSize()) + return InternalValue(); + + return values.GetValueByIndex(index); + } + + template<typename T, typename U> + InternalValue operator() (T&& /*first*/, U&& /*second*/) const + { + return InternalValue(); + } +}; + +} // + +InternalValue InternalValue::subscript(const InternalValue& index) const +{ + return boost::apply_visitor(SubscriptionVisitor(), m_data, index.m_data); +} + +} // jinja2 +#endif |