Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find vocabulary elements by type #280

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
coverage
104 changes: 104 additions & 0 deletions lib/bytype.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
const fs = require("fs");

function qualifiedName(voc, type) {
type ||= "Edm.String";
const i = type.lastIndexOf(".");
let ns = type.substring(0, i);
if (voc.$Schema.$Alias === ns) ns = voc.$Namespace;
else {
ref: for (const url in voc.$Reference)
for (const incl of voc.$Reference[url].$Include)
if (incl.$Alias === ns) {
ns = incl.$Namespace;
break ref;
}
}
return [ns, type.substring(i + 1)];
}

function bytype(vocabFolder, types) {
const vocabularies = {};
const output = [];

function search(voc, node, kind, prefix) {
for (const elem in node)
if (node[elem].$Kind === kind) {
const [ns, local] = qualifiedName(voc, node[elem].$Type);
if (
types.some(
({ nsType, localType }) => ns === nsType && local === localType
)
)
output.push(prefix + elem);
else if (node[elem].$Type)
searchtype(voc, node[elem].$Type, prefix + elem + "/");
}
}

function searchtype(voc, type, prefix) {
const [ns, local] = qualifiedName(voc, type);
const v = vocabularies[ns]?.$Schema;
if (v) {
if (v[local].$BaseType)
searchtype(vocabularies[ns], v[local].$BaseType, prefix);
search(vocabularies[ns], v[local], undefined, prefix);
}
}

fs.readdirSync(vocabFolder)
.filter((fn) => fn.endsWith(".json"))
.forEach(function (fn) {
const voc = JSON.parse(fs.readFileSync(vocabFolder + fn));
for (const ns in voc)
if (/^[^$@]/.test(ns)) {
vocabularies[ns] = voc;
vocabularies[ns].$Namespace = ns;
vocabularies[ns].$Schema = voc[ns];
}
});
for (const ns in vocabularies) {
const voc = vocabularies[ns];
search(voc, voc.$Schema, "Term", ns + ".");
for (const elem in voc.$Schema)
if (voc.$Schema[elem].$Kind === "ComplexType")
searchtype(voc, ns + "." + elem, ns + "." + elem + "/");
else if (/^[^$@]/.test(elem) && voc.$Schema[elem] instanceof Array)
for (const op of voc.$Schema[elem]) {
for (const over of op.$Parameter)
search(voc, over, undefined, ns + "." + elem + "/");
const [nsReturn, localReturn] = qualifiedName(
voc,
op.$ReturnType.$Type
);
if (
types.some(
({ nsType, localType }) =>
nsReturn === nsType && localReturn === localType
)
)
output.push(ns + "." + elem + "/$ReturnType");
else if (op.$ReturnType)
searchtype(
voc,
op.$ReturnType.$Type,
ns + "." + elem + "/$ReturnType/"
);
}
}
return output;
}

if (module.parent) module.exports = bytype;
else {
const types = [];
for (let i = 2; i < process.argv.length; i++) {
const pos = process.argv[i].lastIndexOf(".");
types.push({
nsType: process.argv[i].substring(0, pos),
localType: process.argv[i].substring(pos + 1),
});
}
process.stdout.write(
JSON.stringify(bytype(__dirname + "/../vocabularies/", types), undefined, 2)
);
}
Loading