From 9ebeafbb5b7d4bdc3e7f61d9250969f03fdabd59 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 3 Sep 2021 18:17:11 +0300 Subject: [PATCH] Add get_json_value_bool helper function. --- td/telegram/ConfigManager.cpp | 8 ++------ td/telegram/JsonValue.cpp | 9 +++++++++ td/telegram/JsonValue.h | 2 ++ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/td/telegram/ConfigManager.cpp b/td/telegram/ConfigManager.cpp index a4bb1748a..ffeadb10a 100644 --- a/td/telegram/ConfigManager.cpp +++ b/td/telegram/ConfigManager.cpp @@ -1606,12 +1606,8 @@ void ConfigManager::process_app_config(tl_object_ptr &c continue; } if (key == "autoarchive_setting_available") { - if (value->get_id() == telegram_api::jsonBool::ID) { - can_archive_and_mute_new_chats_from_unknown_users = - std::move(static_cast(value)->value_); - } else { - LOG(ERROR) << "Receive unexpected autoarchive_setting_available " << to_string(*value); - } + can_archive_and_mute_new_chats_from_unknown_users = + get_json_value_bool(std::move(key_value->value_), "autoarchive_setting_available"); continue; } if (key == "autologin_token") { diff --git a/td/telegram/JsonValue.cpp b/td/telegram/JsonValue.cpp index 197d388a8..254d6990b 100644 --- a/td/telegram/JsonValue.cpp +++ b/td/telegram/JsonValue.cpp @@ -199,6 +199,15 @@ string get_json_string(const td_api::JsonValue *json_value) { return json_encode(JsonableJsonValue(json_value)); } +bool get_json_value_bool(telegram_api::object_ptr &&json_value, Slice name) { + CHECK(json_value != nullptr); + if (json_value->get_id() == telegram_api::jsonBool::ID) { + return static_cast(json_value.get())->value_; + } + LOG(ERROR) << "Expected Boolean as " << name << ", but found " << to_string(json_value); + return false; +} + int32 get_json_value_int(telegram_api::object_ptr &&json_value, Slice name) { CHECK(json_value != nullptr); if (json_value->get_id() == telegram_api::jsonNumber::ID) { diff --git a/td/telegram/JsonValue.h b/td/telegram/JsonValue.h index acb972e96..51b1a748d 100644 --- a/td/telegram/JsonValue.h +++ b/td/telegram/JsonValue.h @@ -26,6 +26,8 @@ tl_object_ptr convert_json_value(td_api::object_ptr &&json_value, Slice name); + int32 get_json_value_int(telegram_api::object_ptr &&json_value, Slice name); string get_json_value_string(telegram_api::object_ptr &&json_value, Slice name);