Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
with self.block('type %s struct' % name):
self.emit('dropbox.Tagged')
for field in fields:
if is_void_type(field.data_type):
continue
self._generate_field(field, union_field=True,
namespace=namespace)
self.emit()
self.emit('// Valid tag values for %s' % fmt_var(u.name))
with self.block('const', delim=('(', ')')):
for field in fields:
self.emit('%s%s = "%s"' % (fmt_var(u.name), fmt_var(field.name), field.name))
self.emit()
num_void_fields = sum([is_void_type(f.data_type) for f in fields])
# Simple structure, no need in UnmarshalJSON
if len(fields) == num_void_fields:
return
self.emit('// UnmarshalJSON deserializes into a %s instance' % name)
with self.block('func (u *%s) UnmarshalJSON(body []byte) error' % name):
with self.block('type wrap struct'):
self.emit('dropbox.Tagged')
for field in fields:
if is_void_type(field.data_type) or \
is_primitive_type(field.data_type):
continue
self._generate_field(field, union_field=True,
namespace=namespace, raw=True)
self.emit('var w wrap')
self.emit('var err error')
with self.block('type wrap struct'):
self.emit('dropbox.Tagged')
for field in fields:
if is_void_type(field.data_type) or \
is_primitive_type(field.data_type):
continue
self._generate_field(field, union_field=True,
namespace=namespace, raw=True)
self.emit('var w wrap')
self.emit('var err error')
with self.block('if err = json.Unmarshal(body, &w); err != nil'):
self.emit('return err')
self.emit('u.Tag = w.Tag')
with self.block('switch u.Tag'):
for field in fields:
if is_void_type(field.data_type):
continue
field_name = fmt_var(field.name)
with self.block('case "%s":' % field.name, delim=(None, None)):
if is_union_type(field.data_type):
self.emit('err = json.Unmarshal(w.{0}, &u.{0})'
.format(field_name))
elif is_struct_type(field.data_type) and \
field.data_type.has_enumerated_subtypes():
self.emit("u.{0}, err = Is{1}FromJSON(body)"
.format(field_name, field.data_type.name))
else:
self.emit('err = json.Unmarshal(body, &u.{0})'
.format(field_name))
with self.block("if err != nil"):
self.emit("return err")
self.emit('return nil')
for name, doc in doc_list:
self.emit_wrapped_text(
'@param {} {}'.format(name, doc if doc else undocumented),
prefix=comment_prefix,
width=120)
self.emit(comment_prefix)
output = (
'@return Through the response callback, the caller will ' +
'receive a `{}` object on success or a `{}` object on failure.')
output = output.format(
fmt_type(route.result_data_type, tag=False, no_ptr=True),
fmt_type(route.error_data_type, tag=False, no_ptr=True))
self.emit_wrapped_text(output, prefix=comment_prefix, width=120)
self.emit(comment_prefix)
result_type_str = fmt_type(route.result_data_type) if not is_void_type(
route.result_data_type) else 'DBNilObject *'
error_type_str = fmt_type(route.error_data_type) if not is_void_type(
route.error_data_type) else 'DBNilObject *'
return_type = '{}<{}, {}> *'.format(task_type_name, result_type_str,
error_type_str)
deprecated = self._get_deprecation_warning(route)
route_signature = fmt_signature(
func=func_name,
args=fmt_func_args_declaration(route_args),
return_type='{}'.format(return_type))
self.emit('{}{};'.format(route_signature, deprecated))
self.emit()
def _generate_union_class_symbol_creators(self, data_type):
"""
Class attributes that represent a symbol are set after the union class
definition.
"""
class_name = fmt_class(data_type.name)
lineno = self.lineno
for field in data_type.fields:
if is_void_type(field.data_type):
field_name = fmt_func(field.name)
self.emit("{0}.{1} = {0}('{1}')".format(class_name, field_name))
if lineno != self.lineno:
self.emit()
def _generate_union_properties(self, fields):
"""Emits union instance properties from the given fields."""
for field in fields:
# void types do not need properties to store additional state
# information
if not is_void_type(field.data_type):
doc = self.process_doc(
field.doc, self._docf) if field.doc else undocumented
warning_str = (
' @note Ensure the `is{}` method returns true before accessing, '
'otherwise a runtime exception will be raised.')
doc += warning_str.format(fmt_camel_upper(field.name))
self.emit_wrapped_text(
self.process_doc(doc, self._docf), prefix=comment_prefix)
self.emit(fmt_property(field=field))
self.emit()
namespace = union_type.namespace
union_type_name = fmt_type_name(union_type, namespace)
variant_type_names = []
if parent_type:
variant_type_names.append(fmt_type_name(parent_type, namespace))
for variant in union_type.fields:
if variant.doc:
self._emit_tsdoc_header(variant.doc)
variant_name = '%s%s' % (union_type_name, fmt_pascal(variant.name))
variant_type_names.append(variant_name)
self.emit('export interface %s {' % variant_name)
with self.indent(dent=indent_spaces):
# Since field contains non-alphanumeric character, we need to enclose
# it in quotation marks.
self.emit("'.tag': '%s';" % variant.name)
if is_void_type(variant.data_type) is False:
self.emit("%s: %s;" % (fmt_name_case(variant.name, self.use_camel_case),
fmt_type(variant.data_type, namespace)))
self.emit('}')
self.emit()
if union_type.doc:
self._emit_tsdoc_header(union_type.doc)
self.emit('export type %s = %s;' % (union_type_name, ' | '.join(variant_type_names)))
self.emit()
def _generate_synthesize_ivars(self, union):
non_void_exists = False
for field in union.all_fields:
if not is_void_type(field.data_type):
non_void_exists = True
self.emit('@synthesize {} = _{};'.format(
fmt_var(field.name), fmt_var(field.name)))
if non_void_exists:
self.emit()
def _format_tag_type(self, namespace, data_type): # pylint: disable=unused-argument
if is_void_type(data_type):
return ''
else:
return '({})'.format(fmt_type(data_type))
def _generate_route_signature(self, namespace, route):
req = fmt_type(route.arg_data_type, namespace)
res = fmt_type(route.result_data_type, namespace, use_interface=True)
fn = fmt_var(route.name)
style = route.attrs.get('style', 'rpc')
arg = '' if is_void_type(route.arg_data_type) else 'arg {req}'
ret = '(err error)' if is_void_type(route.result_data_type) else \
'(res {res}, err error)'
signature = '{fn}(' + arg + ') ' + ret
if style == 'download':
signature = '{fn}(' + arg + \
') (res {res}, content io.ReadCloser, err error)'
elif style == 'upload':
signature = '{fn}(' + arg + ', content io.Reader) ' + ret
if is_void_type(route.arg_data_type):
signature = '{fn}(content io.Reader) ' + ret
return signature.format(fn=fn, req=req, res=res)
route_path = '{}_v{}'.format(route.name, route.version)
if route.deprecated:
deprecated = '@{}'.format('YES')
else:
deprecated = '@{}'.format('NO')
if not is_void_type(route.result_data_type):
caller = fmt_class_type(
route.result_data_type, suppress_ptr=True)
result_type = fmt_func_call(
caller=caller, callee='class')
else:
result_type = 'nil'
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(