1818
1919
2020def _not_null (value , field ):
21+ """Check whether 'value' should be coerced to 'field' type."""
2122 return value is not None or field .mode != 'NULLABLE'
2223
2324
2425def _int_from_json (value , field ):
26+ """Coerce 'value' to an int, if set or not nullable."""
2527 if _not_null (value , field ):
2628 return int (value )
2729
2830
2931def _float_from_json (value , field ):
32+ """Coerce 'value' to a float, if set or not nullable."""
3033 if _not_null (value , field ):
3134 return float (value )
3235
3336
3437def _bool_from_json (value , field ):
38+ """Coerce 'value' to a bool, if set or not nullable."""
3539 if _not_null (value , field ):
3640 return value .lower () in ['t' , 'true' , '1' ]
3741
3842
3943def _datetime_from_json (value , field ):
44+ """Coerce 'value' to a datetime, if set or not nullable."""
4045 if _not_null (value , field ):
4146 # value will be a float in seconds, to microsecond precision, in UTC.
4247 return _datetime_from_microseconds (1e6 * float (value ))
4348
4449
4550def _record_from_json (value , field ):
51+ """Coerce 'value' to a mapping, if set or not nullable."""
4652 if _not_null (value , field ):
4753 record = {}
4854 for subfield , cell in zip (field .fields , value ['f' ]):
@@ -56,6 +62,7 @@ def _record_from_json(value, field):
5662
5763
5864def _string_from_json (value , _ ):
65+ """NOOP string -> string coercion"""
5966 return value
6067
6168
@@ -70,6 +77,7 @@ def _string_from_json(value, _):
7077
7178
7279def _rows_from_json (rows , schema ):
80+ """Convert JSON row data to rows w/ appropriate types."""
7381 rows_data = []
7482 for row in rows :
7583 row_data = []
@@ -132,6 +140,10 @@ def __init__(self, name, property_type):
132140 self .property_type = property_type
133141
134142 def _validate (self , value ):
143+ """Ensure that 'value' is of the appropriate type.
144+
145+ :raises: ValueError on a type mismatch.
146+ """
135147 if not isinstance (value , self .property_type ):
136148 raise ValueError ('Required type: %s' % (self .property_type ,))
137149
0 commit comments