|
| 1 | +#include "config_traverse.h" |
| 2 | + |
| 3 | +#include <util/system/compiler.h> |
| 4 | +#include <util/generic/deque.h> |
| 5 | +#include <util/generic/vector.h> |
| 6 | +#include <util/generic/string.h> |
| 7 | +#include <util/generic/map.h> |
| 8 | +#include <util/generic/set.h> |
| 9 | + |
| 10 | +#include <ydb/core/protos/config.pb.h> |
| 11 | + |
| 12 | +namespace NKikimr::NConfig { |
| 13 | + |
| 14 | +ssize_t FindLoop(TDeque<const Descriptor*>& typePath, const Descriptor* child) { |
| 15 | + for (ssize_t i = 0; i < (ssize_t)typePath.size(); ++i) { |
| 16 | + if (typePath[i] == child) { |
| 17 | + return i; |
| 18 | + } |
| 19 | + } |
| 20 | + return -1; |
| 21 | +} |
| 22 | + |
| 23 | +void Traverse(const Descriptor* d, TDeque<const Descriptor*>& typePath, TDeque<const FieldDescriptor*>& fieldPath, const FieldDescriptor* field, TOnEntryFn onEntry) { |
| 24 | + ssize_t loop = FindLoop(typePath, d); |
| 25 | + |
| 26 | + Y_ABORT_IF(!d && !field, "Either field or descriptor must be defined"); |
| 27 | + |
| 28 | + onEntry(d, typePath, fieldPath, field, loop); |
| 29 | + |
| 30 | + if (!d || loop != -1) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + typePath.push_back(d); |
| 35 | + |
| 36 | + for (int i = 0; i < d->field_count(); ++i) { |
| 37 | + const FieldDescriptor* fieldDescriptor = d->field(i); |
| 38 | + fieldPath.push_back(fieldDescriptor); |
| 39 | + Traverse(fieldDescriptor->message_type(), typePath, fieldPath, fieldDescriptor, onEntry); |
| 40 | + fieldPath.pop_back(); |
| 41 | + } |
| 42 | + |
| 43 | + typePath.pop_back(); |
| 44 | +} |
| 45 | + |
| 46 | +void Traverse(TOnEntryFn onEntry) { |
| 47 | + auto& inst = NKikimrConfig::TAppConfig::default_instance(); |
| 48 | + const Descriptor* descriptor = inst.GetDescriptor(); |
| 49 | + |
| 50 | + TDeque<const Descriptor*> typePath; |
| 51 | + TDeque<const FieldDescriptor*> fieldPath; |
| 52 | + fieldPath.push_back(nullptr); |
| 53 | + Traverse(descriptor, typePath, fieldPath, nullptr, onEntry); |
| 54 | + fieldPath.pop_back(); |
| 55 | +} |
| 56 | + |
| 57 | +} // namespace NKikimr::NConfig |
0 commit comments