Add get_json_value_int helper method.

This commit is contained in:
levlam 2021-09-03 17:54:04 +03:00
parent ab4736df28
commit 1d41017d3a
3 changed files with 13 additions and 4 deletions

View File

@ -1548,8 +1548,7 @@ void ConfigManager::process_app_config(tl_object_ptr<telegram_api::JSONValue> &c
if (dice_key_value->value_->get_id() != telegram_api::jsonNumber::ID) {
continue;
}
auto current_value =
static_cast<int32>(static_cast<telegram_api::jsonNumber *>(dice_key_value->value_.get())->value_);
auto current_value = get_json_value_int(std::move(dice_key_value->value_), Slice());
if (dice_key_value->key_ == "value") {
dice_value = current_value;
}
@ -1689,8 +1688,7 @@ void ConfigManager::process_app_config(tl_object_ptr<telegram_api::JSONValue> &c
continue;
}
if (video_note_setting->value_->get_id() == telegram_api::jsonNumber::ID) {
auto setting_value = static_cast<int32>(
static_cast<const telegram_api::jsonNumber *>(video_note_setting->value_.get())->value_);
auto setting_value = get_json_value_int(std::move(video_note_setting->value_), Slice());
if (setting_value > 0) {
if (video_note_setting->key_ == "diameter") {
G()->shared_config().set_option_integer("suggested_video_note_length", setting_value);

View File

@ -199,4 +199,13 @@ string get_json_string(const td_api::JsonValue *json_value) {
return json_encode<string>(JsonableJsonValue(json_value));
}
int32 get_json_value_int(telegram_api::object_ptr<telegram_api::JSONValue> &&json_value, Slice name) {
CHECK(json_value != nullptr);
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);
return 0;
}
} // namespace td

View File

@ -26,4 +26,6 @@ tl_object_ptr<telegram_api::JSONValue> convert_json_value(td_api::object_ptr<td_
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);
} // namespace td