(value, field, require_str=False)
Convert a single scalar field value.

  Args:
    value: A scalar value to convert the scalar field value.
    field: The descriptor of the field to convert.
    require_str: If True, the field value must be a str.

  Returns:
    The converted scalar field value

  Raises:
    ParseError: In case of convert problems.
Convert a single scalar field value.
Convert a single scalar field value .
def _ConvertScalarFieldValue(value, field, require_str=False):
  """Convert a single scalar field value.

  Args:
    value: A scalar value to convert the scalar field value.
    field: The descriptor of the field to convert.
    require_str: If True, the field value must be a str.

  Returns:
    The converted scalar field value

  Raises:
    ParseError: In case of convert problems.
  """
  if field.cpp_type in _INT_TYPES:
    return _ConvertInteger(value)
  elif field.cpp_type in _FLOAT_TYPES:
    return _ConvertFloat(value)
  elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_BOOL:
    return _ConvertBool(value, require_str)
  elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_STRING:
    if field.type == descriptor.FieldDescriptor.TYPE_BYTES:
      return base64.b64decode(value)
    else:
      # Checking for unpaired surrogates appears to be unreliable,
      # depending on the specific Python version, so we check manually.
      if _UNPAIRED_SURROGATE_PATTERN.search(value):
        raise ParseError('Unpaired surrogate')
      return value
  elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_ENUM:
    # Convert an enum value.
    enum_value = field.enum_type.values_by_name.get(value, None)
    if enum_value is None:
      try:
        number = int(value)
        enum_value = field.enum_type.values_by_number.get(number, None)
      except ValueError:
        raise ParseError('Invalid enum value {0} for enum type {1}.'.format(
            value, field.enum_type.full_name))
      if enum_value is None:
        raise ParseError('Invalid enum value {0} for enum type {1}.'.format(
            value, field.enum_type.full_name))
    return enum_value.number
def _ConvertScalarFieldValue ( value , field , require_str = False ) : if field . cpp_type in _INT_TYPES : return _ConvertInteger ( value ) elif field . cpp_type in _FLOAT_TYPES : return _ConvertFloat ( value ) elif field . cpp_type == descriptor . FieldDescriptor . CPPTYPE_BOOL : return _ConvertBool ( value , require_str ) elif field . cpp_type == descriptor . FieldDescriptor . CPPTYPE_STRING : if field . type == descriptor . FieldDescriptor . TYPE_BYTES : return base64 . b64decode ( value ) else : # Checking for unpaired surrogates appears to be unreliable, # depending on the specific Python version, so we check manually. if _UNPAIRED_SURROGATE_PATTERN . search ( value ) : raise ParseError ( 'Unpaired surrogate' ) return value elif field . cpp_type == descriptor . FieldDescriptor . CPPTYPE_ENUM : # Convert an enum value. enum_value = field . enum_type . values_by_name . get ( value , None ) if enum_value is None : try : number = int ( value ) enum_value = field . enum_type . values_by_number . get ( number , None ) except ValueError : raise ParseError ( 'Invalid enum value {0} for enum type {1}.' . format ( value , field . enum_type . full_name ) ) if enum_value is None : raise ParseError ( 'Invalid enum value {0} for enum type {1}.' . format ( value , field . enum_type . full_name ) ) return enum_value . number