Skip to content

Commit

Permalink
Fix TgTypeParser of InlineQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
Royna2544 committed Oct 15, 2024
1 parent 0eca000 commit 864a3aa
Showing 1 changed file with 20 additions and 26 deletions.
46 changes: 20 additions & 26 deletions src/TgTypeParser.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#include "tgbot/TgTypeParser.h"
#include <tgbot/TgTypeParser.h>
#include <tgbot/TgException.h>

#include <json/value.h>

#include <cstdint>
#include <utility>

#include "tgbot/types/InlineKeyboardMarkup.h"

namespace TgBot {

// T should be instance of std::shared_ptr.
Expand Down Expand Up @@ -3342,33 +3341,28 @@ DECLARE_PARSER_TO_JSON(InlineKeyboardButton) {
return ptree;
}

template <typename T, typename CachedT>
auto put(const InlineQueryResult::Ptr& ptr) {
if (const auto cached = std::dynamic_pointer_cast<CachedT>(ptr)) {
return put<typename CachedT::Ptr>(cached);
} else if (const auto result = std::dynamic_pointer_cast<T>(ptr)) {
return put<typename T::Ptr>(result);
} else {
throw TgBot::TgException("Invalid inline query result type", TgException::ErrorCode::Internal);
}
}

DECLARE_PARSER_TO_JSON(InlineQueryResult) {
if (!object) return {};
JsonWrapper ptree;
ptree.put("type", object->type);
ptree.put("id", object->id);
ptree.put("reply_markup", put(object->replyMarkup));

if (object->type == InlineQueryResultCachedAudio::TYPE) {
ptree.put("data", put<InlineQueryResultCachedAudio>(object));
} else if (object->type == InlineQueryResultCachedDocument::TYPE) {
ptree.put("data", put<InlineQueryResultCachedDocument>(object));
} else if (object->type == InlineQueryResultCachedGif::TYPE) {
ptree.put("data", put<InlineQueryResultCachedGif>(object));
} else if (object->type == InlineQueryResultCachedMpeg4Gif::TYPE) {
ptree.put("data", put<InlineQueryResultCachedMpeg4Gif>(object));
} else if (object->type == InlineQueryResultCachedPhoto::TYPE) {
ptree.put("data", put<InlineQueryResultCachedPhoto>(object));
} else if (object->type == InlineQueryResultCachedSticker::TYPE) {
ptree.put("data", put<InlineQueryResultCachedSticker>(object));
} else if (object->type == InlineQueryResultCachedVideo::TYPE) {
ptree.put("data", put<InlineQueryResultCachedVideo>(object));
} else if (object->type == InlineQueryResultCachedVoice::TYPE) {
ptree.put("data", put<InlineQueryResultCachedVoice>(object));
} else if (object->type == InlineQueryResultArticle::TYPE) {
if (object->type == InlineQueryResultArticle::TYPE) {
ptree.put("data", put<InlineQueryResultArticle>(object));
} else if (object->type == InlineQueryResultAudio::TYPE) {
ptree.put("data", put<InlineQueryResultAudio>(object));
ptree.put("data", put<InlineQueryResultAudio, InlineQueryResultCachedAudio>(object));
} else if (object->type == InlineQueryResultContact::TYPE) {
ptree.put("data", put<InlineQueryResultContact>(object));
} else if (object->type == InlineQueryResultGame::TYPE) {
Expand All @@ -3380,15 +3374,15 @@ DECLARE_PARSER_TO_JSON(InlineQueryResult) {
} else if (object->type == InlineQueryResultVenue::TYPE) {
ptree.put("data", put<InlineQueryResultVenue>(object));
} else if (object->type == InlineQueryResultVoice::TYPE) {
ptree.put("data", put<InlineQueryResultVoice>(object));
ptree.put("data", put<InlineQueryResultVoice,InlineQueryResultCachedVoice>(object));
} else if (object->type == InlineQueryResultPhoto::TYPE) {
ptree.put("data", put<InlineQueryResultPhoto>(object));
ptree.put("data", put<InlineQueryResultPhoto, InlineQueryResultCachedPhoto>(object));
} else if (object->type == InlineQueryResultGif::TYPE) {
ptree.put("data", put<InlineQueryResultGif>(object));
ptree.put("data", put<InlineQueryResultGif, InlineQueryResultCachedGif>(object));
} else if (object->type == InlineQueryResultMpeg4Gif::TYPE) {
ptree.put("data", put<InlineQueryResultMpeg4Gif>(object));
ptree.put("data", put<InlineQueryResultMpeg4Gif, InlineQueryResultCachedMpeg4Gif>(object));
} else if (object->type == InlineQueryResultVideo::TYPE) {
ptree.put("data", put<InlineQueryResultVideo>(object));
ptree.put("data", put<InlineQueryResultVideo, InlineQueryResultCachedVideo>(object));
}
return ptree;
}
Expand Down

0 comments on commit 864a3aa

Please sign in to comment.