walk.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "walk.h"
  2. #include <util/generic/hash_set.h>
  3. namespace {
  4. using namespace NProtoBuf;
  5. template <typename TMessage, typename TOnField>
  6. void DoWalkReflection(TMessage& msg, TOnField& onField) {
  7. const Descriptor* descr = msg.GetDescriptor();
  8. for (int i1 = 0; i1 < descr->field_count(); ++i1) {
  9. const FieldDescriptor* fd = descr->field(i1);
  10. if (!onField(msg, fd)) {
  11. continue;
  12. }
  13. std::conditional_t<std::is_const_v<TMessage>, TConstField, TMutableField> ff(msg, fd);
  14. if (ff.IsMessage()) {
  15. for (size_t i2 = 0; i2 < ff.Size(); ++i2) {
  16. if constexpr (std::is_const_v<TMessage>) {
  17. WalkReflection(*ff.template Get<Message>(i2), onField);
  18. } else {
  19. WalkReflection(*ff.MutableMessage(i2), onField);
  20. }
  21. }
  22. }
  23. }
  24. }
  25. void DoWalkSchema(const Descriptor* descriptor,
  26. std::function<bool(const FieldDescriptor*)>& onField,
  27. THashSet<const Descriptor*>& visited)
  28. {
  29. if (!visited.emplace(descriptor).second) {
  30. return;
  31. }
  32. for (int i1 = 0; i1 < descriptor->field_count(); ++i1) {
  33. const FieldDescriptor* fd = descriptor->field(i1);
  34. if (!onField(fd)) {
  35. continue;
  36. }
  37. if (fd->type() == FieldDescriptor::Type::TYPE_MESSAGE) {
  38. DoWalkSchema(fd->message_type(), onField, visited);
  39. }
  40. }
  41. visited.erase(descriptor);
  42. }
  43. }
  44. namespace NProtoBuf {
  45. void WalkReflection(Message& msg,
  46. std::function<bool(Message&, const FieldDescriptor*)> onField)
  47. {
  48. DoWalkReflection(msg, onField);
  49. }
  50. void WalkReflection(const Message& msg,
  51. std::function<bool(const Message&, const FieldDescriptor*)> onField)
  52. {
  53. DoWalkReflection(msg, onField);
  54. }
  55. void WalkSchema(const Descriptor* descriptor,
  56. std::function<bool(const FieldDescriptor*)> onField)
  57. {
  58. THashSet<const Descriptor*> visited;
  59. DoWalkSchema(descriptor, onField, visited);
  60. }
  61. } // namespace NProtoBuf