asekretenko commented on a change in pull request #326: MESOS-6874: Validate 
the match between Type and *Infos in the ContainerInfo.
URL: https://github.com/apache/mesos/pull/326#discussion_r266910355
 
 

 ##########
 File path: src/common/protobuf_utils.hpp
 ##########
 @@ -64,6 +64,53 @@ struct Slave;
 
 namespace protobuf {
 
+//
+// A "protobuf union" is a protobuf message
+// which has an enum named "Type" defined in its scope,
+// a required field named "type" of a "Type" type
+// and a field corresponding to each member of the "Type" enum.
+//
+// A valid "protobuf union" should satisfy the following requirements.
+// 1. A member of the Type enum with a number (not index!) of 0,
+//    if there is any, should be named "UNKNOWN".
+// 2. For each other member of the Type enum there should be an optional field
+//    in the message with an exactly matching name in lowercase.
+// 3. The fields that correspond to members of the Type enum other than
+//    message.type(), should not be set.
+//
+// NOTE: If possible, oneof should be used in the new messages instead
+// of the "protobuf union".
+//
+class UnionValidator
+{
+public:
+  UnionValidator(
+      const google::protobuf::Descriptor*,
+      const google::protobuf::EnumDescriptor*);
+
+  Option<Error> validate(
+      const int messageTypeNumber, const google::protobuf::Message&) const;
+
+private:
+  std::vector<std::pair<int, const google::protobuf::FieldDescriptor*>>
+    unionFieldDescriptors_;
+  const google::protobuf::EnumDescriptor* typeDescriptor_;
+};
+
+
+// This function returns Error if the protobuf union is not valid
+// and Nothing otherwise.
+// In case MessageWithUnion is not a protobuf union,
+// this function will abort the process on the first use.
+template <class MessageWithUnion>
+Option<Error> validateProtobufUnion(const MessageWithUnion& message)
+{
+  static const UnionValidator validator(
+      MessageWithUnion::descriptor(), MessageWithUnion::Type_descriptor());
 
 Review comment:
   I've just realized that the requirement that the field `type` has the type 
`Type` is 
   a) not checked anywhere 
   and 
   b) is completely useless now, when the UnionValidator is initialized once 
per message type.
   I'll simply remove this argument.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to