def DispatchKeyForTagSpecOrNone(tag_spec):
  """For a provided tag_spec, generates its dispatch key.

  Args:
    tag_spec: an instance of type validator_pb2.TagSpec.

  Returns:
    a string indicating the dispatch key, or None.
  """
  for attr in tag_spec.attrs:
    if attr.dispatch_key:
      mandatory_parent = tag_spec.mandatory_parent or ''
      attr_name = attr.name
      attr_value = attr.value_casei or attr.value.lower()
      assert attr_value is not None
      return '%s\\0%s\\0%s' % (attr_name, attr_value, mandatory_parent)
  return None