|
15 | 15 | #include <vector> |
16 | 16 |
|
17 | 17 | #include "google/protobuf/type.pb.h" |
18 | | -#include "absl/container/flat_hash_map.h" |
19 | 18 | #include "absl/status/status.h" |
20 | 19 | #include "absl/status/statusor.h" |
21 | 20 | #include "absl/strings/escaping.h" |
22 | 21 | #include "absl/strings/numbers.h" |
23 | 22 | #include "absl/strings/str_cat.h" |
24 | | -#include "absl/strings/str_format.h" |
25 | 23 | #include "absl/strings/string_view.h" |
26 | 24 | #include "google/protobuf/dynamic_message.h" |
27 | 25 | #include "google/protobuf/io/coded_stream.h" |
@@ -341,54 +339,182 @@ struct UnparseProto3Type : Proto3Type { |
341 | 339 | return absl::InternalError("message fields cannot have defaults"); |
342 | 340 | } |
343 | 341 |
|
344 | | - static absl::StatusOr<float> GetFloat(Field f, const Msg& msg, |
345 | | - size_t idx = 0) { |
| 342 | + static absl::StatusOr<float> GetFloat(Field f, const Msg& msg) { |
| 343 | + auto span = msg.Get<float>(f->proto().number()); |
| 344 | + if (span.empty()) { |
| 345 | + return GetFloat(f); |
| 346 | + } |
| 347 | + if (span.size() > 1) { |
| 348 | + return absl::InternalError(absl::StrCat( |
| 349 | + "Attempted to read a non-repeated field with multiple values: ", |
| 350 | + f->parent().proto().name())); |
| 351 | + } |
| 352 | + return span[0]; |
| 353 | + } |
| 354 | + |
| 355 | + static absl::StatusOr<float> GetFloat(Field f, const Msg& msg, size_t idx) { |
346 | 356 | return msg.Get<float>(f->proto().number())[idx]; |
347 | 357 | } |
348 | 358 |
|
349 | | - static absl::StatusOr<double> GetDouble(Field f, const Msg& msg, |
350 | | - size_t idx = 0) { |
| 359 | + static absl::StatusOr<double> GetDouble(Field f, const Msg& msg) { |
| 360 | + auto span = msg.Get<double>(f->proto().number()); |
| 361 | + if (span.empty()) { |
| 362 | + return GetDouble(f); |
| 363 | + } |
| 364 | + if (span.size() > 1) { |
| 365 | + return absl::InternalError(absl::StrCat( |
| 366 | + "Attempted to read a non-repeated field with multiple values: ", |
| 367 | + f->parent().proto().name())); |
| 368 | + } |
| 369 | + return span[0]; |
| 370 | + } |
| 371 | + |
| 372 | + static absl::StatusOr<double> GetDouble(Field f, const Msg& msg, size_t idx) { |
351 | 373 | return msg.Get<double>(f->proto().number())[idx]; |
352 | 374 | } |
353 | 375 |
|
354 | | - static absl::StatusOr<int32_t> GetInt32(Field f, const Msg& msg, |
355 | | - size_t idx = 0) { |
| 376 | + static absl::StatusOr<int32_t> GetInt32(Field f, const Msg& msg) { |
| 377 | + auto span = msg.Get<int32_t>(f->proto().number()); |
| 378 | + if (span.empty()) { |
| 379 | + return GetInt32(f); |
| 380 | + } |
| 381 | + if (span.size() > 1) { |
| 382 | + return absl::InternalError(absl::StrCat( |
| 383 | + "Attempted to read a non-repeated field with multiple values: ", |
| 384 | + f->parent().proto().name())); |
| 385 | + } |
| 386 | + return span[0]; |
| 387 | + } |
| 388 | + |
| 389 | + static absl::StatusOr<int32_t> GetInt32(Field f, const Msg& msg, size_t idx) { |
356 | 390 | return msg.Get<int32_t>(f->proto().number())[idx]; |
357 | 391 | } |
358 | 392 |
|
| 393 | + static absl::StatusOr<uint32_t> GetUInt32(Field f, const Msg& msg) { |
| 394 | + auto span = msg.Get<uint32_t>(f->proto().number()); |
| 395 | + if (span.empty()) { |
| 396 | + return GetUInt32(f); |
| 397 | + } |
| 398 | + if (span.size() > 1) { |
| 399 | + return absl::InternalError(absl::StrCat( |
| 400 | + "Attempted to read a non-repeated field with multiple values: ", |
| 401 | + f->parent().proto().name())); |
| 402 | + } |
| 403 | + return span[0]; |
| 404 | + } |
| 405 | + |
359 | 406 | static absl::StatusOr<uint32_t> GetUInt32(Field f, const Msg& msg, |
360 | | - size_t idx = 0) { |
| 407 | + size_t idx) { |
361 | 408 | return msg.Get<uint32_t>(f->proto().number())[idx]; |
362 | 409 | } |
363 | 410 |
|
364 | | - static absl::StatusOr<int64_t> GetInt64(Field f, const Msg& msg, |
365 | | - size_t idx = 0) { |
| 411 | + static absl::StatusOr<int64_t> GetInt64(Field f, const Msg& msg) { |
| 412 | + auto span = msg.Get<int64_t>(f->proto().number()); |
| 413 | + if (span.empty()) { |
| 414 | + return GetInt64(f); |
| 415 | + } |
| 416 | + if (span.size() > 1) { |
| 417 | + return absl::InternalError(absl::StrCat( |
| 418 | + "Attempted to read a non-repeated field with multiple values: ", |
| 419 | + f->parent().proto().name())); |
| 420 | + } |
| 421 | + return span[0]; |
| 422 | + } |
| 423 | + |
| 424 | + static absl::StatusOr<int64_t> GetInt64(Field f, const Msg& msg, size_t idx) { |
366 | 425 | return msg.Get<int64_t>(f->proto().number())[idx]; |
367 | 426 | } |
368 | 427 |
|
| 428 | + static absl::StatusOr<uint64_t> GetUInt64(Field f, const Msg& msg) { |
| 429 | + auto span = msg.Get<uint64_t>(f->proto().number()); |
| 430 | + if (span.empty()) { |
| 431 | + return GetUInt64(f); |
| 432 | + } |
| 433 | + if (span.size() > 1) { |
| 434 | + return absl::InternalError(absl::StrCat( |
| 435 | + "Attempted to read a non-repeated field with multiple values: ", |
| 436 | + f->parent().proto().name())); |
| 437 | + } |
| 438 | + return span[0]; |
| 439 | + } |
| 440 | + |
369 | 441 | static absl::StatusOr<uint64_t> GetUInt64(Field f, const Msg& msg, |
370 | | - size_t idx = 0) { |
| 442 | + size_t idx) { |
371 | 443 | return msg.Get<uint64_t>(f->proto().number())[idx]; |
372 | 444 | } |
373 | 445 |
|
374 | | - static absl::StatusOr<bool> GetBool(Field f, const Msg& msg, size_t idx = 0) { |
| 446 | + static absl::StatusOr<bool> GetBool(Field f, const Msg& msg) { |
| 447 | + auto span = msg.Get<Msg::Bool>(f->proto().number()); |
| 448 | + if (span.empty()) { |
| 449 | + return GetBool(f); |
| 450 | + } |
| 451 | + if (span.size() > 1) { |
| 452 | + return absl::InternalError(absl::StrCat( |
| 453 | + "Attempted to read a non-repeated field with multiple values: ", |
| 454 | + f->parent().proto().name())); |
| 455 | + } |
| 456 | + return span[0] == Msg::kTrue; |
| 457 | + } |
| 458 | + |
| 459 | + static absl::StatusOr<bool> GetBool(Field f, const Msg& msg, size_t idx) { |
375 | 460 | return msg.Get<Msg::Bool>(f->proto().number())[idx] == Msg::kTrue; |
376 | 461 | } |
377 | 462 |
|
| 463 | + static absl::StatusOr<int32_t> GetEnumValue(Field f, const Msg& msg) { |
| 464 | + auto span = msg.Get<int32_t>(f->proto().number()); |
| 465 | + if (span.empty()) { |
| 466 | + return GetEnumValue(f); |
| 467 | + } |
| 468 | + if (span.size() > 1) { |
| 469 | + return absl::InternalError(absl::StrCat( |
| 470 | + "Attempted to read a non-repeated field with multiple values: ", |
| 471 | + f->parent().proto().name())); |
| 472 | + } |
| 473 | + return span[0]; |
| 474 | + } |
| 475 | + |
378 | 476 | static absl::StatusOr<int32_t> GetEnumValue(Field f, const Msg& msg, |
379 | | - size_t idx = 0) { |
| 477 | + size_t idx) { |
380 | 478 | return msg.Get<int32_t>(f->proto().number())[idx]; |
381 | 479 | } |
382 | 480 |
|
| 481 | + static absl::StatusOr<absl::string_view> GetString(Field f, |
| 482 | + std::string& scratch, |
| 483 | + const Msg& msg) { |
| 484 | + auto span = msg.Get<std::string>(f->proto().number()); |
| 485 | + if (span.empty()) { |
| 486 | + return GetString(f, scratch); |
| 487 | + } |
| 488 | + if (span.size() > 1) { |
| 489 | + return absl::InternalError(absl::StrCat( |
| 490 | + "Attempted to read a non-repeated field with multiple values: ", |
| 491 | + f->parent().proto().name())); |
| 492 | + } |
| 493 | + return span[0]; |
| 494 | + } |
| 495 | + |
383 | 496 | static absl::StatusOr<absl::string_view> GetString(Field f, |
384 | 497 | std::string& scratch, |
385 | 498 | const Msg& msg, |
386 | | - size_t idx = 0) { |
| 499 | + size_t idx) { |
387 | 500 | return msg.Get<std::string>(f->proto().number())[idx]; |
388 | 501 | } |
389 | 502 |
|
| 503 | + static absl::StatusOr<const Msg*> GetMessage(Field f, const Msg& msg) { |
| 504 | + auto span = msg.Get<Msg>(f->proto().number()); |
| 505 | + if (span.empty()) { |
| 506 | + return GetMessage(f); |
| 507 | + } |
| 508 | + if (span.size() > 1) { |
| 509 | + return absl::InternalError(absl::StrCat( |
| 510 | + "Attempted to read a non-repeated field with multiple values: ", |
| 511 | + f->parent().proto().name())); |
| 512 | + } |
| 513 | + return &span[0]; |
| 514 | + } |
| 515 | + |
390 | 516 | static absl::StatusOr<const Msg*> GetMessage(Field f, const Msg& msg, |
391 | | - size_t idx = 0) { |
| 517 | + size_t idx) { |
392 | 518 | return &msg.Get<Msg>(f->proto().number())[idx]; |
393 | 519 | } |
394 | 520 |
|
|
0 commit comments