File indexing completed on 2025-02-28 10:10:20
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef GOOGLE_PROTOBUF_JSON_INTERNAL_UNPARSER_TRAITS_H__
0009 #define GOOGLE_PROTOBUF_JSON_INTERNAL_UNPARSER_TRAITS_H__
0010
0011 #include <algorithm>
0012 #include <cfloat>
0013 #include <cstdint>
0014 #include <memory>
0015 #include <sstream>
0016 #include <string>
0017 #include <type_traits>
0018 #include <utility>
0019 #include <vector>
0020
0021 #include "google/protobuf/type.pb.h"
0022 #include "absl/container/flat_hash_map.h"
0023 #include "absl/status/status.h"
0024 #include "absl/strings/escaping.h"
0025 #include "absl/strings/numbers.h"
0026 #include "absl/strings/str_format.h"
0027 #include "absl/strings/string_view.h"
0028 #include "absl/types/optional.h"
0029 #include "absl/types/variant.h"
0030 #include "google/protobuf/io/coded_stream.h"
0031 #include "google/protobuf/json/internal/descriptor_traits.h"
0032 #include "google/protobuf/stubs/status_macros.h"
0033
0034
0035 #include "google/protobuf/port_def.inc"
0036
0037 namespace google {
0038 namespace protobuf {
0039 namespace json_internal {
0040
0041
0042
0043
0044
0045
0046 template <typename Traits>
0047 using Msg = typename Traits::Msg;
0048
0049
0050 struct UnparseProto2Descriptor : Proto2Descriptor {
0051
0052 using Msg = Message;
0053
0054 static const Desc& GetDesc(const Msg& msg) { return *msg.GetDescriptor(); }
0055
0056
0057 static void FindAndAppendExtensions(const Msg& msg,
0058 std::vector<Field>& fields) {
0059
0060
0061
0062
0063 std::vector<Field> all_fields;
0064 msg.GetReflection()->ListFields(msg, &all_fields);
0065
0066 for (Field field : all_fields) {
0067 if (field->is_extension()) {
0068 fields.push_back(field);
0069 }
0070 }
0071 }
0072
0073 static size_t GetSize(Field f, const Msg& msg) {
0074 if (f->is_repeated()) {
0075 return msg.GetReflection()->FieldSize(msg, f);
0076 } else {
0077 return msg.GetReflection()->HasField(msg, f) ? 1 : 0;
0078 }
0079 }
0080
0081 static absl::StatusOr<float> GetFloat(Field f) {
0082 return f->default_value_float();
0083 }
0084
0085 static absl::StatusOr<double> GetDouble(Field f) {
0086 return f->default_value_double();
0087 }
0088
0089 static absl::StatusOr<int32_t> GetInt32(Field f) {
0090 return f->default_value_int32();
0091 }
0092
0093 static absl::StatusOr<uint32_t> GetUInt32(Field f) {
0094 return f->default_value_uint32();
0095 }
0096
0097 static absl::StatusOr<int64_t> GetInt64(Field f) {
0098 return f->default_value_int64();
0099 }
0100
0101 static absl::StatusOr<uint64_t> GetUInt64(Field f) {
0102 return f->default_value_uint64();
0103 }
0104
0105 static absl::StatusOr<bool> GetBool(Field f) {
0106 return f->default_value_bool();
0107 }
0108
0109 static absl::StatusOr<int32_t> GetEnumValue(Field f) {
0110 return f->default_value_enum()->number();
0111 }
0112
0113 static absl::StatusOr<absl::string_view> GetString(Field f,
0114 std::string& scratch) {
0115 return f->default_value_string();
0116 }
0117
0118 static absl::StatusOr<const Msg*> GetMessage(Field f) {
0119 return absl::InternalError("message fields cannot have defaults");
0120 }
0121
0122 static absl::StatusOr<float> GetFloat(Field f, const Msg& msg) {
0123 return msg.GetReflection()->GetFloat(msg, f);
0124 }
0125
0126 static absl::StatusOr<double> GetDouble(Field f, const Msg& msg) {
0127 return msg.GetReflection()->GetDouble(msg, f);
0128 }
0129
0130 static absl::StatusOr<int32_t> GetInt32(Field f, const Msg& msg) {
0131 return msg.GetReflection()->GetInt32(msg, f);
0132 }
0133
0134 static absl::StatusOr<uint32_t> GetUInt32(Field f, const Msg& msg) {
0135 return msg.GetReflection()->GetUInt32(msg, f);
0136 }
0137
0138 static absl::StatusOr<int64_t> GetInt64(Field f, const Msg& msg) {
0139 return msg.GetReflection()->GetInt64(msg, f);
0140 }
0141
0142 static absl::StatusOr<uint64_t> GetUInt64(Field f, const Msg& msg) {
0143 return msg.GetReflection()->GetUInt64(msg, f);
0144 }
0145
0146 static absl::StatusOr<bool> GetBool(Field f, const Msg& msg) {
0147 return msg.GetReflection()->GetBool(msg, f);
0148 }
0149
0150 static absl::StatusOr<int32_t> GetEnumValue(Field f, const Msg& msg) {
0151 return msg.GetReflection()->GetEnumValue(msg, f);
0152 }
0153
0154 static absl::StatusOr<absl::string_view> GetString(Field f,
0155 std::string& scratch,
0156 const Msg& msg) {
0157 return msg.GetReflection()->GetStringReference(msg, f, &scratch);
0158 }
0159
0160 static absl::StatusOr<const Msg*> GetMessage(Field f, const Msg& msg) {
0161 return &msg.GetReflection()->GetMessage(msg, f);
0162 }
0163
0164 static absl::StatusOr<float> GetFloat(Field f, const Msg& msg, size_t idx) {
0165 return msg.GetReflection()->GetRepeatedFloat(msg, f, idx);
0166 }
0167
0168 static absl::StatusOr<double> GetDouble(Field f, const Msg& msg, size_t idx) {
0169 return msg.GetReflection()->GetRepeatedDouble(msg, f, idx);
0170 }
0171
0172 static absl::StatusOr<int32_t> GetInt32(Field f, const Msg& msg, size_t idx) {
0173 return msg.GetReflection()->GetRepeatedInt32(msg, f, idx);
0174 }
0175
0176 static absl::StatusOr<uint32_t> GetUInt32(Field f, const Msg& msg,
0177 size_t idx) {
0178 return msg.GetReflection()->GetRepeatedUInt32(msg, f, idx);
0179 }
0180
0181 static absl::StatusOr<int64_t> GetInt64(Field f, const Msg& msg, size_t idx) {
0182 return msg.GetReflection()->GetRepeatedInt64(msg, f, idx);
0183 }
0184
0185 static absl::StatusOr<uint64_t> GetUInt64(Field f, const Msg& msg,
0186 size_t idx) {
0187 return msg.GetReflection()->GetRepeatedUInt64(msg, f, idx);
0188 }
0189
0190 static absl::StatusOr<bool> GetBool(Field f, const Msg& msg, size_t idx) {
0191 return msg.GetReflection()->GetRepeatedBool(msg, f, idx);
0192 }
0193
0194 static absl::StatusOr<int32_t> GetEnumValue(Field f, const Msg& msg,
0195 size_t idx) {
0196 return msg.GetReflection()->GetRepeatedEnumValue(msg, f, idx);
0197 }
0198
0199 static absl::StatusOr<absl::string_view> GetString(Field f,
0200 std::string& scratch,
0201 const Msg& msg,
0202 size_t idx) {
0203 return msg.GetReflection()->GetRepeatedStringReference(msg, f, idx,
0204 &scratch);
0205 }
0206
0207 static absl::StatusOr<const Msg*> GetMessage(Field f, const Msg& msg,
0208 size_t idx) {
0209 return &msg.GetReflection()->GetRepeatedMessage(msg, f, idx);
0210 }
0211
0212 template <typename F>
0213 static absl::Status WithDecodedMessage(const Desc& desc,
0214 absl::string_view data, F body) {
0215 DynamicMessageFactory factory;
0216 std::unique_ptr<Message> unerased(factory.GetPrototype(&desc)->New());
0217 unerased->ParsePartialFromString(data);
0218
0219
0220
0221 const Msg& ref = *unerased;
0222 return body(ref);
0223 }
0224 };
0225
0226 struct UnparseProto3Type : Proto3Type {
0227 using Msg = UntypedMessage;
0228
0229 static const Desc& GetDesc(const Msg& msg) { return msg.desc(); }
0230
0231 static void FindAndAppendExtensions(const Msg&, std::vector<Field>&) {
0232
0233 }
0234
0235 static size_t GetSize(Field f, const Msg& msg) {
0236 return msg.Count(f->proto().number());
0237 }
0238
0239 static absl::StatusOr<float> GetFloat(Field f) {
0240 if (f->proto().default_value().empty()) {
0241 return 0.0;
0242 }
0243 float x;
0244 if (!absl::SimpleAtof(f->proto().default_value(), &x)) {
0245 return absl::InternalError(absl::StrCat(
0246 "bad default value in type.proto: ", f->parent().proto().name()));
0247 }
0248 return x;
0249 }
0250
0251 static absl::StatusOr<double> GetDouble(Field f) {
0252 if (f->proto().default_value().empty()) {
0253 return 0.0;
0254 }
0255 double x;
0256 if (!absl::SimpleAtod(f->proto().default_value(), &x)) {
0257 return absl::InternalError(absl::StrCat(
0258 "bad default value in type.proto: ", f->parent().proto().name()));
0259 }
0260 return x;
0261 }
0262
0263 static absl::StatusOr<int32_t> GetInt32(Field f) {
0264 if (f->proto().default_value().empty()) {
0265 return 0;
0266 }
0267 int32_t x;
0268 if (!absl::SimpleAtoi(f->proto().default_value(), &x)) {
0269 return absl::InternalError(absl::StrCat(
0270 "bad default value in type.proto: ", f->parent().proto().name()));
0271 }
0272 return x;
0273 }
0274
0275 static absl::StatusOr<uint32_t> GetUInt32(Field f) {
0276 if (f->proto().default_value().empty()) {
0277 return 0;
0278 }
0279 uint32_t x;
0280 if (!absl::SimpleAtoi(f->proto().default_value(), &x)) {
0281 return absl::InternalError(absl::StrCat(
0282 "bad default value in type.proto: ", f->parent().proto().name()));
0283 }
0284 return x;
0285 }
0286
0287 static absl::StatusOr<int64_t> GetInt64(Field f) {
0288 if (f->proto().default_value().empty()) {
0289 return 0;
0290 }
0291 int64_t x;
0292 if (!absl::SimpleAtoi(f->proto().default_value(), &x)) {
0293 return absl::InternalError(absl::StrCat(
0294 "bad default value in type.proto: ", f->parent().proto().name()));
0295 }
0296 return x;
0297 }
0298
0299 static absl::StatusOr<uint64_t> GetUInt64(Field f) {
0300 if (f->proto().default_value().empty()) {
0301 return 0;
0302 }
0303 uint64_t x;
0304 if (!absl::SimpleAtoi(f->proto().default_value(), &x)) {
0305 return absl::InternalError(absl::StrCat(
0306 "bad default value in type.proto: ", f->parent().proto().name()));
0307 }
0308 return x;
0309 }
0310
0311 static absl::StatusOr<bool> GetBool(Field f) {
0312 if (f->proto().default_value().empty()) {
0313 return false;
0314 } else if (f->proto().default_value() == "false") {
0315 return false;
0316 } else if (f->proto().default_value() == "true") {
0317 return true;
0318 } else {
0319 return absl::InternalError(absl::StrCat(
0320 "bad default value in type.proto: ", f->parent().proto().name()));
0321 }
0322 }
0323
0324 static absl::StatusOr<int32_t> GetEnumValue(Field f) {
0325 if (f->proto().default_value().empty()) {
0326 auto e = f->EnumType();
0327 RETURN_IF_ERROR(e.status());
0328
0329 return (**e).proto().enumvalue(0).number();
0330 }
0331 return EnumNumberByName(f, f->proto().default_value(),
0332 false);
0333 }
0334
0335 static absl::StatusOr<absl::string_view> GetString(Field f,
0336 std::string& scratch) {
0337 absl::CUnescape(f->proto().default_value(), &scratch);
0338 return scratch;
0339 }
0340
0341 static absl::StatusOr<const Msg*> GetMessage(Field f) {
0342 return absl::InternalError("message fields cannot have defaults");
0343 }
0344
0345 static absl::StatusOr<float> GetFloat(Field f, const Msg& msg,
0346 size_t idx = 0) {
0347 return msg.Get<float>(f->proto().number())[idx];
0348 }
0349
0350 static absl::StatusOr<double> GetDouble(Field f, const Msg& msg,
0351 size_t idx = 0) {
0352 return msg.Get<double>(f->proto().number())[idx];
0353 }
0354
0355 static absl::StatusOr<int32_t> GetInt32(Field f, const Msg& msg,
0356 size_t idx = 0) {
0357 return msg.Get<int32_t>(f->proto().number())[idx];
0358 }
0359
0360 static absl::StatusOr<uint32_t> GetUInt32(Field f, const Msg& msg,
0361 size_t idx = 0) {
0362 return msg.Get<uint32_t>(f->proto().number())[idx];
0363 }
0364
0365 static absl::StatusOr<int64_t> GetInt64(Field f, const Msg& msg,
0366 size_t idx = 0) {
0367 return msg.Get<int64_t>(f->proto().number())[idx];
0368 }
0369
0370 static absl::StatusOr<uint64_t> GetUInt64(Field f, const Msg& msg,
0371 size_t idx = 0) {
0372 return msg.Get<uint64_t>(f->proto().number())[idx];
0373 }
0374
0375 static absl::StatusOr<bool> GetBool(Field f, const Msg& msg, size_t idx = 0) {
0376 return msg.Get<Msg::Bool>(f->proto().number())[idx] == Msg::kTrue;
0377 }
0378
0379 static absl::StatusOr<int32_t> GetEnumValue(Field f, const Msg& msg,
0380 size_t idx = 0) {
0381 return msg.Get<int32_t>(f->proto().number())[idx];
0382 }
0383
0384 static absl::StatusOr<absl::string_view> GetString(Field f,
0385 std::string& scratch,
0386 const Msg& msg,
0387 size_t idx = 0) {
0388 return msg.Get<std::string>(f->proto().number())[idx];
0389 }
0390
0391 static absl::StatusOr<const Msg*> GetMessage(Field f, const Msg& msg,
0392 size_t idx = 0) {
0393 return &msg.Get<Msg>(f->proto().number())[idx];
0394 }
0395
0396 template <typename F>
0397 static absl::Status WithDecodedMessage(const Desc& desc,
0398 absl::string_view data, F body) {
0399 io::CodedInputStream stream(reinterpret_cast<const uint8_t*>(data.data()),
0400 data.size());
0401 auto unerased = Msg::ParseFromStream(&desc, stream);
0402 RETURN_IF_ERROR(unerased.status());
0403
0404
0405
0406 const Msg& ref = *unerased;
0407 return body(ref);
0408 }
0409 };
0410 }
0411 }
0412 }
0413
0414 #include "google/protobuf/port_undef.inc"
0415 #endif