Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _generate_equality_func_helper(self, data_type, field, other_obj_name):
_, nullable = unwrap_nullable(field.data_type)
if is_union_type(data_type):
enum_field_name = fmt_enum_name(field.name, data_type)
self.emit('case {}:'.format(enum_field_name))
if nullable:
with self.block('if (self.{})'.format(fmt_var(field.name))):
self._generate_equality_check(data_type, field, other_obj_name)
else:
self._generate_equality_check(data_type, field, other_obj_name)
def _unpack_and_store_data_type(data_type):
data_type, _ = unwrap_nullable(data_type)
if is_list_type(data_type):
while is_list_type(data_type):
data_type, _ = unwrap_nullable(data_type.data_type)
if not is_void_type(data_type) and is_user_defined_type(data_type):
result.append(data_type)
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):
existing_dt._ast_node.lineno), item.lineno, item.path)
namespace = self.api.ensure_namespace(env.namespace_name)
if item.name in BUILTIN_ANNOTATION_CLASS_BY_STRING:
raise InvalidSpec('Cannot redefine built-in annotation type %s.' %
(quote(item.name), ), item.lineno, item.path)
params = []
for param in item.params:
if param.annotations:
raise InvalidSpec(
'Annotations cannot be applied to parameters of annotation types',
param.lineno, param.path)
param_type = self._resolve_type(env, param.type_ref, True)
dt, nullable_dt = unwrap_nullable(param_type)
if isinstance(dt, Void):
raise InvalidSpec(
'Parameter {} cannot be Void.'.format(quote(param.name)),
param.lineno, param.path)
if nullable_dt and param.has_default:
raise InvalidSpec(
'Parameter {} cannot be a nullable type and have '
'a default specified.'.format(quote(param.name)),
param.lineno, param.path)
if not is_primitive_type(dt):
raise InvalidSpec(
'Parameter {} must have a primitive type (possibly '
'nullable).'.format(quote(param.name)),
param.lineno, param.path)
def fmt_type(data_type, tag=False, has_default=False, no_ptr=False, is_prop=False):
data_type, nullable = unwrap_nullable(data_type)
if is_user_defined_type(data_type):
base = '{}' if no_ptr else '{} *'
result = base.format(fmt_class_prefix(data_type))
else:
result = _primitive_table.get(data_type.__class__,
fmt_class(data_type.name))
if is_list_type(data_type):
data_type, _ = unwrap_nullable(data_type.data_type)
base = '<{}>' if no_ptr else '<{}> *'
result = result + base.format(fmt_type(data_type))
elif is_map_type(data_type):
data_type, _ = unwrap_nullable(data_type.value_data_type)
base = '' if no_ptr else ' *'
result = result + base.format(fmt_type(data_type))
if tag:
if (nullable or has_default) and not is_prop:
result = 'nullable ' + result
return result
def fmt_type(data_type, tag=False, has_default=False, no_ptr=False, is_prop=False):
data_type, nullable = unwrap_nullable(data_type)
if is_user_defined_type(data_type):
base = '{}' if no_ptr else '{} *'
result = base.format(fmt_class_prefix(data_type))
else:
result = _primitive_table.get(data_type.__class__,
fmt_class(data_type.name))
if is_list_type(data_type):
data_type, _ = unwrap_nullable(data_type.data_type)
base = '<{}>' if no_ptr else '<{}> *'
result = result + base.format(fmt_type(data_type))
elif is_map_type(data_type):
data_type, _ = unwrap_nullable(data_type.value_data_type)
base = '' if no_ptr else ' *'
result = result + base.format(fmt_type(data_type))
if tag:
if (nullable or has_default) and not is_prop:
result = 'nullable ' + result
return result
def fmt_type(data_type, namespace=None, use_interface=False):
data_type, nullable = unwrap_nullable(data_type)
if is_list_type(data_type):
return '[]%s' % fmt_type(data_type.data_type, namespace, use_interface)
type_name = data_type.name
if use_interface and _needs_base_type(data_type):
type_name = 'Is' + type_name
if is_composite_type(data_type) and namespace is not None and \
namespace.name != data_type.namespace.name:
type_name = data_type.namespace.name + '.' + type_name
if use_interface and _needs_base_type(data_type):
return _type_table.get(data_type.__class__, type_name)
else:
return _type_table.get(data_type.__class__, '*' + type_name)
def fmt_type(data_type, namespace=None, use_interface=False, raw=False):
data_type, nullable = unwrap_nullable(data_type)
if is_list_type(data_type):
if raw and not _needs_base_type(data_type.data_type):
return "json.RawMessage"
return '[]%s' % fmt_type(data_type.data_type, namespace, use_interface, raw)
if raw:
return "json.RawMessage"
type_name = data_type.name
if use_interface and _needs_base_type(data_type):
type_name = 'Is' + type_name
if is_composite_type(data_type) and namespace is not None and \
namespace.name != data_type.namespace.name:
type_name = data_type.namespace.name + '.' + type_name
if use_interface and _needs_base_type(data_type):
return _type_table.get(data_type.__class__, type_name)
else:
if data_type.__class__ not in _type_table:
def _determine_validator_type(self, data_type, value):
data_type, nullable = unwrap_nullable(data_type)
if is_list_type(data_type):
item_validator = self._determine_validator_type(data_type.data_type, value)
if item_validator:
v = "arrayValidator({})".format(
self._func_args([
("minItems", data_type.min_items),
("maxItems", data_type.max_items),
("itemValidator", item_validator),
])
)
else:
return None
elif is_numeric_type(data_type):
v = "comparableValidator({})".format(
self._func_args([
("minValue", data_type.min_value),