Skip to content

Commit 66b89bf

Browse files
committed
Wrap optional fields with boost::optional (LinkPreviewOptions, ReplyParameters only)
1 parent da74ac4 commit 66b89bf

File tree

3 files changed

+37
-33
lines changed

3 files changed

+37
-33
lines changed

include/tgbot/types/LinkPreviewOptions.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <memory>
55
#include <string>
6+
#include <boost/optional.hpp>
67

78
namespace TgBot {
89

@@ -19,29 +20,29 @@ class LinkPreviewOptions {
1920
/**
2021
* @brief Optional. True, if the link preview is disabled
2122
*/
22-
bool isDisabled;
23+
boost::optional<bool> isDisabled;
2324

2425
/**
2526
* @brief Optional. URL to use for the link preview.
2627
*
2728
* If empty, then the first URL found in the message text will be used
2829
*/
29-
std::string url;
30+
boost::optional<std::string> url;
3031

3132
/**
3233
* @brief Optional. True, if the media in the link preview is supposed to be shrunk; ignored if the URL isn't explicitly specified or media size change isn't supported for the preview
3334
*/
34-
bool preferSmallMedia;
35+
boost::optional<bool> preferSmallMedia;
3536

3637
/**
3738
* @brief Optional. True, if the media in the link preview is supposed to be enlarged; ignored if the URL isn't explicitly specified or media size change isn't supported for the preview
3839
*/
39-
bool preferLargeMedia;
40+
boost::optional<bool> preferLargeMedia;
4041

4142
/**
4243
* @brief Optional. True, if the link preview must be shown above the message text; otherwise, the link preview will be shown below the message text
4344
*/
44-
bool showAboveText;
45+
boost::optional<bool> showAboveText;
4546
};
4647
}
4748

include/tgbot/types/ReplyParameters.h

+7-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <memory>
88
#include <string>
99
#include <vector>
10+
#include <boost/optional.hpp>
1011

1112
namespace TgBot {
1213

@@ -30,42 +31,42 @@ class ReplyParameters {
3031
*
3132
* Not supported for messages sent on behalf of a business account.
3233
*/
33-
std::int64_t chatId;
34+
boost::optional<std::int64_t> chatId;
3435

3536
/**
3637
* @brief Optional. Pass True if the message should be sent even if the specified message to be replied to is not found.
3738
*
3839
* Always False for replies in another chat or forum topic.
3940
* Always True for messages sent on behalf of a business account.
4041
*/
41-
bool allowSendingWithoutReply;
42+
boost::optional<bool> allowSendingWithoutReply;
4243

4344
/**
4445
* @brief Optional. Quoted part of the message to be replied to; 0-1024 characters after entities parsing.
4546
*
4647
* The quote must be an exact substring of the message to be replied to, including bold, italic, underline, strikethrough, spoiler, and customEmoji entities.
4748
* The message will fail to send if the quote isn't found in the original message.
4849
*/
49-
std::string quote;
50+
boost::optional<std::string> quote;
5051

5152
/**
5253
* @brief Optional. Mode for parsing entities in the quote.
5354
*
5455
* See [formatting options](https://core.telegram.org/bots/api#formatting-options) for more details.
5556
*/
56-
std::string quoteParseMode;
57+
boost::optional<std::string> quoteParseMode;
5758

5859
/**
5960
* @brief Optional. A JSON-serialized list of special entities that appear in the quote.
6061
*
6162
* It can be specified instead of quoteParseMode.
6263
*/
63-
std::vector<MessageEntity::Ptr> quoteEntities;
64+
boost::optional<std::vector<MessageEntity::Ptr>> quoteEntities;
6465

6566
/**
6667
* @brief Optional. Position of the quote in the original message in UTF-16 code units
6768
*/
68-
std::int32_t quotePosition;
69+
boost::optional<std::int32_t> quotePosition;
6970
};
7071
}
7172

src/TgTypeParser.cpp

