From d876a2df0c02da59b07fd1854853c70d82d7e5fa Mon Sep 17 00:00:00 2001 From: emeric Date: Sun, 10 Mar 2024 15:28:16 +0100 Subject: [PATCH] Switched to LiteralString for keys in subsonic responses --- src/libs/subsonic/impl/SubsonicResponse.cpp | 22 ++++++++++----------- src/libs/subsonic/impl/SubsonicResponse.hpp | 14 ++----------- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/src/libs/subsonic/impl/SubsonicResponse.cpp b/src/libs/subsonic/impl/SubsonicResponse.cpp index c5d971e3..70153e2c 100644 --- a/src/libs/subsonic/impl/SubsonicResponse.cpp +++ b/src/libs/subsonic/impl/SubsonicResponse.cpp @@ -193,13 +193,13 @@ namespace API::Subsonic for (const auto& [key, value] : node._attributes) { if (std::holds_alternative(value)) - res.put("." + std::string{ key.get() }, std::get(value)); + res.put("." + std::string{ key.str() }, std::get(value)); else if (std::holds_alternative(value)) - res.put("." + std::string{ key.get() }, std::get(value)); + res.put("." + std::string{ key.str() }, std::get(value)); else if (std::holds_alternative(value)) - res.put("." + std::string{ key.get() }, std::get(value)); + res.put("." + std::string{ key.str() }, std::get(value)); else if (std::holds_alternative(value)) - res.put("." + std::string{ key.get() }, std::get(value)); + res.put("." + std::string{ key.str() }, std::get(value)); } auto valueToPropertyTree = [](const Node::ValueType& value) @@ -221,19 +221,19 @@ namespace API::Subsonic { for (const auto& [key, childNode] : node._children) { - res.add_child(std::string{ key.get() }, nodeToPropertyTree(childNode)); + res.add_child(std::string{ key.str() }, nodeToPropertyTree(childNode)); } for (const auto& [key, childArrayNodes] : node._childrenArrays) { for (const Node& childNode : childArrayNodes) - res.add_child(std::string{ key.get() }, nodeToPropertyTree(childNode)); + res.add_child(std::string{ key.str() }, nodeToPropertyTree(childNode)); } for (const auto& [key, childArrayValues] : node._childrenValues) { for (const Response::Node::ValueType& value : childArrayValues) - res.add_child(std::string{ key.get() }, valueToPropertyTree(value)); + res.add_child(std::string{ key.str() }, valueToPropertyTree(value)); } } @@ -255,7 +255,7 @@ namespace API::Subsonic if (!first) os << ','; - serializeEscapedString(os, key.get()); + serializeEscapedString(os, key.str()); os << ':'; serializeValue(os, value); @@ -279,7 +279,7 @@ namespace API::Subsonic if (!first) os << ','; - serializeEscapedString(os, key.get()); + serializeEscapedString(os, key.str()); os << ':'; serializeNode(os, childNode); @@ -291,7 +291,7 @@ namespace API::Subsonic if (!first) os << ','; - serializeEscapedString(os, key.get()); + serializeEscapedString(os, key.str()); os << ":["; bool firstChild{ true }; @@ -313,7 +313,7 @@ namespace API::Subsonic if (!first) os << ','; - serializeEscapedString(os, key.get()); + serializeEscapedString(os, key.str()); os << ":["; bool firstChild{ true }; diff --git a/src/libs/subsonic/impl/SubsonicResponse.hpp b/src/libs/subsonic/impl/SubsonicResponse.hpp index 60690dae..5ff50686 100644 --- a/src/libs/subsonic/impl/SubsonicResponse.hpp +++ b/src/libs/subsonic/impl/SubsonicResponse.hpp @@ -25,6 +25,7 @@ #include #include +#include "utils/LiteralString.hpp" #include "RequestContext.hpp" namespace API::Subsonic @@ -205,18 +206,7 @@ namespace API::Subsonic class Node { public: - class Key - { - public: - template - constexpr Key(const char (&str)[N]) : _str{ str } {} - constexpr std::string_view get() const { return _str; } - - bool constexpr operator<(const Key& other) const { return _str < other._str; } - - private: - const std::string_view _str; - }; + using Key = LiteralString; void setAttribute(Key key, std::string_view value);