def generate_response_single_param_init_and_member_var( self, response_context): single_param = response_context.structBody().singleParameter() self.write_line('let body: ' + swift_type_name(single_param.paramType()) + '?') self.write_line('let rawResponse: HTTPResponse') self.write_line( 'init(content: ResponseContent?, rawResponse: HTTPResponse) throws {' ) self.push_indent() self.write_line('self.rawResponse = rawResponse') self.write_line('guard let content = content else {') self.push_indent() self.write_line('self.body = nil') self.write_line('return') self.pop_indent() self.write_line('}') param_type = single_param.paramType() generic_type = param_type.genericType() if generic_type is not None: array_type = generic_type.arrayGenericParam() dict_type = generic_type.dictGenericParam() if array_type is not None: self.generate_array_from_resp_assignment(array_type, 'body') elif dict_type is not None: self.generate_dict_from_resp_assignment(dict_type, 'body') self.write_line('self.body = body') else: self.write_line('self.body = ' + swift_type_name(param_type) + '(content: content)') self.pop_indent() self.write_line('}')
def generate_response_param_map_init_and_member_var( self, response_context): param_maps = response_context.structBody().parameterMap() for param_map in param_maps: param_type = param_map.paramType() self.write_line('let ' + param_map.key().getText() + ': ' + swift_type_name(param_type) + '?') self.write_line('let rawResponse: HTTPResponse') self.write_line( 'init(content: ResponseContent?, rawResponse: HTTPResponse) throws {' ) self.push_indent() self.write_line('self.rawResponse = rawResponse') self.write_line( 'guard let content = content, case .dictionary(let value) = content else {' ) self.push_indent() for param_map in param_maps: self.write_line('self.' + param_map.key().getText() + ' = nil') self.write_line('return') self.pop_indent() self.write_line('}') for param_map in param_maps: param_type = param_map.paramType() generic_type = param_type.genericType() param_value_name = self.string_from_string_context( param_map.value().string()) if param_map.value( ) is not None else param_map.key().getText() if generic_type is not None: array_type = generic_type.arrayGenericParam() dict_type = generic_type.dictGenericParam() self.write_line('if let content = value["' + param_value_name + '"] {') self.push_indent() if array_type is not None: self.generate_array_from_resp_assignment( array_type, param_map.key().getText()) elif dict_type is not None: self.generate_dict_from_resp_assignment( dict_type, param_map.key().getText()) self.write_line('self.' + param_map.key().getText() + ' = ' + param_map.key().getText()) self.pop_indent() self.write_line('} else {') self.push_indent() self.write_line('self.' + param_map.key().getText() + ' = nil') self.pop_indent() self.write_line('}') else: self.write_line('self.' + param_map.key().getText() + ' = ' + swift_type_name(param_type) + '(content: value["' + param_value_name + '"])') self.pop_indent() self.write_line('}')
def generate_struct_member_var(self, struct_context): self.write_blank_lines(1) single_param = struct_context.structBody().singleParameter() param_maps = struct_context.structBody().parameterMap() if single_param is not None: self.write_line('var body: ' + swift_type_name(single_param.paramType()) + '?') else: for param_map in param_maps: param_type = param_map.paramType() self.write_line('var ' + param_map.key().getText() + ': ' + swift_type_name(param_type) + '?')
def generate_array_from_resp_assignment(self, array_context, container_name): array_element_type = array_context.paramType() nested_type = array_element_type.genericType() type_name = '[' + swift_type_name(array_element_type) + ']' if nested_type is not None: self.write_line('var ' + container_name + ': ' + type_name + '? = nil') self.write_line('if case .array(let value) = content {') self.push_indent() self.write_line(container_name + ' = ' + type_name + '()') self.write_line('value.forEach { (content) in') self.push_indent() array_type = nested_type.arrayGenericParam() dict_type = nested_type.dictGenericParam() if array_type is not None: self.generate_array_from_resp_assignment( array_type, '_' + container_name) else: self.generate_dict_from_resp_assignment( dict_type, '_' + container_name) self.write_line('if let tmp = ' + '_' + container_name + ' {') self.push_indent() self.write_line(container_name + '!.append(tmp)') self.pop_indent() self.write_line('}') self.pop_indent() self.write_line('}') self.pop_indent() self.write_line('}') else: self.write_line('let ' + container_name + ' = ' + type_name + '(content: content)')
def generate_struct_single_param_response_content_impl( self, struct_context): single_param = struct_context.structBody().singleParameter() self.write_line('extension %s: ResponseContentConvertible {' % struct_context.structName().getText()) self.push_indent() self.write_blank_lines(1) self.write_line('init?(content: ResponseContent?) {') self.push_indent() self.write_line('guard let content = content else {') self.push_indent() self.write_line('return nil') self.pop_indent() self.write_line('}') param_type = single_param.paramType() generic_type = param_type.genericType() if generic_type is not None: array_type = generic_type.arrayGenericParam() dict_type = generic_type.dictGenericParam() if array_type is not None: self.generate_array_from_resp_assignment(array_type, 'body') elif dict_type is not None: self.generate_dict_from_resp_assignment(dict_type, 'body') self.write_line('self.body = body') else: self.write_line('self.body = ' + swift_type_name(param_type) + '(content: content)') self.pop_indent() self.write_line('}') self.pop_indent() self.write_line('}')
def generate_struct_param_map_response_content_impl(self, struct_context): param_maps = struct_context.structBody().parameterMap() self.write_line('extension %s: ResponseContentConvertible {' % struct_context.structName().getText()) self.push_indent() self.write_blank_lines(1) self.write_line('init?(content: ResponseContent?) {') self.push_indent() self.write_line( 'guard let content = content, case .dictionary(let value) = content else {' ) self.push_indent() self.write_line('return nil') self.pop_indent() self.write_line('}') for param_map in param_maps: param_type = param_map.paramType() generic_type = param_type.genericType() param_value = param_map.value() param_value_name = self.string_from_string_context( param_value.string( )) if param_value is not None else param_map.key().getText() if generic_type is not None: array_type = generic_type.arrayGenericParam() dict_type = generic_type.dictGenericParam() self.write_line('if let content = value["' + param_value_name + '"] {') self.push_indent() if array_type is not None: self.generate_array_from_resp_assignment( array_type, param_map.key().getText()) elif dict_type is not None: self.generate_dict_from_resp_assignment( dict_type, param_map.key().getText()) self.write_line('self.' + param_map.key().getText() + ' = ' + param_map.key().getText()) self.pop_indent() self.write_line('} else {') self.push_indent() self.write_line('self.' + param_map.key().getText() + ' = nil') self.pop_indent() self.write_line('}') else: self.write_line('self.' + param_map.key().getText() + ' = ' + swift_type_name(param_type) + '(content: value["' + param_value_name + '"])') self.pop_indent() self.write_line('}') self.pop_indent() self.write_line('}')
def generate_dict_from_resp_assignment(self, dict_context, container_name): value_type = dict_context.paramType() key_type = dict_context.baseType() nested_value_type = value_type.genericType() type_name = '[' + swift_base_type_name_from_idl_base_type( key_type.getText()) + ': ' + swift_type_name(value_type) + ']' if nested_value_type is not None: self.write_line('var ' + container_name + ': ' + type_name + '? = nil') self.write_line('if case .dictionary(let value) = content {') self.push_indent() self.write_line(container_name + ' = ' + type_name + '()') self.write_line('value.forEach { (kv) in') self.push_indent() self.write_line('let content = kv.value') array_type = nested_value_type.arrayGenericParam() dict_type = nested_value_type.dictGenericParam() if array_type is not None: self.generate_array_from_resp_assignment( array_type, '_' + container_name) else: self.generate_dict_from_resp_assignment( dict_type, '_' + container_name) self.write_line('if let tmp = ' + '_' + container_name + ' {') self.push_indent() self.write_line(container_name + '!.updateValue(tmp, forKey: kv.key)') self.pop_indent() self.write_line('}') self.pop_indent() self.write_line('}') self.pop_indent() self.write_line('}') else: self.write_line('let ' + container_name + ' = ' + type_name + '(content: content)')
def generate_request_init_and_default_member_var(self, request_context, uri_context): self.write_blank_lines(1) def filter_param_in_uri(uri_path_component): return uri_path_component.parameterInUri() is not None params_in_uri = filter(filter_param_in_uri, uri_context.uriPathComponent()) for param_in_uri in params_in_uri: self.write_line( 'let ' + param_in_uri.parameterInUri().identifier().getText() + ': String') self.write_line('var method: String = "' + request_context.method().getText() + '"') self.write_line('private var _configuration: RequestConfiguration?') self.write_line('var configuration: RequestConfiguration {') self.push_indent() self.write_line('get {') self.push_indent() self.write_line('guard let config = _configuration else {') self.push_indent() self.write_line( 'return BaseRequestConfiguration.create(from: manager.configuration, request: self)' ) self.pop_indent() self.write_line('}') self.write_line('return config') self.pop_indent() self.write_line('}') self.write_line('set {') self.push_indent() self.write_line('_configuration = newValue') self.pop_indent() self.write_line('}') self.pop_indent() self.write_line('}') self.write_line( 'var manager: RequestManager = BaseRequestManager.shared') self.write_line('var uri: String {') self.push_indent() self.write_line('return ' + self.request_uri_from_uri(uri_context)) self.pop_indent() self.write_line('}') self.write_blank_lines(1) single_param = request_context.structBody().singleParameter() if single_param is not None: self.write_line('var body: ' + swift_type_name(single_param.paramType()) + '?') else: param_maps = request_context.structBody().parameterMap() for param_map in param_maps: param_type = param_map.paramType() self.write_line('var ' + param_map.key().getText() + ': ' + swift_type_name(param_type) + '?') self.write_blank_lines(1) for param_map in param_maps: param_value_name = self.string_from_string_context( param_map.value().string()) if param_map.value( ) is not None else param_map.key().getText() self.write_line( 'let keyOf' + underline_to_upper_camel_case(param_map.key().getText()) + ' = "' + param_value_name + '"') init_param_list = ', '.join( map( lambda p: p.parameterInUri().identifier().getText() + ': String', params_in_uri)) if len(init_param_list) != 0: self.write_blank_lines(1) self.write_line('init(' + init_param_list + ') {') self.push_indent() for param_in_uri in params_in_uri: self.write_line( 'self.' + param_in_uri.parameterInUri().identifier().getText() + ' = ' + param_in_uri.parameterInUri().identifier().getText()) self.pop_indent() self.write_line('}')