+24-22
Original file line numberDiff line numberDiff line change
@@ -675,12 +675,14 @@ std::string TgTypeParser::parseExternalReplyInfo(const ExternalReplyInfo::Ptr& o
675675
ReplyParameters::Ptr TgTypeParser::parseJsonAndGetReplyParameters(const boost::property_tree::ptree& data) const {
676676
auto result(std::make_shared<ReplyParameters>());
677677
result->messageId = data.get<std::int32_t>("message_id", 0);
678-
result->chatId = data.get<std::int64_t>("chat_id", 0);
679-
result->allowSendingWithoutReply = data.get<bool>("allow_sending_without_reply", false);
680-
result->quote = data.get<std::string>("quote", "");
681-
result->quoteParseMode = data.get<std::string>("quote_parse_mode", "");
682-
result->quoteEntities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "quote_entities");
683-
result->quotePosition = data.get<std::int32_t>("quote_position", 0);
678+
result->chatId = data.get_optional<std::int64_t>("chat_id");
679+
result->allowSendingWithoutReply = data.get_optional<bool>("allow_sending_without_reply");
680+
result->quote = data.get_optional<std::string>("quote");
681+
result->quoteParseMode = data.get_optional<std::string>("quote_parse_mode");
682+
if (data.find("quote_entities") != data.not_found()) {
683+
result->quoteEntities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "quote_entities");
684+
}
685+
result->quotePosition = data.get_optional<std::int32_t>("quote_position");
684686
return result;
685687
}
686688

@@ -691,12 +693,12 @@ std::string TgTypeParser::parseReplyParameters(const ReplyParameters::Ptr& objec
691693
std::string result;
692694
result += '{';
693695
appendToJson(result, "message_id", object->messageId);
694-
appendToJson(result, "chat_id", object->chatId);
695-
appendToJson(result, "allow_sending_without_reply", object->allowSendingWithoutReply);
696-
appendToJson(result, "quote", object->quote);
697-
appendToJson(result, "quote_parse_mode", object->quoteParseMode);
698-
appendToJson(result, "quote_entities", parseArray(&TgTypeParser::parseMessageEntity, object->quoteEntities));
699-
appendToJson(result, "quote_position", object->quotePosition);
696+
if (object->chatId) appendToJson(result, "chat_id", *(object->chatId));
697+
if (object->allowSendingWithoutReply) appendToJson(result, "allow_sending_without_reply", *(object->allowSendingWithoutReply));
698+
if (object->quote) appendToJson(result, "quote", *(object->quote));
699+
if (object->quoteParseMode) appendToJson(result, "quote_parse_mode", *(object->quoteParseMode));
700+
if (object->quoteEntities) appendToJson(result, "quote_entities", parseArray(&TgTypeParser::parseMessageEntity, *(object->quoteEntities)));
701+
if (object->quotePosition) appendToJson(result, "quote_position", *(object->quotePosition));
700702
removeLastComma(result);
701703
result += '}';
702704
return result;
@@ -1720,11 +1722,11 @@ std::string TgTypeParser::parseGiveawayCompleted(const GiveawayCompleted::Ptr& o
17201722

17211723
LinkPreviewOptions::Ptr TgTypeParser::parseJsonAndGetLinkPreviewOptions(const boost::property_tree::ptree& data) const {
17221724
auto result(std::make_shared<LinkPreviewOptions>());
1723-
result->isDisabled = data.get<bool>("is_disabled", false);
1724-
result->url = data.get<std::string>("url", "");
1725-
result->preferSmallMedia = data.get<bool>("prefer_small_media", false);
1726-
result->preferLargeMedia = data.get<bool>("prefer_large_media", false);
1727-
result->showAboveText = data.get<bool>("show_above_text", false);
1725+
result->isDisabled = data.get_optional<bool>("is_disabled");
1726+
result->url = data.get_optional<std::string>("url");
1727+
result->preferSmallMedia = data.get_optional<bool>("prefer_small_media");
1728+
result->preferLargeMedia = data.get_optional<bool>("prefer_large_media");
1729+
result->showAboveText = data.get_optional<bool>("show_above_text");
17281730
return result;
17291731
}
17301732

@@ -1734,11 +1736,11 @@ std::string TgTypeParser::parseLinkPreviewOptions(const LinkPreviewOptions::Ptr&
17341736
}
17351737
std::string result;
17361738
result += '{';
1737-
appendToJson(result, "is_disabled", object->isDisabled);
1738-
appendToJson(result, "url", object->url);
1739-
appendToJson(result, "prefer_small_media", object->preferSmallMedia);
1740-
appendToJson(result, "prefer_large_media", object->preferLargeMedia);
1741-
appendToJson(result, "show_above_text", object->showAboveText);
1739+
if (object->isDisabled) appendToJson(result, "is_disabled", *(object->isDisabled));
1740+
if (object->url) appendToJson(result, "url", *(object->url));
1741+
if (object->preferSmallMedia) appendToJson(result, "prefer_small_media", *(object->preferSmallMedia));
1742+
if (object->preferLargeMedia) appendToJson(result, "prefer_large_media", *(object->preferLargeMedia));
1743+
if (object->showAboveText) appendToJson(result, "show_above_text", *(object->showAboveText));
17421744
removeLastComma(result);
17431745
result += '}';
17441746
return result;

0 commit comments

Comments
 (0)