title: using protobuf reflection to get a boolean field descriptor in c++
body: <p>I am using grpc in a C++ application.  On the client side, I need to reflectively query a boolean value from a message using reflection. </p>
  
  <p>The response message is shown below.  Unfortunately I cannot see field descriptors for the bool mAP field - I can however see the FieldDescriptors for the string fields,  What am I doing wrong?</p>
  
  <p>John</p>
  
  <pre><code>message Response {
      // OTP Connection Status
      enum OTPConnStatus {
          Disconnected              = 0;
          Connected                 = 1;
          InvalidCS                 = 2;
          DiscRequest               = 3;
      }
  
      // define the fields
      RXMessageType mMessageType    = 1;
      bool mAP                      = 2;
      OTPConnStatus mCS1            = 3;
      OTPConnStatus mCS2            = 4;
      OTPConnStatus mCS3            = 5;
      OTPConnStatus mCS4            = 6;
      string mOTP1                  = 7;
      string mOTP2                  = 8;
      string mOTP3                  = 9;
      string mOTP4                  = 10;
  }
  </code></pre>
  
  
  
  <pre><code>const auto reflection = pMessage-&gt;GetReflection();
  std::vector&lt;const FieldDescriptor*&gt; fields;
  pMessage-&gt;GetReflection()-&gt;ListFields(*pMessage, &amp;fields);
  const auto fieldIter = std::find_if(fields.cbegin(), fields.cend(),
      [&amp;lcFieldName](const FieldDescriptor* next) {
          return boost::iequals(next-&gt;name(), lcFieldName);
      });
  if (fieldIter != fields.cend()) {
      std::string result;
      auto fieldDescriptor = *fieldIter;
      if (!fieldDescriptor-&gt;is_repeated()) {
          switch (fieldDescriptor-&gt;cpp_type()) {
          case FieldDescriptor::CPPTYPE_INT32:
              result = std::to_string(reflection-&gt;GetInt32 (
                  *pMessage, fieldDescriptor));
              break;
          case FieldDescriptor::CPPTYPE_INT64:
              result = std::to_string(reflection-&gt;GetInt64 (
                  *pMessage, fieldDescriptor));
              break;
          case FieldDescriptor::CPPTYPE_UINT32:
              result = std::to_string(reflection-&gt;GetUInt32 (
                  *pMessage, fieldDescriptor));
              break;
          case FieldDescriptor::CPPTYPE_UINT64:
              result = std::to_string(reflection-&gt;GetUInt64 (
                  *pMessage, fieldDescriptor));
              break;
          case FieldDescriptor::CPPTYPE_DOUBLE:
              result = std::to_string(reflection-&gt;GetDouble (
                  *pMessage, fieldDescriptor));
              break;
          case FieldDescriptor::CPPTYPE_FLOAT:
              result = std::to_string(reflection-&gt;GetFloat (
                  *pMessage, fieldDescriptor));
              break;
          case FieldDescriptor::CPPTYPE_BOOL:
              result = reflection-&gt;GetBool(
                  *pMessage, fieldDescriptor) ?
                  "true" : "false";
              break;
          case FieldDescriptor::CPPTYPE_ENUM:
              result = reflection-&gt;GetEnum(
                  *pMessage, fieldDescriptor)-&gt;
                      full_name();
              break;
          case FieldDescriptor::CPPTYPE_STRING:
              result = reflection-&gt;GetString(
                  *pMessage, fieldDescriptor);
              break;
          case FieldDescriptor::CPPTYPE_MESSAGE:
              //result = reflection-&gt;GetMessage(
              //    *pMessage, fieldDescriptor);
              break;
          }
      }
      std::cout &lt;&lt; result &lt;&lt; std::endl;
      exit(0);
  } else {
  </code></pre>
  
  <p>EDIT: I found a utility in the protobuf used to print messages which I used to print the contents.  Here are the results:</p>
  
  <pre><code>const auto reflection = pMessage-&gt;GetReflection();
  std::string formatted;
  pb::TextFormat::PrintToString(*pMessage, &amp;formatted);
  std::cout &lt;&lt; formatted;
  </code></pre>
  
  <p>Printed:</p>
  
  <pre><code>mMessageType: OneTimePassword
  mOTP1: "TAILNO1"
  mOTP2: "TAILNO2"
  mOTP3: "TAILNO3"
  mOTP4: "TAILNO4"
  </code></pre>
  
  <p>Also, note that the Boolean field in question 'map' is being set by the server.  Looking at the generated <code>CAService.pb.h</code> code and comparing the <code>MessageType</code> (which does have a working field descriptor) to the <code>mAP</code> field which does not, may be useful to protobuf users to show me the error in my approach.</p>
  
  <pre><code>// optional .ca.RXMessageType mMessageType = 1;
  inline void OTPResponse::clear_mmessagetype() {
    mmessagetype_ = 0;
  }
  inline ::ca::RXMessageType OTPResponse::mmessagetype() const {
    // @@protoc_insertion_point(field_get:ca.OTPResponse.mMessageType)
    return static_cast&lt; ::ca::RXMessageType &gt;(mmessagetype_);
  }
  inline void OTPResponse::set_mmessagetype(::ca::RXMessageType value) {
  
    mmessagetype_ = value;
    // @@protoc_insertion_point(field_set:ca.OTPResponse.mMessageType)
  }
  
  // optional bool mAP = 2;
  inline void OTPResponse::clear_map() {
    map_ = false;
  }
  inline bool OTPResponse::map() const {
    // @@protoc_insertion_point(field_get:ca.OTPResponse.mAP)
    return map_;
  }
  inline void OTPResponse::set_map(bool value) {
  
    map_ = value;
    // @@protoc_insertion_point(field_set:ca.OTPResponse.mAP)
  }
  </code></pre>