field_option.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #pragma once
  2. #include <google/protobuf/descriptor.h>
  3. #include <google/protobuf/descriptor.pb.h>
  4. #include <google/protobuf/message.h>
  5. namespace NProtobufJson {
  6. // Functor that defines whether given field has some option set to true
  7. //
  8. // Example:
  9. // message T {
  10. // optional stroka some_field = 1 [(some_option) = true];
  11. // }
  12. //
  13. template <typename TFieldOptionExtensionId>
  14. class TFieldOptionFunctor {
  15. public:
  16. TFieldOptionFunctor(const TFieldOptionExtensionId& option, bool positive = true)
  17. : Option(option)
  18. , Positive(positive)
  19. {
  20. }
  21. bool operator()(const NProtoBuf::Message&, const NProtoBuf::FieldDescriptor* field) const {
  22. const NProtoBuf::FieldOptions& opt = field->options();
  23. const bool val = opt.GetExtension(Option);
  24. return Positive ? val : !val;
  25. }
  26. private:
  27. const TFieldOptionExtensionId& Option;
  28. bool Positive;
  29. };
  30. template <typename TFieldOptionExtensionId>
  31. TFieldOptionFunctor<TFieldOptionExtensionId> MakeFieldOptionFunctor(const TFieldOptionExtensionId& option, bool positive = true) {
  32. return TFieldOptionFunctor<TFieldOptionExtensionId>(option, positive);
  33. }
  34. }