Changed coding style

This commit is contained in:
emeric
2023-10-02 20:32:28 +02:00
parent 4fbacd691d
commit 7c7fbba319
18 changed files with 1265 additions and 1311 deletions
+182 -199
View File
@@ -34,249 +34,232 @@
namespace API::Subsonic
{
std::string
ResponseFormatToMimeType(ResponseFormat format)
{
switch (format)
{
case ResponseFormat::xml: return "text/xml";
case ResponseFormat::json: return "application/json";
}
std::string ResponseFormatToMimeType(ResponseFormat format)
{
switch (format)
{
case ResponseFormat::xml: return "text/xml";
case ResponseFormat::json: return "application/json";
}
return "";
}
return "";
}
void
Response::Node::setValue(std::string_view value)
{
if (!_children.empty() || !_childrenArrays.empty())
throw LmsException {"Node already has children"};
void Response::Node::setValue(std::string_view value)
{
if (!_children.empty() || !_childrenArrays.empty())
throw LmsException{ "Node already has children" };
_value = std::string {value};
}
_value = std::string{ value };
}
void
Response::Node::setValue(long long value)
{
if (!_children.empty() || !_childrenArrays.empty())
throw LmsException {"Node already has children"};
void Response::Node::setValue(long long value)
{
if (!_children.empty() || !_childrenArrays.empty())
throw LmsException{ "Node already has children" };
_value = value;
}
_value = value;
}
void
Response::Node::setAttribute(std::string_view key, std::string_view value)
{
_attributes[std::string {key}] = std::string {value};
}
void Response::Node::setAttribute(std::string_view key, std::string_view value)
{
_attributes[std::string{ key }] = std::string{ value };
}
void
Response::Node::addChild(const std::string& key, Node node)
{
if (_value)
throw LmsException {"Node already has a value"};
void Response::Node::addChild(const std::string& key, Node node)
{
if (_value)
throw LmsException{ "Node already has a value" };
_children[key].emplace_back(std::move(node));
}
_children[key].emplace_back(std::move(node));
}
void
Response::Node::addArrayChild(const std::string& key, Node node)
{
if (_value)
throw LmsException {"Node already has a value"};
void Response::Node::addArrayChild(const std::string& key, Node node)
{
if (_value)
throw LmsException{ "Node already has a value" };
_childrenArrays[key].emplace_back(std::move(node));
}
_childrenArrays[key].emplace_back(std::move(node));
}
Response::Node&
Response::Node::createChild(const std::string& key)
{
_children[key].emplace_back();
return _children[key].back();
}
Response::Node& Response::Node::createChild(const std::string& key)
{
_children[key].emplace_back();
return _children[key].back();
}
Response::Node&
Response::Node::createArrayChild(const std::string& key)
{
_childrenArrays[key].emplace_back();
return _childrenArrays[key].back();
}
Response::Node& Response::Node::createArrayChild(const std::string& key)
{
_childrenArrays[key].emplace_back();
return _childrenArrays[key].back();
}
void
Response::Node::setVersionAttribute(ProtocolVersion protocolVersion)
{
setAttribute("version", std::to_string(protocolVersion.major) + "." + std::to_string(protocolVersion.minor) + "." + std::to_string(protocolVersion.patch));
}
void Response::Node::setVersionAttribute(ProtocolVersion protocolVersion)
{
setAttribute("version", std::to_string(protocolVersion.major) + "." + std::to_string(protocolVersion.minor) + "." + std::to_string(protocolVersion.patch));
}
Response
Response::createOkResponse(ProtocolVersion protocolVersion)
{
Response response;
Node& responseNode {response._root.createChild("subsonic-response")};
Response Response::createOkResponse(ProtocolVersion protocolVersion)
{
Response response;
Node& responseNode{ response._root.createChild("subsonic-response") };
responseNode.setAttribute("status", "ok");
responseNode.setVersionAttribute(protocolVersion);
responseNode.setAttribute("type", "lms"); // non standard field to ease client hacks
responseNode.setAttribute("status", "ok");
responseNode.setVersionAttribute(protocolVersion);
responseNode.setAttribute("type", "lms"); // non standard field to ease client hacks
return response;
}
return response;
}
Response
Response::createFailedResponse(ProtocolVersion protocolVersion, const Error& error)
{
Response response;
Node& responseNode {response._root.createChild("subsonic-response")};
Response Response::createFailedResponse(ProtocolVersion protocolVersion, const Error& error)
{
Response response;
Node& responseNode{ response._root.createChild("subsonic-response") };
responseNode.setAttribute("status", "failed");
responseNode.setVersionAttribute(protocolVersion);
responseNode.setAttribute("type", "lms"); // non standard field to ease client hacks
responseNode.setAttribute("status", "failed");
responseNode.setVersionAttribute(protocolVersion);
responseNode.setAttribute("type", "lms"); // non standard field to ease client hacks
Node& errorNode {responseNode.createChild("error")};
errorNode.setAttribute("code", static_cast<int>(error.getCode()));
errorNode.setAttribute("message", error.getMessage());
Node& errorNode{ responseNode.createChild("error") };
errorNode.setAttribute("code", static_cast<int>(error.getCode()));
errorNode.setAttribute("message", error.getMessage());
return response;
}
return response;
}
void
Response::addNode(const std::string& key, Node node)
{
return _root._children["subsonic-response"].front().addChild(key, std::move(node));
}
void Response::addNode(const std::string& key, Node node)
{
return _root._children["subsonic-response"].front().addChild(key, std::move(node));
}
Response::Node&
Response::createNode(const std::string& key)
{
return _root._children["subsonic-response"].front().createChild(key);
}
Response::Node& Response::createNode(const std::string& key)
{
return _root._children["subsonic-response"].front().createChild(key);
}
Response::Node&
Response::createArrayNode(const std::string& key)
{
return _root._children["subsonic-response"].front().createArrayChild(key);
}
Response::Node& Response::createArrayNode(const std::string& key)
{
return _root._children["subsonic-response"].front().createArrayChild(key);
}
void
Response::write(std::ostream& os, ResponseFormat format)
{
switch (format)
{
case ResponseFormat::xml:
writeXML(os);
break;
case ResponseFormat::json:
writeJSON(os);
break;
}
}
void Response::write(std::ostream& os, ResponseFormat format)
{
switch (format)
{
case ResponseFormat::xml:
writeXML(os);
break;
case ResponseFormat::json:
writeJSON(os);
break;
}
}
void
Response::writeXML(std::ostream& os)
{
std::function<boost::property_tree::ptree(const Response::Node&)> nodeToPropertyTree = [&] (const Response::Node& node)
{
boost::property_tree::ptree res;
void Response::writeXML(std::ostream& os)
{
std::function<boost::property_tree::ptree(const Response::Node&)> nodeToPropertyTree = [&](const Response::Node& node)
{
boost::property_tree::ptree res;
for (auto itAttribute : node._attributes)
{
if (std::holds_alternative<std::string>(itAttribute.second))
res.put("<xmlattr>." + itAttribute.first, std::get<std::string>(itAttribute.second));
else if (std::holds_alternative<bool>(itAttribute.second))
res.put("<xmlattr>." + itAttribute.first, std::get<bool>(itAttribute.second));
else if (std::holds_alternative<long long>(itAttribute.second))
res.put("<xmlattr>." + itAttribute.first, std::get<long long>(itAttribute.second));
}
for (auto itAttribute : node._attributes)
{
if (std::holds_alternative<std::string>(itAttribute.second))
res.put("<xmlattr>." + itAttribute.first, std::get<std::string>(itAttribute.second));
else if (std::holds_alternative<bool>(itAttribute.second))
res.put("<xmlattr>." + itAttribute.first, std::get<bool>(itAttribute.second));
else if (std::holds_alternative<long long>(itAttribute.second))
res.put("<xmlattr>." + itAttribute.first, std::get<long long>(itAttribute.second));
}
if (node._value)
{
const auto& value {*node._value};
if (node._value)
{
const auto& value{ *node._value };
if (std::holds_alternative<std::string>(value))
res.put_value(std::get<std::string>(value));
else if (std::holds_alternative<bool>(value))
res.put_value(std::get<bool>(value));
else if (std::holds_alternative<long long>(value))
res.put_value(std::get<long long>(value));
}
else
{
for (auto itChildNode : node._children)
{
for (const Response::Node& childNode : itChildNode.second)
res.add_child(itChildNode.first, nodeToPropertyTree(childNode));
}
if (std::holds_alternative<std::string>(value))
res.put_value(std::get<std::string>(value));
else if (std::holds_alternative<bool>(value))
res.put_value(std::get<bool>(value));
else if (std::holds_alternative<long long>(value))
res.put_value(std::get<long long>(value));
}
else
{
for (auto itChildNode : node._children)
{
for (const Response::Node& childNode : itChildNode.second)
res.add_child(itChildNode.first, nodeToPropertyTree(childNode));
}
for (auto itChildArrayNode : node._childrenArrays)
{
const std::vector<Response::Node>& childArrayNodes {itChildArrayNode.second};
for (auto itChildArrayNode : node._childrenArrays)
{
const std::vector<Response::Node>& childArrayNodes{ itChildArrayNode.second };
for (const Response::Node& childNode : childArrayNodes )
res.add_child(itChildArrayNode.first, nodeToPropertyTree(childNode));
}
}
for (const Response::Node& childNode : childArrayNodes)
res.add_child(itChildArrayNode.first, nodeToPropertyTree(childNode));
}
}
return res;
};
return res;
};
boost::property_tree::ptree root {nodeToPropertyTree(_root)};
boost::property_tree::write_xml(os, root);
}
boost::property_tree::ptree root{ nodeToPropertyTree(_root) };
boost::property_tree::write_xml(os, root);
}
void
Response::writeJSON(std::ostream& os)
{
namespace Json = Wt::Json;
void Response::writeJSON(std::ostream& os)
{
namespace Json = Wt::Json;
std::function<Json::Object(const Response::Node&)> nodeToJsonObject = [&] (const Response::Node& node)
{
Json::Object res;
std::function<Json::Object(const Response::Node&)> nodeToJsonObject = [&](const Response::Node& node)
{
Json::Object res;
auto valueToJsonValue {[](const Node::ValueType& value) -> Json::Value
{
if (std::holds_alternative<std::string>(value))
return Json::Value {std::get<std::string>(value)};
else if (std::holds_alternative<bool>(value))
return Json::Value {std::get<bool>(value)};
else if (std::holds_alternative<long long>(value))
return Json::Value {std::get<long long>(value)};
auto valueToJsonValue{ [](const Node::ValueType& value) -> Json::Value
{
if (std::holds_alternative<std::string>(value))
return Json::Value {std::get<std::string>(value)};
else if (std::holds_alternative<bool>(value))
return Json::Value {std::get<bool>(value)};
else if (std::holds_alternative<long long>(value))
return Json::Value {std::get<long long>(value)};
throw LmsException("Unexpected value type");
}};
throw LmsException("Unexpected value type");
} };
for (auto itAttribute : node._attributes)
res[itAttribute.first] = valueToJsonValue(itAttribute.second);
for (auto itAttribute : node._attributes)
res[itAttribute.first] = valueToJsonValue(itAttribute.second);
if (node._value)
{
res["value"] = valueToJsonValue(*node._value);
}
else
{
for (auto itChildNode : node._children)
{
for (const Response::Node& childNode : itChildNode.second)
res[itChildNode.first] = nodeToJsonObject(childNode);
}
if (node._value)
{
res["value"] = valueToJsonValue(*node._value);
}
else
{
for (auto itChildNode : node._children)
{
for (const Response::Node& childNode : itChildNode.second)
res[itChildNode.first] = nodeToJsonObject(childNode);
}
for (auto itChildArrayNode : node._childrenArrays)
{
const std::vector<Response::Node>& childArrayNodes {itChildArrayNode .second};
for (auto itChildArrayNode : node._childrenArrays)
{
const std::vector<Response::Node>& childArrayNodes{ itChildArrayNode.second };
Json::Array array;
for (const Response::Node& childNode : childArrayNodes )
array.emplace_back(nodeToJsonObject(childNode));
Json::Array array;
for (const Response::Node& childNode : childArrayNodes)
array.emplace_back(nodeToJsonObject(childNode));
res[itChildArrayNode.first] = std::move(array);
}
}
res[itChildArrayNode.first] = std::move(array);
}
}
return res;
};
return res;
};
Json::Object root {nodeToJsonObject(_root)};
os << Json::serialize(root);
}
Json::Object root{ nodeToJsonObject(_root) };
os << Json::serialize(root);
}
} // namespace