31 """Provides type checking routines.
33 This module defines type checking utilities in the forms of dictionaries:
35 VALUE_CHECKERS: A dictionary of field types and a value validation object.
36 TYPE_TO_BYTE_SIZE_FN: A dictionary with field types and a size computing
38 TYPE_TO_SERIALIZE_METHOD: A dictionary with field types and serialization
40 FIELD_TYPE_TO_WIRE_TYPE: A dictionary with field typed and their
41 coresponding wire types.
42 TYPE_TO_DESERIALIZE_METHOD: A dictionary with field types and deserialization
46 __author__ =
'robinson@google.com (Will Robinson)'
63 return field_descriptor.containing_type.syntax ==
"proto3"
66 """Returns a type checker for a message field of the specified types.
69 field: FieldDescriptor object for this field.
72 An instance of TypeChecker which can be used to verify the types
73 of values assigned to a field of the specified type.
75 if (field.cpp_type == _FieldDescriptor.CPPTYPE_STRING
and
76 field.type == _FieldDescriptor.TYPE_STRING):
78 if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
81 return _VALUE_CHECKERS[_FieldDescriptor.CPPTYPE_INT32]
84 return _VALUE_CHECKERS[field.cpp_type]
94 """Type checker used to catch type errors as early as possible
95 when the client is setting scalar fields in protocol messages.
102 """Type check the provided value and return it.
104 The returned value might have been normalized to another type.
107 message = (
'%.1024r has type %s, but expected one of: %s' %
109 raise TypeError(message)
115 return proposed_value
120 def __init__(self, default_value, *acceptable_types):
121 TypeChecker.__init__(self, *acceptable_types)
132 """Checker used for integer fields. Performs type-check and range check."""
135 if not isinstance(proposed_value, numbers.Integral):
136 message = (
'%.1024r has type %s, but expected one of: %s' %
137 (proposed_value, type(proposed_value), six.integer_types))
138 raise TypeError(message)
139 if not self._MIN <= int(proposed_value) <= self._MAX:
140 raise ValueError(
'Value out of range: %d' % proposed_value)
144 proposed_value = self._TYPE(proposed_value)
145 return proposed_value
153 """Checker used for enum fields. Performs type-check and range check."""
159 if not isinstance(proposed_value, numbers.Integral):
160 message = (
'%.1024r has type %s, but expected one of: %s' %
161 (proposed_value, type(proposed_value), six.integer_types))
162 raise TypeError(message)
163 if int(proposed_value)
not in self.
_enum_type.values_by_number:
164 raise ValueError(
'Unknown enum value: %d' % proposed_value)
165 return proposed_value
173 """Checker used for string fields.
175 Always returns a unicode value, even if the input is of type str.
179 if not isinstance(proposed_value, (bytes, six.text_type)):
180 message = (
'%.1024r has type %s, but expected one of: %s' %
181 (proposed_value, type(proposed_value), (bytes, six.text_type)))
182 raise TypeError(message)
185 if isinstance(proposed_value, bytes):
187 proposed_value = proposed_value.decode(
'utf-8')
188 except UnicodeDecodeError:
189 raise ValueError(
'%.1024r has type bytes, but isn\'t valid UTF-8 '
190 'encoding. Non-UTF-8 strings must be converted to '
191 'unicode objects before being added.' %
195 proposed_value.encode(
'utf8')
196 except UnicodeEncodeError:
197 raise ValueError(
'%.1024r isn\'t a valid unicode string and '
198 'can\'t be encoded in UTF-8.'%
201 return proposed_value
234 _FLOAT_MAX = float.fromhex(
'0x1.fffffep+127')
235 _FLOAT_MIN = -_FLOAT_MAX
237 _NEG_INF = float(
'-inf')
242 """Checker used for float fields. Performs type-check and range check.
244 Values exceeding a 32-bit float will be converted to inf/-inf.
248 """Check and convert proposed_value to float."""
249 if not isinstance(proposed_value, numbers.Real):
250 message = (
'%.1024r has type %s, but expected one of: numbers.Real' %
251 (proposed_value, type(proposed_value)))
252 raise TypeError(message)
253 converted_value = float(proposed_value)
255 if converted_value > _FLOAT_MAX:
257 if converted_value < _FLOAT_MIN:
260 return converted_value
275 0.0, float, numbers.Real),
278 False, bool, numbers.Integral),
287 TYPE_TO_BYTE_SIZE_FN = {
288 _FieldDescriptor.TYPE_DOUBLE: wire_format.DoubleByteSize,
289 _FieldDescriptor.TYPE_FLOAT: wire_format.FloatByteSize,
290 _FieldDescriptor.TYPE_INT64: wire_format.Int64ByteSize,
291 _FieldDescriptor.TYPE_UINT64: wire_format.UInt64ByteSize,
292 _FieldDescriptor.TYPE_INT32: wire_format.Int32ByteSize,
293 _FieldDescriptor.TYPE_FIXED64: wire_format.Fixed64ByteSize,
294 _FieldDescriptor.TYPE_FIXED32: wire_format.Fixed32ByteSize,
295 _FieldDescriptor.TYPE_BOOL: wire_format.BoolByteSize,
296 _FieldDescriptor.TYPE_STRING: wire_format.StringByteSize,
297 _FieldDescriptor.TYPE_GROUP: wire_format.GroupByteSize,
298 _FieldDescriptor.TYPE_MESSAGE: wire_format.MessageByteSize,
299 _FieldDescriptor.TYPE_BYTES: wire_format.BytesByteSize,
300 _FieldDescriptor.TYPE_UINT32: wire_format.UInt32ByteSize,
301 _FieldDescriptor.TYPE_ENUM: wire_format.EnumByteSize,
302 _FieldDescriptor.TYPE_SFIXED32: wire_format.SFixed32ByteSize,
303 _FieldDescriptor.TYPE_SFIXED64: wire_format.SFixed64ByteSize,
304 _FieldDescriptor.TYPE_SINT32: wire_format.SInt32ByteSize,
305 _FieldDescriptor.TYPE_SINT64: wire_format.SInt64ByteSize
311 _FieldDescriptor.TYPE_DOUBLE: encoder.DoubleEncoder,
312 _FieldDescriptor.TYPE_FLOAT: encoder.FloatEncoder,
313 _FieldDescriptor.TYPE_INT64: encoder.Int64Encoder,
314 _FieldDescriptor.TYPE_UINT64: encoder.UInt64Encoder,
315 _FieldDescriptor.TYPE_INT32: encoder.Int32Encoder,
316 _FieldDescriptor.TYPE_FIXED64: encoder.Fixed64Encoder,
317 _FieldDescriptor.TYPE_FIXED32: encoder.Fixed32Encoder,
318 _FieldDescriptor.TYPE_BOOL: encoder.BoolEncoder,
319 _FieldDescriptor.TYPE_STRING: encoder.StringEncoder,
320 _FieldDescriptor.TYPE_GROUP: encoder.GroupEncoder,
321 _FieldDescriptor.TYPE_MESSAGE: encoder.MessageEncoder,
322 _FieldDescriptor.TYPE_BYTES: encoder.BytesEncoder,
323 _FieldDescriptor.TYPE_UINT32: encoder.UInt32Encoder,
324 _FieldDescriptor.TYPE_ENUM: encoder.EnumEncoder,
325 _FieldDescriptor.TYPE_SFIXED32: encoder.SFixed32Encoder,
326 _FieldDescriptor.TYPE_SFIXED64: encoder.SFixed64Encoder,
327 _FieldDescriptor.TYPE_SINT32: encoder.SInt32Encoder,
328 _FieldDescriptor.TYPE_SINT64: encoder.SInt64Encoder,
334 _FieldDescriptor.TYPE_DOUBLE: encoder.DoubleSizer,
335 _FieldDescriptor.TYPE_FLOAT: encoder.FloatSizer,
336 _FieldDescriptor.TYPE_INT64: encoder.Int64Sizer,
337 _FieldDescriptor.TYPE_UINT64: encoder.UInt64Sizer,
338 _FieldDescriptor.TYPE_INT32: encoder.Int32Sizer,
339 _FieldDescriptor.TYPE_FIXED64: encoder.Fixed64Sizer,
340 _FieldDescriptor.TYPE_FIXED32: encoder.Fixed32Sizer,
341 _FieldDescriptor.TYPE_BOOL: encoder.BoolSizer,
342 _FieldDescriptor.TYPE_STRING: encoder.StringSizer,
343 _FieldDescriptor.TYPE_GROUP: encoder.GroupSizer,
344 _FieldDescriptor.TYPE_MESSAGE: encoder.MessageSizer,
345 _FieldDescriptor.TYPE_BYTES: encoder.BytesSizer,
346 _FieldDescriptor.TYPE_UINT32: encoder.UInt32Sizer,
347 _FieldDescriptor.TYPE_ENUM: encoder.EnumSizer,
348 _FieldDescriptor.TYPE_SFIXED32: encoder.SFixed32Sizer,
349 _FieldDescriptor.TYPE_SFIXED64: encoder.SFixed64Sizer,
350 _FieldDescriptor.TYPE_SINT32: encoder.SInt32Sizer,
351 _FieldDescriptor.TYPE_SINT64: encoder.SInt64Sizer,
357 _FieldDescriptor.TYPE_DOUBLE: decoder.DoubleDecoder,
358 _FieldDescriptor.TYPE_FLOAT: decoder.FloatDecoder,
359 _FieldDescriptor.TYPE_INT64: decoder.Int64Decoder,
360 _FieldDescriptor.TYPE_UINT64: decoder.UInt64Decoder,
361 _FieldDescriptor.TYPE_INT32: decoder.Int32Decoder,
362 _FieldDescriptor.TYPE_FIXED64: decoder.Fixed64Decoder,
363 _FieldDescriptor.TYPE_FIXED32: decoder.Fixed32Decoder,
364 _FieldDescriptor.TYPE_BOOL: decoder.BoolDecoder,
365 _FieldDescriptor.TYPE_STRING: decoder.StringDecoder,
366 _FieldDescriptor.TYPE_GROUP: decoder.GroupDecoder,
367 _FieldDescriptor.TYPE_MESSAGE: decoder.MessageDecoder,
368 _FieldDescriptor.TYPE_BYTES: decoder.BytesDecoder,
369 _FieldDescriptor.TYPE_UINT32: decoder.UInt32Decoder,
370 _FieldDescriptor.TYPE_ENUM: decoder.EnumDecoder,
371 _FieldDescriptor.TYPE_SFIXED32: decoder.SFixed32Decoder,
372 _FieldDescriptor.TYPE_SFIXED64: decoder.SFixed64Decoder,
373 _FieldDescriptor.TYPE_SINT32: decoder.SInt32Decoder,
374 _FieldDescriptor.TYPE_SINT64: decoder.SInt64Decoder,
378 FIELD_TYPE_TO_WIRE_TYPE = {
379 _FieldDescriptor.TYPE_DOUBLE: wire_format.WIRETYPE_FIXED64,
380 _FieldDescriptor.TYPE_FLOAT: wire_format.WIRETYPE_FIXED32,
381 _FieldDescriptor.TYPE_INT64: wire_format.WIRETYPE_VARINT,
382 _FieldDescriptor.TYPE_UINT64: wire_format.WIRETYPE_VARINT,
383 _FieldDescriptor.TYPE_INT32: wire_format.WIRETYPE_VARINT,
384 _FieldDescriptor.TYPE_FIXED64: wire_format.WIRETYPE_FIXED64,
385 _FieldDescriptor.TYPE_FIXED32: wire_format.WIRETYPE_FIXED32,
386 _FieldDescriptor.TYPE_BOOL: wire_format.WIRETYPE_VARINT,
387 _FieldDescriptor.TYPE_STRING:
388 wire_format.WIRETYPE_LENGTH_DELIMITED,
389 _FieldDescriptor.TYPE_GROUP: wire_format.WIRETYPE_START_GROUP,
390 _FieldDescriptor.TYPE_MESSAGE:
391 wire_format.WIRETYPE_LENGTH_DELIMITED,
392 _FieldDescriptor.TYPE_BYTES:
393 wire_format.WIRETYPE_LENGTH_DELIMITED,
394 _FieldDescriptor.TYPE_UINT32: wire_format.WIRETYPE_VARINT,
395 _FieldDescriptor.TYPE_ENUM: wire_format.WIRETYPE_VARINT,
396 _FieldDescriptor.TYPE_SFIXED32: wire_format.WIRETYPE_FIXED32,
397 _FieldDescriptor.TYPE_SFIXED64: wire_format.WIRETYPE_FIXED64,
398 _FieldDescriptor.TYPE_SINT32: wire_format.WIRETYPE_VARINT,
399 _FieldDescriptor.TYPE_SINT64: wire_format.WIRETYPE_VARINT,