Add get_json_value_string helper function.

This commit is contained in:
levlam 2021-09-03 18:14:15 +03:00
parent 1d41017d3a
commit 9a6e8cad9e
3 changed files with 41 additions and 68 deletions

View File

@ -1493,19 +1493,14 @@ void ConfigManager::process_app_config(tl_object_ptr<telegram_api::JSONValue> &c
if (value->get_id() == telegram_api::jsonArray::ID) {
auto reasons = std::move(static_cast<telegram_api::jsonArray *>(value)->value_);
for (auto &reason : reasons) {
CHECK(reason != nullptr);
if (reason->get_id() == telegram_api::jsonString::ID) {
Slice reason_name = static_cast<telegram_api::jsonString *>(reason.get())->value_;
if (!reason_name.empty() && reason_name.find(',') == Slice::npos) {
if (!ignored_restriction_reasons.empty()) {
ignored_restriction_reasons += ',';
}
ignored_restriction_reasons.append(reason_name.begin(), reason_name.end());
} else {
LOG(ERROR) << "Receive unexpected restriction reason " << reason_name;
auto reason_name = get_json_value_string(std::move(reason), "ignore_restriction_reasons");
if (!reason_name.empty() && reason_name.find(',') == string::npos) {
if (!ignored_restriction_reasons.empty()) {
ignored_restriction_reasons += ',';
}
ignored_restriction_reasons += reason_name;
} else {
LOG(ERROR) << "Receive unexpected restriction reason " << to_string(reason);
LOG(ERROR) << "Receive unexpected restriction reason " << reason_name;
}
}
} else {
@ -1517,17 +1512,12 @@ void ConfigManager::process_app_config(tl_object_ptr<telegram_api::JSONValue> &c
if (value->get_id() == telegram_api::jsonArray::ID) {
auto emojis = std::move(static_cast<telegram_api::jsonArray *>(value)->value_);
for (auto &emoji : emojis) {
CHECK(emoji != nullptr);
if (emoji->get_id() == telegram_api::jsonString::ID) {
Slice emoji_text = static_cast<telegram_api::jsonString *>(emoji.get())->value_;
if (!emoji_text.empty()) {
dice_emoji_index[emoji_text.str()] = dice_emojis.size();
dice_emojis.push_back(emoji_text.str());
} else {
LOG(ERROR) << "Receive empty dice emoji";
}
auto emoji_text = get_json_value_string(std::move(emoji), "emojies_send_dice");
if (!emoji_text.empty()) {
dice_emoji_index[emoji_text] = dice_emojis.size();
dice_emojis.push_back(emoji_text);
} else {
LOG(ERROR) << "Receive unexpected dice emoji " << to_string(emoji);
LOG(ERROR) << "Receive empty dice emoji";
}
}
} else {
@ -1571,30 +1561,21 @@ void ConfigManager::process_app_config(tl_object_ptr<telegram_api::JSONValue> &c
continue;
}
if (key == "gif_search_branding") {
if (value->get_id() == telegram_api::jsonString::ID) {
animation_search_provider = std::move(static_cast<telegram_api::jsonString *>(value)->value_);
} else {
LOG(ERROR) << "Receive unexpected gif_search_branding " << to_string(*value);
}
animation_search_provider = get_json_value_string(std::move(key_value->value_), "gif_search_branding");
continue;
}
if (key == "gif_search_emojies") {
if (value->get_id() == telegram_api::jsonArray::ID) {
auto emojis = std::move(static_cast<telegram_api::jsonArray *>(value)->value_);
for (auto &emoji : emojis) {
CHECK(emoji != nullptr);
if (emoji->get_id() == telegram_api::jsonString::ID) {
Slice emoji_str = static_cast<telegram_api::jsonString *>(emoji.get())->value_;
if (!emoji_str.empty() && emoji_str.find(',') == Slice::npos) {
if (!animation_search_emojis.empty()) {
animation_search_emojis += ',';
}
animation_search_emojis.append(emoji_str.begin(), emoji_str.end());
} else {
LOG(ERROR) << "Receive unexpected animation search emoji " << emoji_str;
auto emoji_str = get_json_value_string(std::move(emoji), "gif_search_emojies");
if (!emoji_str.empty() && emoji_str.find(',') == string::npos) {
if (!animation_search_emojis.empty()) {
animation_search_emojis += ',';
}
animation_search_emojis += emoji_str;
} else {
LOG(ERROR) << "Receive unexpected animation search emoji " << to_string(emoji);
LOG(ERROR) << "Receive unexpected animation search emoji " << emoji_str;
}
}
} else {
@ -1606,22 +1587,17 @@ void ConfigManager::process_app_config(tl_object_ptr<telegram_api::JSONValue> &c
if (value->get_id() == telegram_api::jsonArray::ID) {
auto actions = std::move(static_cast<telegram_api::jsonArray *>(value)->value_);
for (auto &action : actions) {
CHECK(action != nullptr);
if (action->get_id() == telegram_api::jsonString::ID) {
Slice action_str = static_cast<telegram_api::jsonString *>(action.get())->value_;
SuggestedAction suggested_action(action_str);
if (!suggested_action.is_empty()) {
if (archive_and_mute &&
suggested_action == SuggestedAction{SuggestedAction::Type::EnableArchiveAndMuteNewChats}) {
LOG(INFO) << "Skip EnableArchiveAndMuteNewChats suggested action";
} else {
suggested_actions.push_back(suggested_action);
}
auto action_str = get_json_value_string(std::move(action), "pending_suggestions");
SuggestedAction suggested_action(action_str);
if (!suggested_action.is_empty()) {
if (archive_and_mute &&
suggested_action == SuggestedAction{SuggestedAction::Type::EnableArchiveAndMuteNewChats}) {
LOG(INFO) << "Skip EnableArchiveAndMuteNewChats suggested action";
} else {
LOG(ERROR) << "Receive unsupported suggested action " << action_str;
suggested_actions.push_back(suggested_action);
}
} else {
LOG(ERROR) << "Receive unexpected suggested action " << to_string(action);
LOG(ERROR) << "Receive unsupported suggested action " << action_str;
}
}
} else {
@ -1639,23 +1615,14 @@ void ConfigManager::process_app_config(tl_object_ptr<telegram_api::JSONValue> &c
continue;
}
if (key == "autologin_token") {
if (value->get_id() == telegram_api::jsonString::ID) {
autologin_token = url_encode(static_cast<telegram_api::jsonString *>(value)->value_);
} else {
LOG(ERROR) << "Receive unexpected autologin_token " << to_string(*value);
}
autologin_token = get_json_value_string(std::move(key_value->value_), "autologin_token");
continue;
}
if (key == "autologin_domains") {
if (value->get_id() == telegram_api::jsonArray::ID) {
auto domains = std::move(static_cast<telegram_api::jsonArray *>(value)->value_);
for (auto &domain : domains) {
CHECK(domain != nullptr);
if (domain->get_id() == telegram_api::jsonString::ID) {
autologin_domains.push_back(std::move(static_cast<telegram_api::jsonString *>(domain.get())->value_));
} else {
LOG(ERROR) << "Receive unexpected autologin domain " << to_string(domain);
}
autologin_domains.push_back(get_json_value_string(std::move(domain), "autologin_domains"));
}
} else {
LOG(ERROR) << "Receive unexpected autologin_domains " << to_string(*value);
@ -1666,12 +1633,7 @@ void ConfigManager::process_app_config(tl_object_ptr<telegram_api::JSONValue> &c
if (value->get_id() == telegram_api::jsonArray::ID) {
auto domains = std::move(static_cast<telegram_api::jsonArray *>(value)->value_);
for (auto &domain : domains) {
CHECK(domain != nullptr);
if (domain->get_id() == telegram_api::jsonString::ID) {
url_auth_domains.push_back(std::move(static_cast<telegram_api::jsonString *>(domain.get())->value_));
} else {
LOG(ERROR) << "Receive unexpected url auth domain " << to_string(domain);
}
autologin_domains.push_back(get_json_value_string(std::move(domain), "url_auth_domains"));
}
} else {
LOG(ERROR) << "Receive unexpected url_auth_domains " << to_string(*value);

View File

@ -204,8 +204,17 @@ int32 get_json_value_int(telegram_api::object_ptr<telegram_api::JSONValue> &&jso
if (json_value->get_id() == telegram_api::jsonNumber::ID) {
return static_cast<int32>(static_cast<const telegram_api::jsonNumber *>(json_value.get())->value_);
}
LOG(ERROR) << "Expected integer as " << name << " found " << to_string(*json_value);
LOG(ERROR) << "Expected Integer as " << name << ", but found " << to_string(json_value);
return 0;
}
string get_json_value_string(telegram_api::object_ptr<telegram_api::JSONValue> &&json_value, Slice name) {
CHECK(json_value != nullptr);
if (json_value->get_id() == telegram_api::jsonString::ID) {
return std::move(static_cast<const telegram_api::jsonString *>(json_value.get())->value_);
}
LOG(ERROR) << "Expected String as " << name << ", but found " << to_string(json_value);
return string();
}
} // namespace td

View File

@ -28,4 +28,6 @@ string get_json_string(const td_api::JsonValue *json_value);
int32 get_json_value_int(telegram_api::object_ptr<telegram_api::JSONValue> &&json_value, Slice name);
string get_json_value_string(telegram_api::object_ptr<telegram_api::JSONValue> &&json_value, Slice name);
} // namespace td