[英]Iterating over Swagger YAML File to dynamically generate a list of properties
我正在嘗試創建一個腳本,該腳本可以接受一個龐大的yml文件(例如示例petstore.yaml),並從該文件生成使用的“屬性”列表。
這涉及到yaml的解析,然后遍歷json對象中的所有元素以返回所需的數據。 我打算遍歷所有路徑以標識有效的響應,但是現在我只想過濾掉yaml文件中指定的所有定義,然后為每個def輸出屬性列表。
可以在此處下載示例yaml文件
在一天結束時,我想為每個屬性生成一個字符串,該字符串顯示如下內容
<filename>~Pet~id~integer~int64
<filename>~Pet~name~string~
<filename>~Pet~tag~string~
為此,我需要找到“ definitions”節點,並遍歷所有子節點以讀取信息。
我正在努力為yaml樣式的文件弄清楚邏輯。.下面是我的工作代碼。
感覺就像是我使迭代循環過於復雜(也許更好的解決方案是使用正則表達式?
的index.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<title>Read YAML</title>
</script><script src='https://cdnjs.cloudflare.com/ajax/libs/js-yaml/3.13.1/js-yaml.min.js'>
</script>
<body>
<input type="file" id="file-input" />
<h3>Contents of the file:</h3>
<pre id="file-content"></pre>
<div id='output'></div>
</body>
<script>
var yaml = window.jsyaml;
</script>
<script src="app.js"></script>
</html>
我的javascript文件
var body = '';
var mystring = ''
function readSingleFile(e) {
var file = e.target.files[0];
if (!file) {
return;
}
var reader = new FileReader();
reader.onload = function(e) {
var contents = e.target.result;
displayContents(contents);
};
reader.readAsText(file);
}
function displayContents(contents) {
var element = document.getElementById('file-content');
var doc = yaml.load(contents);
// Process the file.
// Process only the definitions section of the file
var definition = doc.definitions
console.log (definition) ;
for(var def in definition) {
if(definition.hasOwnProperty(def)) {
var node = definition[def]
if (typeof(node) === 'object') {
// loop through the definitions
processContents(node)
}
}
}
function processContents(obj) {
for (var item in obj) {
var definitions = obj[item]
if (typeof definitions === 'object'){
for (var attribute in definitions) {
// HELP -- if there is an attribute called "properties" then iterate over all properties and create the concatenated string
// Broken from here !!!!
if (attribute.properties) {
for (var param in attribute.properties) {
mystring = param.name + '~' + param.type + '~' + param.description + '~' + param.format
}
}
}
}
}
}
document.getElementById('output').innerHTML = body;
}
document.getElementById('file-input')
.addEventListener('change', readSingleFile, false);
我沒時間了,所以我把它留在這里。 它雖然不夠精致,但希望對您有所幫助。
所以我做了一個遞歸函數來遍歷對象。 該函數接受一個對象和一個前綴。 該前綴將用於打印細節。 排除前綴用於不顯示某些字段名稱,排除類型用於不打印某些類型。 在對象的字段中循環顯示捕獲格式,類型,描述以及您要捕獲的任何內容。 完成循環后,檢查對象的字段是否填充了type字段。 如果是,則記錄參數詳細信息。
var body = '';
var mystring = ''
function readSingleFile(e) {
var file = e.target.files[0];
if (!file) {
return;
}
var reader = new FileReader();
reader.onload = function(e) {
var contents = e.target.result;
displayContents(contents);
};
reader.readAsText(file);
}
function displayContents(contents) {
console.log('---contents---')
console.log(contents)
var element = document.getElementById('file-content');
console.log('---element----')
console.log(element)
var doc = yaml.load(contents);
console.log('---doc')
console.log(doc)
// Process the file.
// Process only the definitions section of the file
var definition = doc.definitions
console.log('---definition---')
console.log (definition) ;
processContents2(doc.definitions)
function processContents2(obj, prefix) {
const excludePrefixes = ['properties']
const excludeTypes = ['object', 'array']
let format = ''
let type = ''
let description = ''
let count = 0;
for (var field in obj) {
if (typeof obj[field] === 'object') {
processContents2(obj[field], (prefix || '') + (excludePrefixes.indexOf(field) === -1 ? '~' + field : ''))
} else {
if (field === 'format') {
format = obj[field]
} else if (field === 'type') {
type = obj[field]
} else if (field === 'description') {
description = obj[field]
}
}
}
if (type && (excludeTypes.indexOf(type) === -1)) {
console.log((prefix || '') + '~' + type + (description ? '~' + description : '') + (format ? '~' + format : ''))
}
}
document.getElementById('output').innerHTML = body;
}
document.getElementById('file-input')
.addEventListener('change', readSingleFile, false);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.