Enhanced Entity::valid
the build was successful Details

It now checks if all non-nullable attributes are set. fixes #10
This commit is contained in:
tastytea 2018-07-14 11:44:30 +02:00
parent 2565ff8d15
commit b74381ceed
Signed by: tastytea
GPG Key ID: 59346E0EA35C67E5
35 changed files with 293 additions and 13 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.7)
project (mastodon-cpp
VERSION 0.18.1
VERSION 0.18.2
LANGUAGES CXX
)

View File

@ -341,9 +341,9 @@ public:
/*!
* @brief Returns true if the Entity holds valid data
*
* @since before 0.11.0
* @since before 0.11.0 (virtual since 0.18.2)
*/
const bool valid() const;
virtual const bool valid() const = 0;
/*!
* @brief Returns error string sent by the server
@ -444,9 +444,20 @@ public:
const std::uint_fast64_t stouint64(const string &str) const;
/*!
* @brief Checks if an Entity is valid
*
* @param attributes The attributes to check
*
* @return true if all attributes are set
*
* @since 0.18.2
*/
const bool
check_valid(const std::vector<string> &attributes) const;
private:
Json::Value _tree;
bool _valid;
mutable bool _was_set;
};

View File

@ -14,6 +14,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <array>
#include "account.hpp"
#include "macros.hpp"
@ -28,6 +29,30 @@ Account::Account()
: Entity()
{}
const bool Account::valid() const
{
const std::vector<string> attributes =
{{
"id",
"username",
"acct",
"display_name",
"locked",
"created_at",
"followers_count",
"following_count",
"statuses_count",
"note",
"url",
"avatar",
"avatar_static",
"header",
"header_static"
}};
return Entity::check_valid(attributes);
}
const string Account::acct() const
{
return get_string("acct");

View File

@ -69,6 +69,8 @@ namespace Mastodon
*/
Account();
virtual const bool valid() const;
/*!
* @brief Returns username
*

View File

@ -27,6 +27,11 @@ Application::Application()
: Entity()
{}
const bool Application::valid() const
{
return Entity::check_valid({"name"});
}
const string Application::name() const
{
return get_string("name");

View File

@ -56,6 +56,8 @@ namespace Mastodon
*/
Application();
virtual const bool valid() const;
/*!
* @brief Returns the name of the application
*

View File

@ -29,6 +29,19 @@ Attachment::Attachment()
: Entity()
{}
const bool Attachment::valid() const
{
const std::vector<string> attributes =
{{
"id",
"type",
"url",
"preview_url"
}};
return Entity::check_valid(attributes);
}
const double Attachment::aspect() const
{
return get_double("meta.original.aspect");

View File

@ -60,6 +60,8 @@ namespace Mastodon
*/
Attachment();
virtual const bool valid() const;
/*!
* @brief Aspect of original image
*

View File

@ -28,6 +28,19 @@ Card::Card()
: Entity()
{}
const bool Card::valid() const
{
const std::vector<string> attributes =
{{
"url",
"title",
"description",
"type"
}};
return Entity::check_valid(attributes);
}
const string Card::author_name() const
{
return get_string("author_name");

View File

@ -58,6 +58,8 @@ namespace Mastodon
*/
Card();
virtual const bool valid() const;
/*!
* @brief Returns the name of the author
*

View File

@ -28,6 +28,17 @@ Context::Context()
: Entity()
{}
const bool Context::valid() const
{
const std::vector<string> attributes =
{{
"ancestors",
"descendants"
}};
return Entity::check_valid(attributes);
}
const std::vector<Easy::Status> Context::ancestors() const
{
const Json::Value node = get("ancestors");

View File

@ -59,6 +59,8 @@ namespace Mastodon
*/
Context();
virtual const bool valid() const;
/*!
* @brief Returns the ancestors of the Status as vector of Statuses
*

View File

@ -28,6 +28,18 @@ Emoji::Emoji()
: Entity()
{}
const bool Emoji::valid() const
{
const std::vector<string> attributes =
{{
"shortcode",
"static_url",
"url"
}};
return Entity::check_valid(attributes);
}
const string Emoji::shortcode() const
{
return get_string("shortcode");

View File

@ -56,6 +56,8 @@ namespace Mastodon
*/
Emoji();
virtual const bool valid() const;
/*!
* @brief Returns the shortcode of the emoji
*

View File

@ -30,6 +30,23 @@ Instance::Instance()
: Entity()
{}
const bool Instance::valid() const
{
const std::vector<string> attributes =
{{
"uri",
"title",
"description",
"email",
"version",
"urls",
"languages",
"contact_account"
}};
return Entity::check_valid(attributes);
}
const Easy::Account Instance::contact_account() const
{
const Json::Value node = get("contact_account");

View File

@ -59,6 +59,8 @@ namespace Mastodon
*/
Instance();
virtual const bool valid() const;
/*!
* @brief Returns the Account of the admin or another contact person
*

View File

@ -29,6 +29,17 @@ List::List()
: Entity()
{}
const bool List::valid() const
{
const std::vector<string> attributes =
{{
"id",
"title"
}};
return Entity::check_valid(attributes);
}
const uint_fast64_t List::id() const
{
return stouint64(get_string("id"));

View File

@ -59,6 +59,8 @@ namespace Mastodon
*/
List();
virtual const bool valid() const;
/*!
* @brief Returns list-ID
*

View File

@ -27,6 +27,19 @@ Mention::Mention()
: Entity()
{}
const bool Mention::valid() const
{
const std::vector<string> attributes =
{{
"url",
"username",
"acct",
"id"
}};
return Entity::check_valid(attributes);
}
const string Mention::acct() const
{
return get_string("acct");

View File

@ -59,6 +59,8 @@ namespace Mastodon
*/
Mention();
virtual const bool valid() const;
/*!
* @brief Returns acct
*

View File

@ -28,6 +28,19 @@ Notification::Notification()
: Entity()
{}
const bool Notification::valid() const
{
const std::vector<string> attributes =
{{
"id",
"type",
"created_at",
"account"
}};
return Entity::check_valid(attributes);
}
const Easy::Account Notification::account() const
{
const Json::Value node = get("account");

View File

@ -64,6 +64,8 @@ namespace Mastodon
*/
Notification();
virtual const bool valid() const;
/*!
* @brief Returns the Account sending the notification to the user
*

View File

@ -32,6 +32,18 @@ PushSubscription::PushSubscription()
: Entity()
{}
const bool PushSubscription::valid() const
{
const std::vector<string> attributes =
{{
"id",
"endpoint",
"server_key"
}};
return Entity::check_valid(attributes);
}
const string PushSubscription::endpoint() const
{
return get_string("endpoint");

View File

@ -59,6 +59,8 @@ namespace Mastodon
*/
PushSubscription();
virtual const bool valid() const;
/*!
* @brief Returns push subscription ID
*

View File

@ -27,6 +27,23 @@ Relationship::Relationship()
: Entity()
{}
const bool Relationship::valid() const
{
const std::vector<string> attributes =
{{
"id",
"following",
"followed_by",
"blocking",
"muting",
"muting_notifications",
"requested",
"domain_blocking"
}};
return Entity::check_valid(attributes);
}
const bool Relationship::blocking() const
{
return get_bool("blocking");

View File

@ -58,6 +58,8 @@ namespace Mastodon
*/
Relationship();
virtual const bool valid() const;
/*!
* @brief Returns true if the user is blocking the account
*

View File

@ -27,6 +27,17 @@ Report::Report()
: Entity()
{}
const bool Report::valid() const
{
const std::vector<string> attributes =
{{
"id",
"action_taken"
}};
return Entity::check_valid(attributes);
}
const bool Report::action_taken() const
{
return get_bool("action_taken");

View File

@ -58,6 +58,8 @@ namespace Mastodon
*/
Report();
virtual const bool valid() const;
/*!
* @brief Returns true if an action was taken in response to the
* report

View File

@ -28,6 +28,18 @@ Results::Results()
: Entity()
{}
const bool Results::valid() const
{
const std::vector<string> attributes =
{{
"accounts",
"statuses",
"hashtags"
}};
return Entity::check_valid(attributes);
}
const std::vector<Easy::Account> Results::accounts() const
{
const Json::Value node = get("accounts");

View File

@ -63,6 +63,8 @@ namespace Mastodon
*/
Results();
virtual const bool valid() const;
/*!
* @brief Returns an array of matched Accounts
*

View File

@ -29,6 +29,29 @@ Status::Status()
: Entity()
{}
const bool Status::valid() const
{
const std::vector<string> attributes =
{{
"id",
"uri",
"account",
"content",
"created_at",
"emojis",
"reblogs_count",
"favourites_count",
"sensitive",
"spoiler_text",
"visibility",
"media_attachments",
"mentions",
"tags"
}};
return Entity::check_valid(attributes);
}
const Easy::Account Status::account() const
{
const Json::Value node = get("account");

View File

@ -73,6 +73,8 @@ namespace Mastodon
*/
Status();
virtual const bool valid() const;
/*!
* @brief Returns an array of matched accounts.
*

View File

@ -30,6 +30,17 @@ Tag::Tag()
: Entity()
{}
const bool Tag::valid() const
{
const std::vector<string> attributes =
{{
"name",
"url"
}};
return Entity::check_valid(attributes);
}
const string Tag::name() const
{
return get_string("name");
@ -66,6 +77,18 @@ Tag::History::History()
: Entity()
{}
const bool Tag::History::valid() const
{
const std::vector<string> attributes =
{{
"day",
"uses",
"accounts"
}};
return Entity::check_valid(attributes);
}
const uint_fast64_t Tag::History::accounts()
{
return stouint64(get_string("accounts"));

View File

@ -68,6 +68,8 @@ namespace Mastodon
*/
History();
virtual const bool valid() const;
/*!
* @brief Returns the number of accounts using that hashtag.
*
@ -106,6 +108,8 @@ namespace Mastodon
*/
Tag();
virtual const bool valid() const;
/*!
* @brief Returns the name of the tag
*

View File

@ -28,7 +28,6 @@ using std::chrono::system_clock;
Easy::Entity::Entity(const string &json)
: _tree(Json::nullValue)
, _valid(false)
,_was_set(false)
{
from_string(json);
@ -56,10 +55,6 @@ const void Easy::Entity::from_string(const string &json)
ttdebug << "ERROR: Server returned an error\n";
ttdebug << "String was: " << json << '\n';
}
else
{
_valid = true;
}
}
const Json::Value Easy::Entity::to_object() const
@ -68,13 +63,22 @@ const Json::Value Easy::Entity::to_object() const
}
Easy::Entity::Entity()
: _valid(false)
, _was_set(false)
: _was_set(false)
{}
const bool Easy::Entity::valid() const
const bool
Easy::Entity::check_valid(const std::vector<string> &attributes) const
{
return _valid;
for (const string &attribute: attributes)
{
get(attribute);
if (!was_set())
{
return false;
}
}
return true;
}
const string Easy::Entity::error() const