Exemplo n.º 1
0
def PrintField(field, value, out, indent=0, as_utf8=False, as_one_line=False):
    """Print a single field name/value pair.  For repeated fields, the value
  should be a single element."""

    out.write(b' ' * indent)
    if field.is_extension:
        out.write(b'[')
        if (field.containing_type.GetOptions().message_set_wire_format
                and field.type == descriptor.FieldDescriptor.TYPE_MESSAGE
                and field.message_type == field.extension_scope
                and field.label == descriptor.FieldDescriptor.LABEL_OPTIONAL):
            out.write(string_to_bytestr(field.message_type.full_name))
        else:
            out.write(string_to_bytestr(field.full_name))
        out.write(b']')
    elif field.type == descriptor.FieldDescriptor.TYPE_GROUP:
        # For groups, use the capitalized name.
        out.write(string_to_bytestr(field.message_type.name))
    else:
        out.write(string_to_bytestr(field.name))

    if field.cpp_type != descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
        # The colon is optional in this case, but our cross-language golden files
        # don't include it.
        out.write(b': ')

    PrintFieldValue(field, value, out, indent, as_utf8, as_one_line)
    if as_one_line:
        out.write(b' ')
    else:
        out.write(b'\n')
def PrintField(field, value, out, indent=0, as_utf8=False, as_one_line=False):
  """Print a single field name/value pair.  For repeated fields, the value
  should be a single element."""

  out.write(b' ' * indent);
  if field.is_extension:
    out.write(b'[')
    if (field.containing_type.GetOptions().message_set_wire_format and
        field.type == descriptor.FieldDescriptor.TYPE_MESSAGE and
        field.message_type == field.extension_scope and
        field.label == descriptor.FieldDescriptor.LABEL_OPTIONAL):
      out.write(string_to_bytestr(field.message_type.full_name))
    else:
      out.write(string_to_bytestr(field.full_name))
    out.write(b']')
  elif field.type == descriptor.FieldDescriptor.TYPE_GROUP:
    # For groups, use the capitalized name.
    out.write(string_to_bytestr(field.message_type.name))
  else:
    out.write(string_to_bytestr(field.name))

  if field.cpp_type != descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
    # The colon is optional in this case, but our cross-language golden files
    # don't include it.
    out.write(b': ')

  PrintFieldValue(field, value, out, indent, as_utf8, as_one_line)
  if as_one_line:
    out.write(b' ')
  else:
    out.write(b'\n')
Exemplo n.º 3
0
def PrintFieldValue(field,
                    value,
                    out,
                    indent=0,
                    as_utf8=False,
                    as_one_line=False):
    """Print a single field value (not including name).  For repeated fields,
  the value should be a single element."""

    if field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
        if as_one_line:
            out.write(b' { ')
            PrintMessage(value, out, indent, as_utf8, as_one_line)
            out.write(b'}')
        else:
            out.write(b' {\n')
            PrintMessage(value, out, indent + 2, as_utf8, as_one_line)
            out.write(b' ' * indent + b'}')
    elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_ENUM:
        out.write(
            string_to_bytestr(field.enum_type.values_by_number[value].name))
    elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_STRING:
        out.write(b'\"')
        if type(value) is unicode:
            out.write(_CEscape(value.encode('utf-8'), as_utf8))
        else:
            out.write(_CEscape(value, as_utf8))
        out.write(b'\"')
    elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_BOOL:
        if value:
            out.write(b"true")
        else:
            out.write(b"false")
    else:
        out.write(string_to_bytestr(str(value)))
