Skip to content

Commit

Permalink
feat: add convert function from 2.0.0 to 2.1.0 (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu authored Jun 29, 2021
1 parent 32d6d11 commit 24457c4
Show file tree
Hide file tree
Showing 5 changed files with 338 additions and 45 deletions.
2 changes: 1 addition & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ if (!asyncapiFile) {
program.help(); // This exits the process
}
if (!version) {
version = '2.0.0';
version = '2.1.0';
}

try {
Expand Down
98 changes: 54 additions & 44 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,63 @@ const {

const lib = module.exports;

/**
* Value for key (version) represents the function which converts specification from previous version to the given as key.
*/
const conversions = {
'1.0.0-to-2.0.0-rc1': from1to2rc1,
'1.1.0-to-2.0.0-rc1': from1to2rc1,
'1.2.0-to-2.0.0-rc1': from1to2rc1,
'2.0.0-rc1-to-2.0.0-rc2': from2rc1to2rc2,
'1.0.0-to-2.0.0-rc2': from1to2rc2,
'1.1.0-to-2.0.0-rc2': from1to2rc2,
'1.2.0-to-2.0.0-rc2': from1to2rc2,
'1.0.0-to-2.0.0': from1to2,
'1.1.0-to-2.0.0': from1to2,
'1.2.0-to-2.0.0': from1to2,
'2.0.0-rc1-to-2.0.0': from2rc1to2,
'2.0.0-rc2-to-2.0.0': from2rc2to2,

};
'1.0.0': undefined,
'1.1.0': from__1_0_0__to__1_1_0,
'1.2.0': from__1_1_0__to__1_2_0,
'2.0.0-rc1': from__1_2_0__to__2_0_0_rc1,
'2.0.0-rc2': from__2_0_0_rc1__to__2_0_0_rc2,
'2.0.0': from__2_0_0_rc2__to__2_0_0,
'2.1.0': from__2_0_0__to__2_1_0,
}
const conversionVersions = Object.keys(conversions);

lib.convert = (asyncapi, version, options = {}) => {
const { isYAML, parsed } = serialize(asyncapi);
if (parsed === undefined) return '';

const conversion = `${parsed.asyncapi}-to-${version}`;
let fromVersion = conversionVersions.indexOf(parsed.asyncapi);
const toVersion = conversionVersions.indexOf(version);

if (fromVersion === -1 || toVersion === -1) {
console.error(`Cannot convert from ${parsed.asyncapi} to ${version}.`);
return;
}
if (fromVersion > toVersion) {
console.error(`Cannot downgrade from ${parsed.asyncapi} to ${version}.`);
return;
}
if (fromVersion === toVersion) {
console.error(`Cannot convert to the same version.`);
return;
}

// add 1 to `fromVersion` because we convert from previous to next
fromVersion++;
let converted = parsed;
for (let i = fromVersion; i <= toVersion; i++) {
const fn = conversions[conversionVersions[i]];
converted = fn(converted, options);
}

if (conversions[conversion]) {
const result = conversions[conversion](parsed, options);
let converted = result;
if (isYAML) converted = yaml.safeDump(result, { skipInvalid: true });
return converted;
} else {
console.error(`Can't convert from ${parsed.asyncapi} to ${version}.`);
if (isYAML) {
converted = yaml.safeDump(converted, { skipInvalid: true });
}
return converted;
};

function from1to2rc1 (asyncapi1, options) {
function from__1_0_0__to__1_1_0(asyncapi) {
return asyncapi;
}

function from__1_1_0__to__1_2_0(asyncapi) {
return asyncapi;
}

function from__1_2_0__to__2_0_0_rc1(asyncapi1, options) { // NOSONAR
const result = asyncapi1;

result.asyncapi = '2.0.0-rc1';
Expand Down Expand Up @@ -99,7 +123,7 @@ function from1to2rc1 (asyncapi1, options) {
return result;
}

function from2rc1to2rc2 (asyncapi2rc1, options) {
function from__2_0_0_rc1__to__2_0_0_rc2(asyncapi2rc1, options) { // NOSONAR
const result = asyncapi2rc1;

result.asyncapi = '2.0.0-rc2';
Expand Down Expand Up @@ -155,33 +179,19 @@ function from2rc1to2rc2 (asyncapi2rc1, options) {
});
}

if (!options.id) delete result.id;
return result;
}

function from1to2rc2 (asyncapi1, options) {
let res = from1to2rc1(asyncapi1, options);
if (!options.id) delete res.id;
return from2rc1to2rc2(res, options);
}

// Release 2.0.0 mappings

function from1to2 (asyncapi1, options) {
return from2rc2to2(from1to2rc2(asyncapi1, options), options);
}

function from2rc1to2 (asyncapi2rc1, options) {
let result = from2rc1to2rc2(asyncapi2rc1, options);
function from__2_0_0_rc2__to__2_0_0(asyncapi2rc2, options) {
const result = asyncapi2rc2;
if (!options.id) delete result.id;
result.asyncapi = '2.0.0';

return result;
}

// version 2.0.0-rc2 has the same schema as 2.0.0 release
function from2rc2to2 (asyncapi2rc2, options) {
const result = asyncapi2rc2;
result.asyncapi = '2.0.0';

function from__2_0_0__to__2_1_0(asyncapi2) {
const result = asyncapi2;
result.asyncapi = '2.1.0';
return result;
}
57 changes: 57 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ const path = require("path");
const { convert } = require('../lib');

describe('#convert', () => {
it('should not convert to lowest version', () => {
const result = convert(`asyncapi: '2.1.0'`, '2.0.0');
assert.strictEqual(result, undefined);
});

it('should not convert from non existing version', () => {
const result = convert(`asyncapi: '2.0.0-rc3'`, '2.1.0');
assert.strictEqual(result, undefined);
});

it('should not convert to this same version', () => {
const result = convert(`asyncapi: '2.1.0'`, '2.1.0');
assert.strictEqual(result, undefined);
});

it('should convert from 1.0.0 to 2.0.0-rc1', () => {
const input = fs.readFileSync(path.resolve(__dirname, 'input', '1.0.0', 'streetlights.yml'), 'utf8');
const output = fs.readFileSync(path.resolve(__dirname, 'output', '2.0.0-rc1', 'streetlights.yml'), 'utf8');
Expand Down Expand Up @@ -108,6 +123,48 @@ describe('#convert', () => {
const result = convert(input, '2.0.0');
assertResults(output, result);
});

it('should convert from 1.0.0 to 2.1.0', () => {
const input = fs.readFileSync(path.resolve(__dirname, 'input', '1.0.0', 'streetlights.yml'), 'utf8');
const output = fs.readFileSync(path.resolve(__dirname, 'output', '2.1.0', 'streetlights.yml'), 'utf8');
const result = convert(input, '2.1.0');
assertResults(output, result);
});

it('should convert from 1.1.0 to 2.1.0', () => {
const input = fs.readFileSync(path.resolve(__dirname, 'input', '1.1.0', 'streetlights.yml'), 'utf8');
const output = fs.readFileSync(path.resolve(__dirname, 'output', '2.1.0', 'streetlights.yml'), 'utf8');
const result = convert(input, '2.1.0');
assertResults(output, result);
});

it('should convert from 1.2.0 to 2.1.0', () => {
const input = fs.readFileSync(path.resolve(__dirname, 'input', '1.2.0', 'streetlights.yml'), 'utf8');
const output = fs.readFileSync(path.resolve(__dirname, 'output', '2.1.0', 'streetlights.yml'), 'utf8');
const result = convert(input, '2.1.0');
assertResults(output, result);
});

it('should convert from 2.0.0-rc1 to 2.1.0', () => {
const input = fs.readFileSync(path.resolve(__dirname, 'input', '2.0.0-rc1', 'streetlights.yml'), 'utf8');
const output = fs.readFileSync(path.resolve(__dirname, 'output', '2.1.0', 'streetlights.yml'), 'utf8');
const result = convert(input, '2.1.0');
assertResults(output, result);
});

it('should convert from 2.0.0-rc2 to 2.1.0', () => {
const input = fs.readFileSync(path.resolve(__dirname, 'input', '2.0.0-rc2', 'streetlights.yml'), 'utf8');
const output = fs.readFileSync(path.resolve(__dirname, 'output', '2.1.0', 'streetlights.yml'), 'utf8');
const result = convert(input, '2.1.0');
assertResults(output, result);
});

it('should convert from 2.0.0 to 2.1.0', () => {
const input = fs.readFileSync(path.resolve(__dirname, 'input', '2.0.0', 'streetlights.yml'), 'utf8');
const output = fs.readFileSync(path.resolve(__dirname, 'output', '2.1.0', 'streetlights.yml'), 'utf8');
const result = convert(input, '2.1.0');
assertResults(output, result);
});
});

/*
Expand Down
113 changes: 113 additions & 0 deletions test/input/2.0.0/streetlights.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
asyncapi: 2.0.0
info:
title: Streetlights API
version: 1.0.0
description: "The Smartylighting Streetlights API allows you to remotely manage the city lights.\n\n### Check out its awesome features:\n\n* Turn a specific streetlight on/off \U0001F303\n* Dim a specific streetlight \U0001F60E\n* Receive real-time information about environmental lighting conditions \U0001F4C8\n"
license:
name: Apache 2.0
url: 'https://www.apache.org/licenses/LICENSE-2.0'
servers:
default:
url: 'api.streetlights.smartylighting.com:{port}'
description: Test broker
variables:
port:
description: Secure connection (TLS) is available through port 8883.
default: '1883'
enum:
- '1883'
- '8883'
protocol: mqtt
security:
- apiKey: []
components:
messages:
lightMeasured:
summary: >-
Inform about environmental lighting conditions for a particular
streetlight.
payload:
$ref: '#/components/schemas/lightMeasuredPayload'
turnOnOff:
summary: Command a particular streetlight to turn the lights on or off.
payload:
$ref: '#/components/schemas/turnOnOffPayload'
dimLight:
summary: Command a particular streetlight to dim the lights.
payload:
$ref: '#/components/schemas/dimLightPayload'
schemas:
lightMeasuredPayload:
type: object
properties:
lumens:
type: integer
minimum: 0
description: Light intensity measured in lumens.
sentAt:
$ref: '#/components/schemas/sentAt'
turnOnOffPayload:
type: object
properties:
command:
type: string
enum:
- 'on'
- 'off'
description: Whether to turn on or off the light.
sentAt:
$ref: '#/components/schemas/sentAt'
dimLightPayload:
type: object
properties:
percentage:
type: integer
description: Percentage to which the light should be dimmed to.
minimum: 0
maximum: 100
sentAt:
$ref: '#/components/schemas/sentAt'
sentAt:
type: string
format: date-time
description: Date and time when the message was sent.
securitySchemes:
apiKey:
type: apiKey
in: user
description: Provide your API key as the user and leave the password empty.
parameters:
streetlightId:
name: streetlightId
description: The ID of the streetlight.
schema:
type: string
channels:
'smartylighting/streetlights/1/0/event/{streetlightId}/lighting/measured':
parameters:
streetlightId:
$ref: '#/components/parameters/streetlightId'
publish:
message:
$ref: '#/components/messages/lightMeasured'
'smartylighting/streetlights/1/0/action/{streetlightId}/turn/on':
parameters:
streetlightId:
$ref: '#/components/parameters/streetlightId'
subscribe:
message:
$ref: '#/components/messages/turnOnOff'
'smartylighting/streetlights/1/0/action/{streetlightId}/turn/off':
parameters:
streetlightId:
$ref: '#/components/parameters/streetlightId'
subscribe:
message:
$ref: '#/components/messages/turnOnOff'
'smartylighting/streetlights/1/0/action/{streetlightId}/dim':
parameters:
streetlightId:
$ref: '#/components/parameters/streetlightId'
subscribe:
message:
$ref: '#/components/messages/dimLight'
Loading

0 comments on commit 24457c4

Please sign in to comment.