Skip to content

Commit

Permalink
Merge pull request #664 from SahilChoudhary22/feature/remove-semaphor…
Browse files Browse the repository at this point in the history
…e-from-swift-snippet

Removed semaphore usage from swift snippet, minor workflow fix, swift options cleanup
  • Loading branch information
SahilChoudhary22 authored Feb 3, 2023
2 parents 8d76066 + 7312a4a commit 3b85e31
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/integration.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Test

on: push
on: [push, pull_request]

jobs:
Unit-Tests:
Expand Down
28 changes: 15 additions & 13 deletions codegens/swift/lib/swift.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,11 @@ self = module.exports = {
description: 'Remove white space and additional lines that may affect the server\'s response'
},
{
name: 'Follow redirects',
id: 'followRedirect',
name: 'Include boilerplate',
id: 'includeBoilerplate',
type: 'boolean',
default: true,
description: 'Automatically follow HTTP redirects'
default: false,
description: 'Include class definition and import statements in snippet'
}
];
},
Expand Down Expand Up @@ -286,15 +286,15 @@ self = module.exports = {
throw new Error('Swift-Converter: callback is not valid function');
}
options = sanitizeOptions(options, self.getOptions());
var codeSnippet, indent, trim, timeout, finalUrl, // followRedirect,
var indent, trim, timeout, finalUrl,
codeSnippet = '',
bodySnippet = '',
headerSnippet = '',
requestBody;

indent = options.indentType === 'Tab' ? '\t' : ' ';
indent = indent.repeat(options.indentCount);
timeout = options.requestTimeout;
// followRedirect = options.followRedirect;
trim = options.trimRequestBody;
finalUrl = getUrlStringfromUrlObject(request.url);

Expand Down Expand Up @@ -342,9 +342,10 @@ self = module.exports = {
requestBody = (request.body ? request.body.toJSON() : {});
bodySnippet = parseBody(requestBody, trim, indent);

codeSnippet = 'import Foundation\n';
codeSnippet += '#if canImport(FoundationNetworking)\nimport FoundationNetworking\n#endif\n\n';
codeSnippet += 'var semaphore = DispatchSemaphore (value: 0)\n\n';
if (options.includeBoilerplate) {
codeSnippet += 'import Foundation\n';
codeSnippet += '#if canImport(FoundationNetworking)\nimport FoundationNetworking\n#endif\n\n';
}
if (bodySnippet !== '') {
codeSnippet += `${bodySnippet}\n\n`;
}
Expand Down Expand Up @@ -375,13 +376,14 @@ self = module.exports = {
codeSnippet += '\nlet task = URLSession.shared.dataTask(with: request) { data, response, error in \n';
codeSnippet += `${indent}guard let data = data else {\n`;
codeSnippet += `${indent.repeat(2)}print(String(describing: error))\n`;
codeSnippet += `${indent.repeat(2)}semaphore.signal()\n`;
codeSnippet += `${indent.repeat(2)}return\n`;
codeSnippet += `${indent.repeat(2)}`;
codeSnippet += options.includeBoilerplate ? 'exit(EXIT_SUCCESS)\n' : 'return\n';
codeSnippet += `${indent}}\n`;
codeSnippet += `${indent}print(String(data: data, encoding: .utf8)!)\n`;
codeSnippet += `${indent}semaphore.signal()\n}\n\n`;
codeSnippet += options.includeBoilerplate ? `${indent}exit(EXIT_SUCCESS)\n` : '';
codeSnippet += '}\n\n';
codeSnippet += 'task.resume()\n';
codeSnippet += 'semaphore.wait()\n';
codeSnippet += options.includeBoilerplate ? 'dispatchMain()\n' : '';

return callback(null, codeSnippet);
}
Expand Down
1 change: 1 addition & 0 deletions codegens/swift/test/newman/newman.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var runNewmanTest = require('../../../../test/codegen/newman/newmanTestUtil').ru

describe('Convert for different types of request', function () {
var options = {
includeBoilerplate: true,
indentType: 'Space',
indentCount: 4
},
Expand Down
1 change: 1 addition & 0 deletions codegens/swift/test/unit/convert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ describe('Swift Converter', function () {
expect(getOptions()[1]).to.have.property('id', 'indentType');
expect(getOptions()[2]).to.have.property('id', 'requestTimeout');
expect(getOptions()[3]).to.have.property('id', 'trimRequestBody');
expect(getOptions()[4]).to.have.property('id', 'includeBoilerplate');
});
});

Expand Down

0 comments on commit 3b85e31

Please sign in to comment.