Skip to content

Commit 864a3aa

Browse files
committed
Fix TgTypeParser of InlineQuery
1 parent 0eca000 commit 864a3aa

File tree

1 file changed

+20
-26
lines changed

1 file changed

+20
-26
lines changed

src/TgTypeParser.cpp

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
#include "tgbot/TgTypeParser.h"
1+
#include <tgbot/TgTypeParser.h>
2+
#include <tgbot/TgException.h>
23

34
#include <json/value.h>
45

56
#include <cstdint>
67
#include <utility>
78

8-
#include "tgbot/types/InlineKeyboardMarkup.h"
9-
109
namespace TgBot {
1110

1211
// T should be instance of std::shared_ptr.
@@ -3342,33 +3341,28 @@ DECLARE_PARSER_TO_JSON(InlineKeyboardButton) {
33423341
return ptree;
33433342
}
33443343

3344+
template <typename T, typename CachedT>
3345+
auto put(const InlineQueryResult::Ptr& ptr) {
3346+
if (const auto cached = std::dynamic_pointer_cast<CachedT>(ptr)) {
3347+
return put<typename CachedT::Ptr>(cached);
3348+
} else if (const auto result = std::dynamic_pointer_cast<T>(ptr)) {
3349+
return put<typename T::Ptr>(result);
3350+
} else {
3351+
throw TgBot::TgException("Invalid inline query result type", TgException::ErrorCode::Internal);
3352+
}
3353+
}
3354+
33453355
DECLARE_PARSER_TO_JSON(InlineQueryResult) {
33463356
if (!object) return {};
33473357
JsonWrapper ptree;
33483358
ptree.put("type", object->type);
33493359
ptree.put("id", object->id);
33503360
ptree.put("reply_markup", put(object->replyMarkup));
33513361

3352-
if (object->type == InlineQueryResultCachedAudio::TYPE) {
3353-
ptree.put("data", put<InlineQueryResultCachedAudio>(object));
3354-
} else if (object->type == InlineQueryResultCachedDocument::TYPE) {
3355-
ptree.put("data", put<InlineQueryResultCachedDocument>(object));
3356-
} else if (object->type == InlineQueryResultCachedGif::TYPE) {
3357-
ptree.put("data", put<InlineQueryResultCachedGif>(object));
3358-
} else if (object->type == InlineQueryResultCachedMpeg4Gif::TYPE) {
3359-
ptree.put("data", put<InlineQueryResultCachedMpeg4Gif>(object));
3360-
} else if (object->type == InlineQueryResultCachedPhoto::TYPE) {
3361-
ptree.put("data", put<InlineQueryResultCachedPhoto>(object));
3362-
} else if (object->type == InlineQueryResultCachedSticker::TYPE) {
3363-
ptree.put("data", put<InlineQueryResultCachedSticker>(object));
3364-
} else if (object->type == InlineQueryResultCachedVideo::TYPE) {
3365-
ptree.put("data", put<InlineQueryResultCachedVideo>(object));
3366-
} else if (object->type == InlineQueryResultCachedVoice::TYPE) {
3367-
ptree.put("data", put<InlineQueryResultCachedVoice>(object));
3368-
} else if (object->type == InlineQueryResultArticle::TYPE) {
3362+
if (object->type == InlineQueryResultArticle::TYPE) {
33693363
ptree.put("data", put<InlineQueryResultArticle>(object));
33703364
} else if (object->type == InlineQueryResultAudio::TYPE) {
3371-
ptree.put("data", put<InlineQueryResultAudio>(object));
3365+
ptree.put("data", put<InlineQueryResultAudio, InlineQueryResultCachedAudio>(object));
33723366
} else if (object->type == InlineQueryResultContact::TYPE) {
33733367
ptree.put("data", put<InlineQueryResultContact>(object));
33743368
} else if (object->type == InlineQueryResultGame::TYPE) {
@@ -3380,15 +3374,15 @@ DECLARE_PARSER_TO_JSON(InlineQueryResult) {
33803374
} else if (object->type == InlineQueryResultVenue::TYPE) {
33813375
ptree.put("data", put<InlineQueryResultVenue>(object));
33823376
} else if (object->type == InlineQueryResultVoice::TYPE) {
3383-
ptree.put("data", put<InlineQueryResultVoice>(object));
3377+
ptree.put("data", put<InlineQueryResultVoice,InlineQueryResultCachedVoice>(object));
33843378
} else if (object->type == InlineQueryResultPhoto::TYPE) {
3385-
ptree.put("data", put<InlineQueryResultPhoto>(object));
3379+
ptree.put("data", put<InlineQueryResultPhoto, InlineQueryResultCachedPhoto>(object));
33863380
} else if (object->type == InlineQueryResultGif::TYPE) {
3387-
ptree.put("data", put<InlineQueryResultGif>(object));
3381+
ptree.put("data", put<InlineQueryResultGif, InlineQueryResultCachedGif>(object));
33883382
} else if (object->type == InlineQueryResultMpeg4Gif::TYPE) {
3389-
ptree.put("data", put<InlineQueryResultMpeg4Gif>(object));
3383+
ptree.put("data", put<InlineQueryResultMpeg4Gif, InlineQueryResultCachedMpeg4Gif>(object));
33903384
} else if (object->type == InlineQueryResultVideo::TYPE) {
3391-
ptree.put("data", put<InlineQueryResultVideo>(object));
3385+
ptree.put("data", put<InlineQueryResultVideo, InlineQueryResultCachedVideo>(object));
33923386
}
33933387
return ptree;
33943388
}

0 commit comments

Comments
 (0)