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_client(self, api):
self.emit_raw(base)
self.emit('import Alamofire')
self.emit()
with self.block('open class {}'.format(self.args.class_name)):
namespace_fields = []
for namespace in api.namespaces.values():
if namespace.routes:
namespace_fields.append((namespace.name,
fmt_class(namespace.name)))
for var, typ in namespace_fields:
self.emit('/// Routes within the {} namespace. '
'See {}Routes for details.'.format(var, typ))
self.emit('open var {}: {}Routes!'.format(var, typ))
self.emit()
with self.function_block('public init', args=self._func_args(
[('client', '{}'.format(self.args.transport_client_name))])):
for var, typ in namespace_fields:
self.emit('self.{} = {}Routes(client: client)'.format(var, typ))
with self.class_block(data_type, protocols=protocols):
for field in data_type.fields:
fdoc = self.process_doc(field.doc,
self._docf) if field.doc else undocumented
self.emit_wrapped_text(fdoc, prefix='/// ', width=120)
self.emit('public let {}: {}'.format(
fmt_var(field.name),
fmt_type(field.data_type),
))
self._generate_struct_init(namespace, data_type)
decl = 'open var' if not data_type.parent_type else 'open override var'
with self.block('{} description: String'.format(decl)):
cls = fmt_class(data_type.name) + 'Serializer'
self.emit('return "\\(SerializeUtil.prepareJSONForSerialization' +
'({}().serialize(self)))"'.format(cls))
self._generate_struct_class_serializer(namespace, data_type)
def serializer_block(self, data_type):
with self.class_block(fmt_class(data_type.name) + 'Serializer',
protocols=['JSONSerializer']):
self.emit("public init() { }")
yield
def _generate_struct_base_class_deserializer(self, namespace, data_type):
args = []
for field in data_type.all_fields:
var = fmt_var(field.name)
value = 'dict["{}"]'.format(field.name)
self.emit('let {} = {}.deserialize({} ?? {})'.format(
var,
fmt_serial_obj(field.data_type),
value,
fmt_default_value(namespace, field) if field.has_default else '.null'
))
args.append((var, var))
self.emit('return {}({})'.format(
fmt_class(data_type.name),
self._func_args(args)
))
def serializer_func(self, data_type):
with self.function_block('open func serialize',
args=self._func_args([('_ value', fmt_class(data_type.name))]),
return_type='JSON'):
yield
def deserializer_func(self, data_type):
with self.function_block('open func deserialize',
args=self._func_args([('_ json', 'JSON')]),
return_type=fmt_class(data_type.name)):
yield
self.logger.info('Copying StoneValidators.swift to output folder')
shutil.copy(os.path.join(rsrc_folder, 'StoneValidators.swift'),
self.target_folder_path)
self.logger.info('Copying StoneSerializers.swift to output folder')
shutil.copy(os.path.join(rsrc_folder, 'StoneSerializers.swift'),
self.target_folder_path)
self.logger.info('Copying StoneBase.swift to output folder')
shutil.copy(os.path.join(rsrc_folder, 'StoneBase.swift'),
self.target_folder_path)
jazzy_cfg_path = os.path.join('../Format', 'jazzy.json')
with open(jazzy_cfg_path) as jazzy_file:
jazzy_cfg = json.load(jazzy_file)
for namespace in api.namespaces.values():
ns_class = fmt_class(namespace.name)
with self.output_to_relative_path('{}.swift'.format(ns_class)):
self._generate_base_namespace_module(api, namespace)
jazzy_cfg['custom_categories'][1]['children'].append(ns_class)
if namespace.routes:
jazzy_cfg['custom_categories'][0]['children'].append(ns_class + 'Routes')
with self.output_to_relative_path('../../../../.jazzy.json'):
self.emit_raw(json.dumps(jazzy_cfg, indent=2) + '\n')
def generate(self, api):
for namespace in api.namespaces.values():
ns_class = fmt_class(namespace.name)
if namespace.routes:
with self.output_to_relative_path('{}Routes.swift'.format(ns_class)):
self._generate_routes(namespace)
with self.output_to_relative_path('{}.swift'.format(self.args.module_name)):
self._generate_client(api)
def _get_route_args(self, namespace, route):
data_type = route.arg_data_type
arg_type = fmt_type(data_type)
if is_struct_type(data_type):
arg_list = self._struct_init_args(data_type, namespace=namespace)
doc_list = [(fmt_var(f.name), self.process_doc(f.doc, self._docf)
if f.doc else undocumented) for f in data_type.fields if f.doc]
elif is_union_type(data_type):
arg_list = [(fmt_var(data_type.name), '{}.{}'.format(
fmt_class(namespace.name), fmt_class(data_type.name)))]
doc_list = [(fmt_var(data_type.name),
self.process_doc(data_type.doc, self._docf)
if data_type.doc else 'The {} union'.format(fmt_class(data_type.name)))]
else:
arg_list = [] if is_void_type(data_type) else [('request', arg_type)]
doc_list = []
return arg_list, doc_list