gpt4 book ai didi

javascript - 迭代 Swagger YAML 文件以动态生成属性列表

转载 作者:行者123 更新时间:2023-12-02 23:28:19 25 4
gpt4 key购买 nike

我正在尝试创建一个可以接受 swagger yml 文件(如示例 petstore.yaml)的脚本,并从该文件生成已使用的“属性”列表。

这涉及到解析 yaml,然后迭代 json 对象中的所有元素以返回所需的数据。我打算遍历所有路径来识别有效响应,但现在我只想过滤掉 yaml 文件中指定的所有定义,然后对于每个 def,输出属性列表。

可以下载示例 yaml 文件 here

最终,我想为每个属性生成一个字符串,显示类似

<filename>~Pet~id~integer~int64
<filename>~Pet~name~string~
<filename>~Pet~tag~string~

为此,我需要找到“定义”节点,并迭代所有子节点以读取信息。

我正在努力为 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);

最佳答案

我的时间不多了,所以我会把它留在这里。虽然不是那么精致,但希望对您有所帮助。

所以我做了一个递归函数来迭代对象。该函数接受一个对象和一个前缀。该前缀将用于打印详细信息。排除前缀用于不显示某些字段名称,排除类型用于不打印某些类型。循环遍历对象的字段,捕获格式、类型、描述以及您想要捕获的任何内容。循环完成对象的字段后,检查类型字段是否已填充。如果是,则记录参数详细信息。

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);

关于javascript - 迭代 Swagger YAML 文件以动态生成属性列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56625791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com