-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#2 convert YAML to JSON and vice-versa
- Loading branch information
Raisel Melian
committed
Jan 24, 2019
1 parent
88a58f6
commit 5e2685b
Showing
10 changed files
with
210 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,3 +34,4 @@ jspm_packages | |
|
||
# Optional REPL history | ||
.node_repl_history | ||
/.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/env node | ||
|
||
'use strict' | ||
|
||
const converter = require('./lib/converter.js'); | ||
const formats = converter.formats; | ||
|
||
const command = async (specFilePath, targetFormat) => { | ||
|
||
if (targetFormat !== formats.YAML || targetFormat !== formats.JSON) { | ||
console.log("throw `Invalid target format: ${targetFormat}`;"); | ||
process.exit(1); | ||
} | ||
|
||
const document = await converter.convert(specFilePath, targetFormat); | ||
console.log(document); | ||
}; | ||
|
||
|
||
module.exports = { command }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict'; | ||
const parser = require('./parser'); | ||
const YAML = require('js-yaml'); | ||
|
||
|
||
const formats = Object.freeze({ | ||
JSON: "json", | ||
YAML: "yaml" | ||
}); | ||
|
||
const convert = async (specFilePath, targetFormat) => { | ||
|
||
let content = await parser.getFileContent(specFilePath); | ||
|
||
content = content.toString('utf8'); | ||
|
||
if (targetFormat === formats.YAML) { | ||
|
||
let jsonObject = JSON.parse(content); | ||
|
||
return YAML.safeDump(jsonObject); | ||
|
||
} else if (targetFormat === formats.JSON) { | ||
|
||
const parsedContent = YAML.safeLoad(content); | ||
|
||
const bundledContent = await parser.bundle(parsedContent); | ||
|
||
const dereferencedContent = await parser.dereference(bundledContent); | ||
|
||
return JSON.stringify(dereferencedContent); | ||
} | ||
|
||
throw `Invalid target format: ${targetFormat}`; | ||
}; | ||
|
||
module.exports = { | ||
convert: convert, | ||
formats: formats | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
'use strict'; | ||
const parse = require('./parser'); | ||
const parser = require('./parser'); | ||
|
||
const validate = async (specFile) => { | ||
|
||
await parse(specFile); | ||
await parser.parse(specFile); | ||
return true; | ||
|
||
}; | ||
|
||
module.exports = {validate}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
#!/usr/bin/env bash | ||
async-api-linter validate /Users/melianr/dev/asyncapi/async-api-linter/test/samples/asyncapi.yml | ||
#async-api-linter validate /Users/melianr/dev/asyncapi/async-api-linter/test/samples/asyncapi.yml | ||
#async-api-linter validate /Users/melianr/dev/asyncapi/async-api-linter/test/samples/asyncapi.json | ||
|
||
async-api-linter convert /Users/melianr/dev/asyncapi/async-api-linter/test/samples/asyncapi.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
{ | ||
"asyncapid": "1.2.0", | ||
"info": { | ||
"title": "Streetlights API", | ||
"version": "1.0.0", | ||
"description": "The Smartylighting Streetlights API allows you\nto remotely manage the city lights.\n", | ||
"license": { | ||
"name": "Apache 2.0", | ||
"url": "https://www.apache.org/licenses/LICENSE-2.0" | ||
} | ||
}, | ||
"baseTopic": "smartylighting.streetlights.1.0", | ||
"servers": [ | ||
{ | ||
"url": "test.mosquitto.org", | ||
"scheme": "mqtt", | ||
"description": "Test broker", | ||
"variables": { | ||
"port": { | ||
"description": "Secure connection (TLS) is available through port 8883.", | ||
"default": "1883", | ||
"enum": [ | ||
"1883", | ||
"8883" | ||
] | ||
} | ||
} | ||
} | ||
], | ||
"topics": { | ||
"event.lighting.measured": { | ||
"x-service-name": "streetlights", | ||
"publish": { | ||
"x-operation-id": "lightMeasuredPublish", | ||
"summary": "Inform about environmental lighting conditions for a particular streetlight.", | ||
"payload": { | ||
"type": "object", | ||
"properties": { | ||
"lumens": { | ||
"type": "integer", | ||
"minimum": 0, | ||
"description": "Light intensity measured in lumens." | ||
}, | ||
"sentAt": { | ||
"type": "string", | ||
"format": "date-time", | ||
"description": "Date and time when the message was sent." | ||
} | ||
} | ||
} | ||
}, | ||
"subscribe": { | ||
"x-operation-id": "lightMeasuredSubscribe", | ||
"summary": "Inform about environmental lighting conditions for a particular streetlight.", | ||
"payload": { | ||
"type": "object", | ||
"properties": { | ||
"lumens": { | ||
"type": "integer", | ||
"minimum": 0, | ||
"description": "Light intensity measured in lumens." | ||
}, | ||
"sentAt": { | ||
"type": "string", | ||
"format": "date-time", | ||
"description": "Date and time when the message was sent." | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"components": { | ||
"messages": { | ||
"lightMeasured": { | ||
"summary": "Inform about environmental lighting conditions for a particular streetlight.", | ||
"payload": { | ||
"type": "object", | ||
"properties": { | ||
"lumens": { | ||
"type": "integer", | ||
"minimum": 0, | ||
"description": "Light intensity measured in lumens." | ||
}, | ||
"sentAt": { | ||
"type": "string", | ||
"format": "date-time", | ||
"description": "Date and time when the message was sent." | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"schemas": { | ||
"lightMeasuredPayload": { | ||
"type": "object", | ||
"properties": { | ||
"lumens": { | ||
"type": "integer", | ||
"minimum": 0, | ||
"description": "Light intensity measured in lumens." | ||
}, | ||
"sentAt": { | ||
"type": "string", | ||
"format": "date-time", | ||
"description": "Date and time when the message was sent." | ||
} | ||
} | ||
}, | ||
"sentAt": { | ||
"type": "string", | ||
"format": "date-time", | ||
"description": "Date and time when the message was sent." | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters