parameters: (message_descriptor, cls)
docstring: Adds the IsInitialized and FindInitializationError methods to the
    protocol message class.
docstring_summary: Adds the IsInitialized and FindInitializationError methods to the
    protocol message class.
docstring_tokens: Adds the IsInitialized and FindInitializationError methods to the protocol message class .
function: def _AddIsInitializedMethod(message_descriptor, cls):
    """Adds the IsInitialized and FindInitializationError methods to the
    protocol message class."""
  
    required_fields = [field for field in message_descriptor.fields
                             if field.label == _FieldDescriptor.LABEL_REQUIRED]
  
    def IsInitialized(self, errors=None):
      """Checks if all required fields of a message are set.
  
      Args:
        errors:  A list which, if provided, will be populated with the field
                 paths of all missing required fields.
  
      Returns:
        True iff the specified message has all required fields set.
      """
  
      # Performance is critical so we avoid HasField() and ListFields().
  
      for field in required_fields:
        if (field not in self._fields or
            (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
             not self._fields[field]._is_present_in_parent)):
          if errors is not None:
            errors.extend(self.FindInitializationErrors())
          return False
  
      for field, value in self._fields.iteritems():
        if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
          if field.label == _FieldDescriptor.LABEL_REPEATED:
            for element in value:
              if not element.IsInitialized():
                if errors is not None:
                  errors.extend(self.FindInitializationErrors())
                return False
          elif value._is_present_in_parent and not value.IsInitialized():
            if errors is not None:
              errors.extend(self.FindInitializationErrors())
            return False
  
      return True
  
    cls.IsInitialized = IsInitialized
  
    def FindInitializationErrors(self):
      """Finds required fields which are not initialized.
  
      Returns:
        A list of strings.  Each string is a path to an uninitialized field from
        the top-level message, e.g. "foo.bar[5].baz".
      """
  
      errors = []  # simplify things
  
      for field in required_fields:
        if not self.HasField(field.name):
          errors.append(field.name)
  
      for field, value in self.ListFields():
        if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
          if field.is_extension:
            name = "(%s)" % field.full_name
          else:
            name = field.name
  
          if field.label == _FieldDescriptor.LABEL_REPEATED:
            for i in xrange(len(value)):
              element = value[i]
              prefix = "%s[%d]." % (name, i)
              sub_errors = element.FindInitializationErrors()
              errors += [ prefix + error for error in sub_errors ]
          else:
            prefix = name + "."
            sub_errors = value.FindInitializationErrors()
            errors += [ prefix + error for error in sub_errors ]
  
      return errors
  
    cls.FindInitializationErrors = FindInitializationErrors
function_tokens: def _AddIsInitializedMethod ( message_descriptor , cls ) : required_fields = [ field for field in message_descriptor . fields if field . label == _FieldDescriptor . LABEL_REQUIRED ] def IsInitialized ( self , errors = None ) : """Checks if all required fields of a message are set.
  
      Args:
        errors:  A list which, if provided, will be populated with the field
                 paths of all missing required fields.
  
      Returns:
        True iff the specified message has all required fields set.
      """ # Performance is critical so we avoid HasField() and ListFields(). for field in required_fields : if ( field not in self . _fields or ( field . cpp_type == _FieldDescriptor . CPPTYPE_MESSAGE and not self . _fields [ field ] . _is_present_in_parent ) ) : if errors is not None : errors . extend ( self . FindInitializationErrors ( ) ) return False for field , value in self . _fields . iteritems ( ) : if field . cpp_type == _FieldDescriptor . CPPTYPE_MESSAGE : if field . label == _FieldDescriptor . LABEL_REPEATED : for element in value : if not element . IsInitialized ( ) : if errors is not None : errors . extend ( self . FindInitializationErrors ( ) ) return False elif value . _is_present_in_parent and not value . IsInitialized ( ) : if errors is not None : errors . extend ( self . FindInitializationErrors ( ) ) return False return True cls . IsInitialized = IsInitialized def FindInitializationErrors ( self ) : """Finds required fields which are not initialized.
  
      Returns:
        A list of strings.  Each string is a path to an uninitialized field from
        the top-level message, e.g. "foo.bar[5].baz".
      """ errors = [ ] # simplify things for field in required_fields : if not self . HasField ( field . name ) : errors . append ( field . name ) for field , value in self . ListFields ( ) : if field . cpp_type == _FieldDescriptor . CPPTYPE_MESSAGE : if field . is_extension : name = "(%s)" % field . full_name else : name = field . name if field . label == _FieldDescriptor . LABEL_REPEATED : for i in xrange ( len ( value ) ) : element = value [ i ] prefix = "%s[%d]." % ( name , i ) sub_errors = element . FindInitializationErrors ( ) errors += [ prefix + error for error in sub_errors ] else : prefix = name + "." sub_errors = value . FindInitializationErrors ( ) errors += [ prefix + error for error in sub_errors ] return errors cls . FindInitializationErrors = FindInitializationErrors