diff options
Diffstat (limited to 'lib/converters.h')
-rw-r--r-- | lib/converters.h | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/lib/converters.h b/lib/converters.h index 515c96fd..6515310a 100644 --- a/lib/converters.h +++ b/lib/converters.h @@ -414,4 +414,20 @@ inline void addParam(ContT& container, const QString& key, ValT&& value) _impl::AddNode<std::decay_t<ValT>, Force>::impl(container, key, std::forward<ValT>(value)); } + +// This is a facility function to convert camelCase method/variable names +// used throughout Quotient to snake_case JSON keys - see usage in +// single_key_value.h and event.h (DEFINE_CONTENT_GETTER macro). +inline auto toSnakeCase(QLatin1String s) +{ + QString result { s }; + for (auto it = result.begin(); it != result.end(); ++it) + if (it->isUpper()) { + const auto offset = static_cast<int>(it - result.begin()); + result.insert(offset, '_'); // NB: invalidates iterators + it = result.begin() + offset + 1; + *it = it->toLower(); + } + return result; +} } // namespace Quotient |