diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3e1ac5f7..f9b07763 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -75,9 +75,9 @@ jobs: mkdir -p build/doxygen doxygen 2>&1 | tee build/doxygen.log WARNINGS=$(cat build/doxygen.log | sort | uniq | grep -e ": warning: " | wc -l) - MAX_ALLOWED=865 + MAX_ALLOWED=840 echo "Doxygen finished with $WARNINGS warnings, max allowed: $MAX_ALLOWED" - if [ "$WARNINGS" -gt "$MAX_ALLOWED" ]; then exit 1; else exit 0; fi; + if [ "$WARNINGS" -gt "$MAX_ALLOWED" ]; then exit $WARNINGS; else exit 0; fi; unit-test: diff --git a/server/config.hpp b/server/config.hpp index 736bc0f4..3c97dddf 100644 --- a/server/config.hpp +++ b/server/config.hpp @@ -42,35 +42,44 @@ using ClientInfoPtr = std::shared_ptr; using GroupPtr = std::shared_ptr; -template -T jGet(const json& j, const std::string& what, const T& def) +struct JsonConfigItem { - try + /// Read config item from json object @p j + virtual void fromJson(const json& j) = 0; + /// @return config item serialized to json + virtual json toJson() = 0; + +protected: + /// @return value for key @p what or @p def, if not found. Result is casted to T. + template + T jGet(const json& j, const std::string& what, const T& def) { - if (!j.count(what)) + try + { + if (!j.count(what)) + return def; + return j[what].get(); + } + catch (...) + { return def; - return j[what].get(); - } - catch (...) - { - return def; + } } -} - +}; -struct Volume +struct Volume : public JsonConfigItem { Volume(uint16_t _percent = 100, bool _muted = false) : percent(_percent), muted(_muted) { } - void fromJson(const json& j) + void fromJson(const json& j) override { percent = jGet(j, "percent", percent); muted = jGet(j, "muted", muted); } - json toJson() + json toJson() override { json j; j["percent"] = percent; @@ -84,7 +93,7 @@ struct Volume -struct Host +struct Host : public JsonConfigItem { Host() : name(""), mac(""), os(""), arch(""), ip("") { @@ -97,7 +106,7 @@ struct Host arch = getArch(); } - void fromJson(const json& j) + void fromJson(const json& j) override { name = strutils::trim_copy(jGet(j, "name", "")); mac = strutils::trim_copy(jGet(j, "mac", "")); @@ -106,7 +115,7 @@ struct Host ip = strutils::trim_copy(jGet(j, "ip", "")); } - json toJson() + json toJson() override { json j; j["name"] = name; @@ -125,13 +134,13 @@ struct Host }; -struct ClientConfig +struct ClientConfig : public JsonConfigItem { ClientConfig() : name(""), volume(100), latency(0), instance(1) { } - void fromJson(const json& j) + void fromJson(const json& j) override { name = strutils::trim_copy(jGet(j, "name", "")); volume.fromJson(j["volume"]); @@ -139,7 +148,7 @@ struct ClientConfig instance = jGet(j, "instance", 1); } - json toJson() + json toJson() override { json j; j["name"] = strutils::trim_copy(name); @@ -157,7 +166,7 @@ struct ClientConfig -struct Snapcast +struct Snapcast : public JsonConfigItem { Snapcast(const std::string& _name = "", const std::string& _version = "") : name(_name), version(_version), protocolVersion(1) { @@ -165,14 +174,14 @@ struct Snapcast virtual ~Snapcast() = default; - virtual void fromJson(const json& j) + void fromJson(const json& j) override { name = strutils::trim_copy(jGet(j, "name", "")); version = strutils::trim_copy(jGet(j, "version", "")); protocolVersion = jGet(j, "protocolVersion", 1); } - virtual json toJson() + json toJson() override { json j; j["name"] = strutils::trim_copy(name); @@ -218,7 +227,7 @@ struct Snapserver : public Snapcast }; -struct ClientInfo +struct ClientInfo : public JsonConfigItem { ClientInfo(const std::string& _clientId = "") : id(_clientId), connected(false) { @@ -226,7 +235,7 @@ struct ClientInfo lastSeen.tv_usec = 0; } - void fromJson(const json& j) + void fromJson(const json& j) override { host.fromJson(j["host"]); id = jGet(j, "id", host.mac); @@ -237,7 +246,7 @@ struct ClientInfo connected = jGet(j, "connected", true); } - json toJson() + json toJson() override { json j; j["id"] = id; @@ -259,7 +268,7 @@ struct ClientInfo }; -struct Group +struct Group : public JsonConfigItem { Group(const ClientInfoPtr client = nullptr) : muted(false) { @@ -268,7 +277,7 @@ struct Group id = generateUUID(); } - void fromJson(const json& j) + void fromJson(const json& j) override { name = strutils::trim_copy(jGet(j, "name", "")); id = strutils::trim_copy(jGet(j, "id", "")); @@ -287,7 +296,7 @@ struct Group } } - json toJson() + json toJson() override { json j; j["name"] = strutils::trim_copy(name);