Use JsonObject::get_.*_field if possible.

This commit is contained in:
levlam 2023-07-31 17:17:52 +03:00
parent a5a408290e
commit 58f15df705
9 changed files with 68 additions and 63 deletions

View File

@ -103,8 +103,8 @@ void gen_from_json_constructor(StringBuilder &sb, const T *constructor, bool is_
sb << " {\n";
for (auto &arg : constructor->args) {
sb << " TRY_STATUS(from_json" << (arg.type->type == tl::simple::Type::Bytes ? "_bytes" : "") << "(to."
<< tl::simple::gen_cpp_field_name(arg.name) << ", get_json_object_field_force(from, \""
<< tl::simple::gen_cpp_name(arg.name) << "\")));\n";
<< tl::simple::gen_cpp_field_name(arg.name) << ", from.extract_field(\"" << tl::simple::gen_cpp_name(arg.name)
<< "\")));\n";
}
sb << " return Status::OK();\n";
sb << "}\n\n";

View File

@ -40,8 +40,8 @@ static std::pair<td_api::object_ptr<td_api::Function>, string> to_request(Slice
}
string extra;
if (has_json_object_field(json_value.get_object(), "@extra")) {
extra = json_encode<string>(get_json_object_field_force(json_value.get_object(), "@extra"));
if (json_value.get_object().has_field("@extra")) {
extra = json_encode<string>(json_value.get_object().extract_field("@extra"));
}
td_api::object_ptr<td_api::Function> func;

View File

@ -270,7 +270,7 @@ static ActorOwn<> get_simple_config_dns(Slice address, Slice host, Promise<Simpl
return Status::Error("Expected JSON object");
}
auto &data_object = answer_part.get_object();
TRY_RESULT(part, get_json_object_string_field(data_object, "data", false));
TRY_RESULT(part, data_object.get_required_string_field("data"));
parts.push_back(std::move(part));
}
if (parts.size() != 2) {
@ -347,7 +347,7 @@ ActorOwn<> get_simple_config_firebase_remote_config(Promise<SimpleConfigResult>
return Status::Error("Expected JSON object");
}
auto &entries_object = json.get_object();
TRY_RESULT(config, get_json_object_string_field(entries_object, "ipconfigv3", false));
TRY_RESULT(config, entries_object.get_required_string_field("ipconfigv3"));
return std::move(config);
};
return get_simple_config_impl(std::move(promise), scheduler_id, std::move(url), "firebaseremoteconfig.googleapis.com",
@ -382,8 +382,10 @@ ActorOwn<> get_simple_config_firebase_firestore(Promise<SimpleConfigResult> prom
if (json.type() != JsonValue::Type::Object) {
return Status::Error("Expected JSON object");
}
TRY_RESULT(data, get_json_object_field(json.get_object(), "data", JsonValue::Type::Object, false));
TRY_RESULT(config, get_json_object_string_field(data.get_object(), "stringValue", false));
auto &json_object = json.get_object();
TRY_RESULT(data, get_json_object_field(json_object, "data", JsonValue::Type::Object, false));
auto &data_object = data.get_object();
TRY_RESULT(config, data_object.get_required_string_field("stringValue"));
return std::move(config);
};
return get_simple_config_impl(std::move(promise), scheduler_id, std::move(url), "firestore.googleapis.com", {},

View File

@ -3015,7 +3015,7 @@ Status NotificationManager::process_push_notification_payload(string payload, bo
auto data = std::move(json_value.get_object());
int32 sent_date = G()->unix_time();
if (has_json_object_field(data, "data")) {
TRY_RESULT(date, get_json_object_int_field(data, "date", true, sent_date));
TRY_RESULT(date, data.get_optional_int_field("date", sent_date));
if (sent_date - 28 * 86400 <= date && date <= sent_date + 5) {
sent_date = date;
}
@ -3056,7 +3056,7 @@ Status NotificationManager::process_push_notification_payload(string payload, bo
}
announcement_message_text = field_value.second.get_string().str();
} else if (field_value.first == "google.sent_time") {
TRY_RESULT(google_sent_time, get_json_object_long_field(data, "google.sent_time"));
TRY_RESULT(google_sent_time, data.get_optional_long_field("google.sent_time"));
google_sent_time /= 1000;
if (sent_date - 28 * 86400 <= google_sent_time && google_sent_time <= sent_date + 5) {
sent_date = narrow_cast<int32>(google_sent_time);
@ -3080,7 +3080,7 @@ Status NotificationManager::process_push_notification_payload(string payload, bo
if (announcement_message_text.empty()) {
return Status::Error("Receive empty announcement message text");
}
TRY_RESULT(announcement_id, get_json_object_int_field(custom, "announcement"));
TRY_RESULT(announcement_id, custom.get_optional_int_field("announcement"));
if (announcement_id == 0) {
return Status::Error(200, "Receive unsupported announcement ID");
}
@ -3105,8 +3105,8 @@ Status NotificationManager::process_push_notification_payload(string payload, bo
}
if (loc_key == "DC_UPDATE") {
TRY_RESULT(dc_id, get_json_object_int_field(custom, "dc", false));
TRY_RESULT(addr, get_json_object_string_field(custom, "addr", false));
TRY_RESULT(dc_id, custom.get_required_int_field("dc"));
TRY_RESULT(addr, custom.get_required_string_field("addr"));
if (!DcId::is_valid(dc_id)) {
return Status::Error("Invalid datacenter identifier");
}
@ -3144,7 +3144,7 @@ Status NotificationManager::process_push_notification_payload(string payload, bo
DialogId dialog_id;
if (has_json_object_field(custom, "from_id")) {
TRY_RESULT(user_id_int, get_json_object_long_field(custom, "from_id"));
TRY_RESULT(user_id_int, custom.get_optional_long_field("from_id"));
UserId user_id(user_id_int);
if (!user_id.is_valid()) {
return Status::Error("Receive invalid user identifier");
@ -3152,7 +3152,7 @@ Status NotificationManager::process_push_notification_payload(string payload, bo
dialog_id = DialogId(user_id);
}
if (has_json_object_field(custom, "chat_id")) {
TRY_RESULT(chat_id_int, get_json_object_long_field(custom, "chat_id"));
TRY_RESULT(chat_id_int, custom.get_optional_long_field("chat_id"));
ChatId chat_id(chat_id_int);
if (!chat_id.is_valid()) {
return Status::Error("Receive invalid basic group identifier");
@ -3160,7 +3160,7 @@ Status NotificationManager::process_push_notification_payload(string payload, bo
dialog_id = DialogId(chat_id);
}
if (has_json_object_field(custom, "channel_id")) {
TRY_RESULT(channel_id_int, get_json_object_long_field(custom, "channel_id"));
TRY_RESULT(channel_id_int, custom.get_optional_long_field("channel_id"));
ChannelId channel_id(channel_id_int);
if (!channel_id.is_valid()) {
return Status::Error("Receive invalid supergroup identifier");
@ -3168,7 +3168,7 @@ Status NotificationManager::process_push_notification_payload(string payload, bo
dialog_id = DialogId(channel_id);
}
if (has_json_object_field(custom, "encryption_id")) {
TRY_RESULT(secret_chat_id_int, get_json_object_int_field(custom, "encryption_id"));
TRY_RESULT(secret_chat_id_int, custom.get_optional_int_field("encryption_id"));
SecretChatId secret_chat_id(secret_chat_id_int);
if (!secret_chat_id.is_valid()) {
return Status::Error("Receive invalid secret chat identifier");
@ -3187,7 +3187,7 @@ Status NotificationManager::process_push_notification_payload(string payload, bo
return Status::Error("Receive read history in a secret chat");
}
TRY_RESULT(max_id, get_json_object_int_field(custom, "max_id"));
TRY_RESULT(max_id, custom.get_optional_int_field("max_id"));
ServerMessageId max_server_message_id(max_id);
if (!max_server_message_id.is_valid()) {
return Status::Error("Receive invalid max_id");
@ -3203,7 +3203,7 @@ Status NotificationManager::process_push_notification_payload(string payload, bo
if (dialog_id.get_type() == DialogType::SecretChat) {
return Status::Error("Receive MESSAGE_DELETED in a secret chat");
}
TRY_RESULT(server_message_ids_str, get_json_object_string_field(custom, "messages", false));
TRY_RESULT(server_message_ids_str, custom.get_required_string_field("messages"));
auto server_message_ids = full_split(server_message_ids_str, ',');
vector<MessageId> message_ids;
for (const auto &server_message_id_str : server_message_ids) {
@ -3228,7 +3228,7 @@ Status NotificationManager::process_push_notification_payload(string payload, bo
return Status::Error("Receive READ_STORIES in a secret chat");
}
TRY_RESULT(max_id, get_json_object_int_field(custom, "max_id"));
TRY_RESULT(max_id, custom.get_optional_int_field("max_id"));
StoryId max_story_id(max_id);
if (!max_story_id.is_server()) {
return Status::Error("Receive invalid max_id");
@ -3243,7 +3243,7 @@ Status NotificationManager::process_push_notification_payload(string payload, bo
if (dialog_id.get_type() != DialogType::SecretChat) {
return Status::Error("Receive STORY_DELETED in a secret chat");
}
TRY_RESULT(server_story_ids_str, get_json_object_string_field(custom, "story_id", false));
TRY_RESULT(server_story_ids_str, custom.get_required_string_field("story_id"));
auto server_story_ids = full_split(server_story_ids_str, ',');
vector<StoryId> story_ids;
for (const auto &server_story_id_str : server_story_ids) {
@ -3259,30 +3259,30 @@ Status NotificationManager::process_push_notification_payload(string payload, bo
return Status::OK();
}
TRY_RESULT(msg_id, get_json_object_int_field(custom, "msg_id"));
TRY_RESULT(msg_id, custom.get_optional_int_field("msg_id"));
ServerMessageId server_message_id(msg_id);
if (server_message_id != ServerMessageId() && !server_message_id.is_valid()) {
return Status::Error("Receive invalid msg_id");
}
TRY_RESULT(random_id, get_json_object_long_field(custom, "random_id"));
TRY_RESULT(random_id, custom.get_optional_long_field("random_id"));
UserId sender_user_id;
DialogId sender_dialog_id;
if (has_json_object_field(custom, "chat_from_broadcast_id")) {
TRY_RESULT(sender_channel_id_int, get_json_object_long_field(custom, "chat_from_broadcast_id"));
TRY_RESULT(sender_channel_id_int, custom.get_optional_long_field("chat_from_broadcast_id"));
sender_dialog_id = DialogId(ChannelId(sender_channel_id_int));
if (!sender_dialog_id.is_valid()) {
return Status::Error("Receive invalid chat_from_broadcast_id");
}
} else if (has_json_object_field(custom, "chat_from_group_id")) {
TRY_RESULT(sender_channel_id_int, get_json_object_long_field(custom, "chat_from_group_id"));
TRY_RESULT(sender_channel_id_int, custom.get_optional_long_field("chat_from_group_id"));
sender_dialog_id = DialogId(ChannelId(sender_channel_id_int));
if (!sender_dialog_id.is_valid()) {
return Status::Error("Receive invalid chat_from_group_id");
}
} else if (has_json_object_field(custom, "chat_from_id")) {
TRY_RESULT(sender_user_id_int, get_json_object_long_field(custom, "chat_from_id"));
TRY_RESULT(sender_user_id_int, custom.get_optional_long_field("chat_from_id"));
sender_user_id = UserId(sender_user_id_int);
if (!sender_user_id.is_valid()) {
return Status::Error("Receive invalid chat_from_id");
@ -3293,7 +3293,7 @@ Status NotificationManager::process_push_notification_payload(string payload, bo
sender_dialog_id = dialog_id;
}
TRY_RESULT(contains_mention_int, get_json_object_int_field(custom, "mention"));
TRY_RESULT(contains_mention_int, custom.get_optional_int_field("mention"));
bool contains_mention = contains_mention_int != 0;
if (begins_with(loc_key, "CHANNEL_MESSAGE") || loc_key == "CHANNEL_ALBUM") {
@ -3390,11 +3390,12 @@ Status NotificationManager::process_push_notification_payload(string payload, bo
telegram_api::object_ptr<telegram_api::UserProfilePhoto> sender_photo;
TRY_RESULT(mtpeer, get_json_object_field(custom, "mtpeer", JsonValue::Type::Object));
if (mtpeer.type() != JsonValue::Type::Null) {
TRY_RESULT(ah, get_json_object_string_field(mtpeer.get_object(), "ah"));
auto &mtpeer_object = mtpeer.get_object();
TRY_RESULT(ah, mtpeer_object.get_optional_string_field("ah"));
if (!ah.empty()) {
TRY_RESULT_ASSIGN(sender_access_hash, to_integer_safe<int64>(ah));
}
TRY_RESULT(ph, get_json_object_field(mtpeer.get_object(), "ph", JsonValue::Type::Object));
TRY_RESULT(ph, get_json_object_field(mtpeer_object, "ph", JsonValue::Type::Object));
if (ph.type() != JsonValue::Type::Null) {
// TODO parse sender photo
}
@ -3420,7 +3421,7 @@ Status NotificationManager::process_push_notification_payload(string payload, bo
Photo attached_photo;
Document attached_document;
if (has_json_object_field(custom, "attachb64")) {
TRY_RESULT(attachb64, get_json_object_string_field(custom, "attachb64", false));
TRY_RESULT(attachb64, custom.get_required_string_field("attachb64"));
TRY_RESULT(attach, base64url_decode(attachb64));
TlParser gzip_parser(attach);
@ -3542,7 +3543,7 @@ Status NotificationManager::process_push_notification_payload(string payload, bo
if (random_id != 0) {
return Status::Error("Receive edit of secret message");
}
TRY_RESULT(edit_date, get_json_object_int_field(custom, "edit_date"));
TRY_RESULT(edit_date, custom.get_optional_int_field("edit_date"));
if (edit_date <= 0) {
return Status::Error("Receive wrong edit date");
}
@ -3557,7 +3558,7 @@ Status NotificationManager::process_push_notification_payload(string payload, bo
disable_notification = true;
ringtone_id = 0;
} else if (has_json_object_field(custom, "ringtone")) {
TRY_RESULT_ASSIGN(ringtone_id, get_json_object_long_field(custom, "ringtone"));
TRY_RESULT_ASSIGN(ringtone_id, custom.get_optional_long_field("ringtone"));
}
add_message_push_notification(dialog_id, MessageId(server_message_id), random_id, sender_user_id, sender_dialog_id,
std::move(sender_name), sent_date, is_from_scheduled, contains_mention,

View File

@ -132,12 +132,12 @@ Result<Address> address_from_json(Slice json) {
}
auto &object = value.get_object();
TRY_RESULT(country_code, get_json_object_string_field(object, "country_code"));
TRY_RESULT(state, get_json_object_string_field(object, "state"));
TRY_RESULT(city, get_json_object_string_field(object, "city"));
TRY_RESULT(street_line1, get_json_object_string_field(object, "street_line1"));
TRY_RESULT(street_line2, get_json_object_string_field(object, "street_line2"));
TRY_RESULT(postal_code, get_json_object_string_field(object, "post_code"));
TRY_RESULT(country_code, object.get_optional_string_field("country_code"));
TRY_RESULT(state, object.get_optional_string_field("state"));
TRY_RESULT(city, object.get_optional_string_field("city"));
TRY_RESULT(street_line1, object.get_optional_string_field("street_line1"));
TRY_RESULT(street_line2, object.get_optional_string_field("street_line2"));
TRY_RESULT(postal_code, object.get_optional_string_field("post_code"));
TRY_STATUS(check_country_code(country_code));
TRY_STATUS(check_state(state));

View File

@ -220,13 +220,13 @@ static tl_object_ptr<td_api::PaymentProvider> convert_payment_provider(
return nullptr;
}
auto r_public_token = get_json_object_string_field(value.get_object(), "public_token", false);
const auto &object = value.get_object();
auto r_public_token = object.get_required_string_field("public_token");
if (r_public_token.is_error()) {
LOG(ERROR) << "Unsupported JSON data \"" << native_parameters->data_ << '"';
return nullptr;
}
if (value.get_object().field_count() != 1) {
if (object.field_count() != 1) {
LOG(ERROR) << "Unsupported JSON data \"" << native_parameters->data_ << '"';
}
@ -246,10 +246,11 @@ static tl_object_ptr<td_api::PaymentProvider> convert_payment_provider(
return nullptr;
}
auto r_need_country = get_json_object_bool_field(value.get_object(), "need_country", false);
auto r_need_postal_code = get_json_object_bool_field(value.get_object(), "need_zip", false);
auto r_need_cardholder_name = get_json_object_bool_field(value.get_object(), "need_cardholder_name", false);
auto r_publishable_key = get_json_object_string_field(value.get_object(), "publishable_key", false);
const auto &object = value.get_object();
auto r_need_country = object.get_required_bool_field("need_country");
auto r_need_postal_code = object.get_required_bool_field("need_zip");
auto r_need_cardholder_name = object.get_required_bool_field("need_cardholder_name");
auto r_publishable_key = object.get_required_string_field("publishable_key");
// TODO support "gpay_parameters":{"gateway":"stripe","stripe:publishableKey":"...","stripe:version":"..."}
if (r_need_country.is_error() || r_need_postal_code.is_error() || r_need_cardholder_name.is_error() ||
@ -257,7 +258,7 @@ static tl_object_ptr<td_api::PaymentProvider> convert_payment_provider(
LOG(ERROR) << "Unsupported JSON data \"" << native_parameters->data_ << '"';
return nullptr;
}
if (value.get_object().field_count() != 5) {
if (object.field_count() != 5) {
LOG(ERROR) << "Unsupported JSON data \"" << native_parameters->data_ << '"';
}

View File

@ -799,19 +799,19 @@ static Result<td_api::object_ptr<td_api::personalDetails>> get_personal_details_
}
auto &object = value.get_object();
TRY_RESULT(first_name, get_json_object_string_field(object, "first_name"));
TRY_RESULT(middle_name, get_json_object_string_field(object, "middle_name"));
TRY_RESULT(last_name, get_json_object_string_field(object, "last_name"));
TRY_RESULT(native_first_name, get_json_object_string_field(object, "first_name_native"));
TRY_RESULT(native_middle_name, get_json_object_string_field(object, "middle_name_native"));
TRY_RESULT(native_last_name, get_json_object_string_field(object, "last_name_native"));
TRY_RESULT(birthdate, get_json_object_string_field(object, "birth_date"));
TRY_RESULT(first_name, object.get_optional_string_field("first_name"));
TRY_RESULT(middle_name, object.get_optional_string_field("middle_name"));
TRY_RESULT(last_name, object.get_optional_string_field("last_name"));
TRY_RESULT(native_first_name, object.get_optional_string_field("first_name_native"));
TRY_RESULT(native_middle_name, object.get_optional_string_field("middle_name_native"));
TRY_RESULT(native_last_name, object.get_optional_string_field("last_name_native"));
TRY_RESULT(birthdate, object.get_optional_string_field("birth_date"));
if (birthdate.empty()) {
return Status::Error(400, "Birthdate must be non-empty");
}
TRY_RESULT(gender, get_json_object_string_field(object, "gender"));
TRY_RESULT(country_code, get_json_object_string_field(object, "country_code"));
TRY_RESULT(residence_country_code, get_json_object_string_field(object, "residence_country_code"));
TRY_RESULT(gender, object.get_optional_string_field("gender"));
TRY_RESULT(country_code, object.get_optional_string_field("country_code"));
TRY_RESULT(residence_country_code, object.get_optional_string_field("residence_country_code"));
TRY_STATUS(check_name(first_name));
TRY_STATUS(check_name(middle_name));
@ -933,8 +933,8 @@ static Result<td_api::object_ptr<td_api::identityDocument>> get_identity_documen
}
auto &object = json_value.get_object();
TRY_RESULT(number, get_json_object_string_field(object, "document_no"));
TRY_RESULT(expiry_date, get_json_object_string_field(object, "expiry_date"));
TRY_RESULT(number, object.get_optional_string_field("document_no"));
TRY_RESULT(expiry_date, object.get_optional_string_field("expiry_date"));
TRY_STATUS(check_document_number(number));
TRY_RESULT(date, get_date_object(expiry_date));

View File

@ -7099,7 +7099,7 @@ Status StickersManager::on_animated_emoji_message_clicked(string &&emoji, FullMe
return Status::Error("Expected an object");
}
auto &object = value.get_object();
TRY_RESULT(version, get_json_object_int_field(object, "v", false));
TRY_RESULT(version, object.get_required_int_field("v"));
if (version != 1) {
return Status::OK();
}
@ -7116,11 +7116,11 @@ Status StickersManager::on_animated_emoji_message_clicked(string &&emoji, FullMe
return Status::Error("Expected clicks as JSON objects");
}
auto &click_object = click.get_object();
TRY_RESULT(index, get_json_object_int_field(click_object, "i", false));
TRY_RESULT(index, click_object.get_required_int_field("i"));
if (index <= 0 || index > 9) {
return Status::Error("Wrong index");
}
TRY_RESULT(start_time, get_json_object_double_field(click_object, "t", false));
TRY_RESULT(start_time, click_object.get_required_double_field("t"));
if (!std::isfinite(start_time)) {
return Status::Error("Receive invalid start time");
}

View File

@ -66,7 +66,7 @@ class GoogleDnsResolver final : public Actor {
return Status::Error("Failed to parse DNS result: Answer[0] is not an object");
}
auto &answer_0 = array[0].get_object();
TRY_RESULT(ip_str, get_json_object_string_field(answer_0, "data", false));
TRY_RESULT(ip_str, answer_0.get_required_string_field("data"));
IPAddress ip;
TRY_STATUS(ip.init_host_port(ip_str, 0));
return ip;
@ -82,7 +82,8 @@ class GoogleDnsResolver final : public Actor {
if (json_value.type() != JsonValue::Type::Object) {
return Status::Error("Failed to parse DNS result: not an object");
}
TRY_RESULT(answer, get_json_object_field(json_value.get_object(), "Answer", JsonValue::Type::Array, false));
auto &object = json_value.get_object();
TRY_RESULT(answer, get_json_object_field(object, "Answer", JsonValue::Type::Array, false));
return get_ip_address(answer);
}
}