Make object const in get_json_object_*_field.

This commit is contained in:
levlam 2023-07-31 15:37:54 +03:00
parent 88f169b714
commit 6d05e8af6e
2 changed files with 38 additions and 20 deletions

View File

@ -615,20 +615,28 @@ Result<JsonValue> get_json_object_field(JsonObject &object, Slice name, JsonValu
}
}
if (!is_optional) {
return Status::Error(400, PSLICE() << "Can't find field \"" << name << "\"");
return Status::Error(400, PSLICE() << "Can't find field \"" << name << '"');
}
return JsonValue();
}
Result<bool> get_json_object_bool_field(JsonObject &object, Slice name, bool is_optional, bool default_value) {
TRY_RESULT(value, get_json_object_field(object, name, JsonValue::Type::Boolean, is_optional));
if (value.type() == JsonValue::Type::Null) {
return default_value;
}
return value.get_boolean();
Result<bool> get_json_object_bool_field(const JsonObject &object, Slice name, bool is_optional, bool default_value) {
for (auto &field_value : object.field_values_) {
if (field_value.first == name) {
if (field_value.second.type() != JsonValue::Type::Boolean) {
return Status::Error(400, PSLICE() << "Field \"" << name << "\" must be of type Boolean");
}
Result<int32> get_json_object_int_field(JsonObject &object, Slice name, bool is_optional, int32 default_value) {
return field_value.second.get_boolean();
}
}
if (is_optional) {
return default_value;
}
return Status::Error(400, PSLICE() << "Can't find field \"" << name << '"');
}
Result<int32> get_json_object_int_field(const JsonObject &object, Slice name, bool is_optional, int32 default_value) {
for (auto &field_value : object.field_values_) {
if (field_value.first == name) {
if (field_value.second.type() == JsonValue::Type::String) {
@ -644,10 +652,10 @@ Result<int32> get_json_object_int_field(JsonObject &object, Slice name, bool is_
if (is_optional) {
return default_value;
}
return Status::Error(400, PSLICE() << "Can't find field \"" << name << "\"");
return Status::Error(400, PSLICE() << "Can't find field \"" << name << '"');
}
Result<int64> get_json_object_long_field(JsonObject &object, Slice name, bool is_optional, int64 default_value) {
Result<int64> get_json_object_long_field(const JsonObject &object, Slice name, bool is_optional, int64 default_value) {
for (auto &field_value : object.field_values_) {
if (field_value.first == name) {
if (field_value.second.type() == JsonValue::Type::String) {
@ -663,18 +671,28 @@ Result<int64> get_json_object_long_field(JsonObject &object, Slice name, bool is
if (is_optional) {
return default_value;
}
return Status::Error(400, PSLICE() << "Can't find field \"" << name << "\"");
return Status::Error(400, PSLICE() << "Can't find field \"" << name << '"');
}
Result<double> get_json_object_double_field(JsonObject &object, Slice name, bool is_optional, double default_value) {
TRY_RESULT(value, get_json_object_field(object, name, JsonValue::Type::Number, is_optional));
if (value.type() == JsonValue::Type::Null) {
Result<double> get_json_object_double_field(const JsonObject &object, Slice name, bool is_optional,
double default_value) {
for (auto &field_value : object.field_values_) {
if (field_value.first == name) {
if (field_value.second.type() != JsonValue::Type::Number) {
return Status::Error(400, PSLICE() << "Field \"" << name << "\" must be of type Number");
}
return to_double(field_value.second.get_number());
}
}
if (is_optional) {
return default_value;
}
return to_double(value.get_number());
return Status::Error(400, PSLICE() << "Can't find field \"" << name << '"');
}
Result<string> get_json_object_string_field(JsonObject &object, Slice name, bool is_optional, string default_value) {
Result<string> get_json_object_string_field(const JsonObject &object, Slice name, bool is_optional,
string default_value) {
for (auto &field_value : object.field_values_) {
if (field_value.first == name) {
if (field_value.second.type() == JsonValue::Type::String) {
@ -690,7 +708,7 @@ Result<string> get_json_object_string_field(JsonObject &object, Slice name, bool
if (is_optional) {
return default_value;
}
return Status::Error(400, PSLICE() << "Can't find field \"" << name << "\"");
return Status::Error(400, PSLICE() << "Can't find field \"" << name << '"');
}
} // namespace td

View File

@ -856,19 +856,19 @@ JsonValue get_json_object_field_force(JsonObject &object, Slice name) TD_WARN_UN
Result<JsonValue> get_json_object_field(JsonObject &object, Slice name, JsonValue::Type type,
bool is_optional = true) TD_WARN_UNUSED_RESULT;
Result<bool> get_json_object_bool_field(JsonObject &object, Slice name, bool is_optional = true,
Result<bool> get_json_object_bool_field(const JsonObject &object, Slice name, bool is_optional = true,
bool default_value = false) TD_WARN_UNUSED_RESULT;
Result<int32> get_json_object_int_field(JsonObject &object, Slice name, bool is_optional = true,
Result<int32> get_json_object_int_field(const JsonObject &object, Slice name, bool is_optional = true,
int32 default_value = 0) TD_WARN_UNUSED_RESULT;
Result<int64> get_json_object_long_field(JsonObject &object, Slice name, bool is_optional = true,
Result<int64> get_json_object_long_field(const JsonObject &object, Slice name, bool is_optional = true,
int64 default_value = 0) TD_WARN_UNUSED_RESULT;
Result<double> get_json_object_double_field(JsonObject &object, Slice name, bool is_optional = true,
Result<double> get_json_object_double_field(const JsonObject &object, Slice name, bool is_optional = true,
double default_value = 0.0) TD_WARN_UNUSED_RESULT;
Result<string> get_json_object_string_field(JsonObject &object, Slice name, bool is_optional = true,
Result<string> get_json_object_string_field(const JsonObject &object, Slice name, bool is_optional = true,
string default_value = "") TD_WARN_UNUSED_RESULT;
} // namespace td