Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
data_type = annotated_object.data_type
name = annotated_object.name
loc = annotated_object._ast_node.lineno, annotated_object._ast_node.path
curr_data_type = data_type
while isinstance(curr_data_type, Alias) or isinstance(curr_data_type, Nullable):
# aliases have redactors assocaited with the type itself
if hasattr(curr_data_type, 'redactor') and curr_data_type.redactor:
raise InvalidSpec("A redactor has already been defined for '%s' by '%s'." %
(str(name), str(curr_data_type.name)), *loc)
curr_data_type = curr_data_type.data_type
if hasattr(annotated_object, 'redactor') and annotated_object.redactor:
if is_map_type(curr_data_type) or is_list_type(curr_data_type):
while True:
if is_map_type(curr_data_type):
curr_data_type = curr_data_type.value_data_type
else:
curr_data_type = curr_data_type.data_type
should_continue = (is_map_type(curr_data_type) or is_list_type(curr_data_type)
or is_nullable_type(curr_data_type))
if should_continue is False:
break
if is_user_defined_type(curr_data_type) or is_void_type(curr_data_type):
raise InvalidSpec("Redactors can't be applied to user-defined or void types.", *loc)
def _fmt_serialization_call(self, data_type, input_value, serialize, depth=0):
"""Returns the appropriate serialization / deserialization method
call for the given data type."""
data_type, _ = unwrap_nullable(data_type)
serializer_func = 'serialize' if serialize else 'deserialize'
serializer_args = []
if is_primitive_type(data_type):
return input_value
if is_list_type(data_type) or is_map_type(data_type):
serializer_args.append(('value', input_value))
elem_data_type = (data_type.value_data_type if
is_map_type(data_type) else data_type.data_type)
serialization_call = self._fmt_serialization_call(
elem_data_type, 'elem{}'.format(depth), serialize, depth + 1)
data_struct_block = '^id(id elem{}) {{ return {}; }}'.format(
depth, serialization_call)
serializer_args.append(('withBlock', data_struct_block))
elif is_timestamp_type(data_type):
serializer_args.append(('value', input_value))
serializer_args.append(('dateFormat',
'@"{}"'.format(data_type.format)))
else:
serializer_args.append(('value', input_value))
return '{}'.format(
if not is_void_type(route.error_data_type):
caller = fmt_class_type(
route.error_data_type, suppress_ptr=True)
error_type = fmt_func_call(
caller=caller, callee='class')
else:
error_type = 'nil'
if is_list_type(route.arg_data_type) or is_map_type(route.arg_data_type):
dataStructSerialBlock = '^id(id dataStruct) {{ return {}; }}'.format(
self._fmt_serialization_call(
route.result_data_type, 'dataStruct', True))
else:
dataStructSerialBlock = 'nil'
if is_list_type(route.result_data_type) or is_map_type(route.result_data_type):
dataStructDeserialBlock = '^id(id dataStruct) {{ return {}; }}'.format(
self._fmt_serialization_call(
route.result_data_type, 'dataStruct', False))
else:
dataStructDeserialBlock = 'nil'
with self.block_func(
func=route_name,
args=[],
return_type='DBRoute *',
class_func=True):
with self.block('if (!{})'.format(route_name)):
with self.block(
'{} = [[DBRoute alloc] init:'.format(
route_name),
delim=(None, None),
# aliases have redactors assocaited with the type itself
if hasattr(curr_data_type, 'redactor') and curr_data_type.redactor:
raise InvalidSpec("A redactor has already been defined for '%s' by '%s'." %
(str(name), str(curr_data_type.name)), *loc)
curr_data_type = curr_data_type.data_type
if hasattr(annotated_object, 'redactor') and annotated_object.redactor:
if is_map_type(curr_data_type) or is_list_type(curr_data_type):
while True:
if is_map_type(curr_data_type):
curr_data_type = curr_data_type.value_data_type
else:
curr_data_type = curr_data_type.data_type
should_continue = (is_map_type(curr_data_type) or is_list_type(curr_data_type)
or is_nullable_type(curr_data_type))
if should_continue is False:
break
if is_user_defined_type(curr_data_type) or is_void_type(curr_data_type):
raise InvalidSpec("Redactors can't be applied to user-defined or void types.", *loc)
for t in doc_types:
self._find_dependencies_recursive(t, seen, output_types, output_routes)
for namespace_name, routes in routes_by_ns.items():
route_namespace = self.api.namespaces[namespace_name]
for route in routes:
output_routes[namespace_name].add(route)
route_types = route_namespace.get_route_io_data_types_for_route(route)
for route_type in route_types:
self._find_dependencies_recursive(route_type, seen, output_types,
output_routes)
elif is_list_type(data_type) or is_nullable_type(data_type):
# recurse on underlying field for aliases, lists, nullables, and fields
seen.add(data_type)
self._find_dependencies_recursive(data_type.data_type, seen, output_types,
output_routes)
elif is_map_type(data_type):
# recurse on key/value fields for maps
seen.add(data_type)
self._find_dependencies_recursive(data_type.key_data_type, seen, output_types,
output_routes)
self._find_dependencies_recursive(data_type.value_data_type, seen, output_types,
output_routes)
else:
assert False, "Unexpected type in: %s" % data_type
name = annotated_object.name
loc = annotated_object._ast_node.lineno, annotated_object._ast_node.path
curr_data_type = data_type
while isinstance(curr_data_type, Alias) or isinstance(curr_data_type, Nullable):
# aliases have redactors assocaited with the type itself
if hasattr(curr_data_type, 'redactor') and curr_data_type.redactor:
raise InvalidSpec("A redactor has already been defined for '%s' by '%s'." %
(str(name), str(curr_data_type.name)), *loc)
curr_data_type = curr_data_type.data_type
if hasattr(annotated_object, 'redactor') and annotated_object.redactor:
if is_map_type(curr_data_type) or is_list_type(curr_data_type):
while True:
if is_map_type(curr_data_type):
curr_data_type = curr_data_type.value_data_type
else:
curr_data_type = curr_data_type.data_type
should_continue = (is_map_type(curr_data_type) or is_list_type(curr_data_type)
or is_nullable_type(curr_data_type))
if should_continue is False:
break
if is_user_defined_type(curr_data_type) or is_void_type(curr_data_type):
raise InvalidSpec("Redactors can't be applied to user-defined or void types.", *loc)
def _fmt_serialization_call(self, data_type, input_value, serialize, depth=0):
"""Returns the appropriate serialization / deserialization method
call for the given data type."""
data_type, _ = unwrap_nullable(data_type)
serializer_func = 'serialize' if serialize else 'deserialize'
serializer_args = []
if is_primitive_type(data_type):
return input_value
if is_list_type(data_type) or is_map_type(data_type):
serializer_args.append(('value', input_value))
elem_data_type = (data_type.value_data_type if
is_map_type(data_type) else data_type.data_type)
serialization_call = self._fmt_serialization_call(
elem_data_type, 'elem{}'.format(depth), serialize, depth + 1)
data_struct_block = '^id(id elem{}) {{ return {}; }}'.format(
depth, serialization_call)
serializer_args.append(('withBlock', data_struct_block))
elif is_timestamp_type(data_type):
serializer_args.append(('value', input_value))
serializer_args.append(('dateFormat',
'@"{}"'.format(data_type.format)))
else:
serializer_args.append(('value', input_value))
return '{}'.format(
fmt_func_call(
caller=fmt_serial_obj(data_type),
callee=serializer_func,
def generate_validator_constructor(ns, data_type):
"""
Given a Stone data type, returns a string that can be used to construct
the appropriate validation object in Python.
"""
dt, nullable_dt = unwrap_nullable(data_type)
if is_list_type(dt):
v = generate_func_call(
'bv.List',
args=[
generate_validator_constructor(ns, dt.data_type)],
kwargs=[
('min_items', dt.min_items),
('max_items', dt.max_items)],
)
elif is_map_type(dt):
v = generate_func_call(
'bv.Map',
args=[
generate_validator_constructor(ns, dt.key_data_type),
generate_validator_constructor(ns, dt.value_data_type),
]
)
elif is_numeric_type(dt):
v = generate_func_call(
'bv.{}'.format(dt.name),
kwargs=[
('min_value', dt.min_value),
('max_value', dt.max_value)],
)
elif is_string_type(dt):
pattern = None