def PrintFieldValue(field, value, out, indent=0,
                    as_utf8=False, as_one_line=False):
  """Print a single field value (not including name).  For repeated fields,
  the value should be a single element."""

  if field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
    if as_one_line:
      out.write(b' { ')
      PrintMessage(value, out, indent, as_utf8, as_one_line)
      out.write(b'}')
    else:
      out.write(b' {\n')
      PrintMessage(value, out, indent + 2, as_utf8, as_one_line)
      out.write(b' ' * indent + b'}')
  elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_ENUM:
    out.write(string_to_bytestr(field.enum_type.values_by_number[value].name))
  elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_STRING:
    out.write(b'\"')
    if type(value) is unicode:
      out.write(_CEscape(value.encode('utf-8'), as_utf8))
    else:
      out.write(_CEscape(value, as_utf8))
    out.write(b'\"')
  elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_BOOL:
    if value:
      out.write(b"true")
    else:
      out.write(b"false")
  else:
    out.write(string_to_bytestr(str(value)))
  def testConsumeIntegers(self):
    # This test only tests the failures in the integer parsing methods as well
    # as the '0' special cases.
    int64_max = (1 << 63) - 1
    uint32_max = (1 << 32) - 1
    text = string_to_bytestr('-1 %d %d' % (uint32_max + 1, int64_max + 1))
    tokenizer = text_format._Tokenizer(text)
    self.assertRaises(text_format.ParseError, tokenizer.ConsumeUint32)
    self.assertRaises(text_format.ParseError, tokenizer.ConsumeUint64)
    self.assertEqual(-1, tokenizer.ConsumeInt32())

    self.assertRaises(text_format.ParseError, tokenizer.ConsumeUint32)
    self.assertRaises(text_format.ParseError, tokenizer.ConsumeInt32)
    self.assertEqual(uint32_max + 1, tokenizer.ConsumeInt64())

    self.assertRaises(text_format.ParseError, tokenizer.ConsumeInt64)
    self.assertEqual(int64_max + 1, tokenizer.ConsumeUint64())
    self.assertTrue(tokenizer.AtEnd())

    text = b'-0 -0 0 0'
    tokenizer = text_format._Tokenizer(text)
    self.assertEqual(0, tokenizer.ConsumeUint32())
    self.assertEqual(0, tokenizer.ConsumeUint64())
    self.assertEqual(0, tokenizer.ConsumeUint32())
    self.assertEqual(0, tokenizer.ConsumeUint64())
    self.assertTrue(tokenizer.AtEnd())
Exemplo n.º 6
0
    def testConsumeIntegers(self):
        # This test only tests the failures in the integer parsing methods as well
        # as the '0' special cases.
        int64_max = (1 << 63) - 1
        uint32_max = (1 << 32) - 1
        text = string_to_bytestr('-1 %d %d' % (uint32_max + 1, int64_max + 1))
        tokenizer = text_format._Tokenizer(text)
        self.assertRaises(text_format.ParseError, tokenizer.ConsumeUint32)
        self.assertRaises(text_format.ParseError, tokenizer.ConsumeUint64)
        self.assertEqual(-1, tokenizer.ConsumeInt32())

        self.assertRaises(text_format.ParseError, tokenizer.ConsumeUint32)
        self.assertRaises(text_format.ParseError, tokenizer.ConsumeInt32)
        self.assertEqual(uint32_max + 1, tokenizer.ConsumeInt64())

        self.assertRaises(text_format.ParseError, tokenizer.ConsumeInt64)
        self.assertEqual(int64_max + 1, tokenizer.ConsumeUint64())
        self.assertTrue(tokenizer.AtEnd())

        text = b'-0 -0 0 0'
        tokenizer = text_format._Tokenizer(text)
        self.assertEqual(0, tokenizer.ConsumeUint32())
        self.assertEqual(0, tokenizer.ConsumeUint64())
        self.assertEqual(0, tokenizer.ConsumeUint32())
        self.assertEqual(0, tokenizer.ConsumeUint64())
        self.assertTrue(tokenizer.AtEnd())
Exemplo n.º 7
0
 def EncodeField(write, value):
     encoded = string_to_bytestr(value)
     write(tag)
     local_EncodeVarint(write, local_len(encoded))
     return write(encoded)
Exemplo n.º 8
0
 def EncodeRepeatedField(write, value):
     for element in value:
         encoded = string_to_bytestr(element)
         write(tag)
         local_EncodeVarint(write, local_len(encoded))
         write(encoded)
Exemplo n.º 9
0
 def EncodeField(write, value):
   encoded = string_to_bytestr(value)
   write(tag)
   local_EncodeVarint(write, local_len(encoded))
   return write(encoded)
Exemplo n.º 10
0
 def EncodeRepeatedField(write, value):
   for element in value:
     encoded = string_to_bytestr(element)
     write(tag)
     local_EncodeVarint(write, local_len(encoded))
     write(encoded)