예제 #1
0
def FormatField(type_context, field):
    """Format FieldDescriptorProto as a proto field.

  Args:
    type_context: contextual information for message/enum/field.
    field: FieldDescriptor proto.

  Returns:
    Formatted proto field as a string.
  """
    if options.HasHideOption(field.options):
        return ''
    leading_comment, trailing_comment = FormatTypeContextComments(type_context)
    annotations = []
    if field.options.HasExtension(validate_pb2.rules):
        rules = field.options.Extensions[validate_pb2.rules]
        annotations.append('(validate.rules) %s' %
                           FormatValidateFieldRules(rules))
    if field.options.deprecated:
        annotations.append('deprecated = true')
    formatted_annotations = '[ %s]' % ','.join(
        annotations) if annotations else ''
    return '%s%s %s = %d%s;\n%s' % (
        leading_comment, FormatFieldType(type_context, field), field.name,
        field.number, formatted_annotations, trailing_comment)
예제 #2
0
 def VisitMessage(self, msg_proto, type_context, nested_msgs, nested_enums):
     # Skip messages synthesized to represent map types.
     if msg_proto.options.map_entry:
         return ''
     if protoxform_options.HasHideOption(msg_proto.options):
         return ''
     annotation_xforms = {
         annotations.NEXT_FREE_FIELD_ANNOTATION:
         CreateNextFreeFieldXform(msg_proto)
     }
     leading_comment, trailing_comment = FormatTypeContextComments(
         type_context, annotation_xforms)
     formatted_options = FormatOptions(msg_proto.options, is_message=True)
     formatted_enums = FormatBlock('\n'.join(nested_enums))
     formatted_msgs = FormatBlock('\n'.join(nested_msgs))
     reserved_fields = FormatReserved(msg_proto)
     # Recover the oneof structure. This needs some extra work, since
     # DescriptorProto just gives use fields and a oneof_index that can allow
     # recovery of the original oneof placement.
     fields = ''
     oneof_index = None
     for index, field in enumerate(msg_proto.field):
         if oneof_index is not None:
             if not field.HasField(
                     'oneof_index') or field.oneof_index != oneof_index:
                 fields += '}\n\n'
                 oneof_index = None
         if oneof_index is None and field.HasField('oneof_index'):
             oneof_index = field.oneof_index
             oneof_proto = msg_proto.oneof_decl[oneof_index]
             if oneof_proto.options.HasExtension(validate_pb2.required):
                 oneof_options = 'option (validate.required) = true;\n\n'
             else:
                 oneof_options = ''
             oneof_leading_comment, oneof_trailing_comment = FormatTypeContextComments(
                 type_context.ExtendOneof(oneof_index, field.name))
             fields += '%soneof %s {\n%s%s' % (
                 oneof_leading_comment, oneof_proto.name,
                 oneof_trailing_comment, oneof_options)
         fields += FormatBlock(
             FormatField(type_context.ExtendField(index, field.name),
                         field))
     if oneof_index is not None:
         fields += '}\n\n'
     return '%smessage %s {\n%s%s%s%s%s%s\n}\n' % (
         leading_comment, msg_proto.name, trailing_comment,
         formatted_options, formatted_enums, formatted_msgs,
         reserved_fields, fields)
예제 #3
0
파일: protoprint.py 프로젝트: zhxinp/envoy
def FormatEnumValue(type_context, value):
  """Format a EnumValueDescriptorProto as a proto enum value.

  Args:
    type_context: contextual information for message/enum/field.
    value: EnumValueDescriptorProto.

  Returns:
    Formatted proto enum value as a string.
  """
  if protoxform_options.HasHideOption(value.options):
    return ''
  leading_comment, trailing_comment = FormatTypeContextComments(type_context)
  formatted_annotations = FormatOptions(value.options)
  return '%s%s = %d%s;\n%s' % (leading_comment, value.name, value.number, formatted_annotations,
                               trailing_comment)
예제 #4
0
파일: protoprint.py 프로젝트: zhxinp/envoy
def FormatField(type_context, field):
  """Format FieldDescriptorProto as a proto field.

  Args:
    type_context: contextual information for message/enum/field.
    field: FieldDescriptor proto.

  Returns:
    Formatted proto field as a string.
  """
  if protoxform_options.HasHideOption(field.options):
    return ''
  leading_comment, trailing_comment = FormatTypeContextComments(type_context)

  return '%s%s %s = %d%s;\n%s' % (leading_comment, FormatFieldType(type_context, field), field.name,
                                  field.number, FormatOptions(field.options), trailing_comment)
예제 #5
0
 def VisitEnum(self, enum_proto, type_context):
     if protoxform_options.HasHideOption(enum_proto.options):
         return ''
     leading_comment, trailing_comment = FormatTypeContextComments(
         type_context)
     formatted_options = FormatOptions(enum_proto.options)
     reserved_fields = FormatReserved(enum_proto)
     values = [
         FormatEnumValue(type_context.ExtendField(index, value.name), value)
         for index, value in enumerate(enum_proto.value)
     ]
     joined_values = ('\n' if any('//' in v
                                  for v in values) else '').join(values)
     return '%senum %s {\n%s%s%s%s\n}\n' % (
         leading_comment, enum_proto.name, trailing_comment,
         formatted_options, reserved_fields, joined_values)
예제 #6
0
def FormatEnumValue(type_context, value):
  """Format a EnumValueDescriptorProto as a proto enum value.

  Args:
    type_context: contextual information for message/enum/field.
    value: EnumValueDescriptorProto.

  Returns:
    Formatted proto enum value as a string.
  """
  if options.HasHideOption(value.options):
    return ''
  leading_comment, trailing_comment = FormatTypeContextComments(type_context)
  annotations = []
  if value.options.deprecated:
    annotations.append('deprecated = true')
  formatted_annotations = '[ %s]' % ','.join(annotations) if annotations else ''
  return '%s%s = %d%s;\n%s' % (leading_comment, value.name, value.number, formatted_annotations,
                               trailing_comment)