Created
July 7, 2021 13:46
-
-
Save xmayeur/c45c8b08aedb8ebe2954dde71142279e to your computer and use it in GitHub Desktop.
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 script parses the JSONschema from data-object concept with a property 'JSONschema' | |
* JSON schema is read from file and stored in the JSONschema property | |
* It replaces the data-object title, documentation by the schema title and description | |
* It adds the schema properties to the data object | |
* It finally format the object to display it with its attributes | |
* | |
* Source: https://www.eaprincipals.com/content/data-modeling-archi-part-2-adding-entity-attributes | |
*/ | |
console.show(); | |
console.clear(); | |
var schema = {}; | |
$(selection).filter("data-object").each(function (o) { | |
var filePath = window.promptOpenFile({ | |
title: "Open JSON Schema for <<" + o.name + ">>", | |
filterExtensions: ["*.json"] | |
}); | |
var schemaTxt = ""; | |
if (filePath) { | |
var FileReader = Java.type("java.io.FileReader"); | |
var theSchemaFile = new FileReader(filePath); | |
data = theSchemaFile.read(); | |
console.log("Loading " + filePath + '...'); | |
while (data != -1) { | |
var c = String.fromCharCode(data); | |
schemaTxt += c; | |
data = theSchemaFile.read(); | |
} | |
theSchemaFile.close(); | |
o.prop('JSONschema', JSON.stringify(JSON.parse(schemaTxt))); | |
} | |
console.log("Processing schema...") | |
try { | |
schema = JSON.parse(o.prop('JSONschema')); | |
} catch (e) { | |
console.log('No such properties "JSONschema" or invalid schema.'); | |
} | |
if (!!schema) { | |
const title = schema.title; | |
const description = String(schema.description); | |
let attributes = ""; | |
let len = 0; | |
o.name = title; | |
o.labelVisible = true; | |
o.documentation = description + '\n\n'; | |
o.prop('objectDescription', description) | |
var len2 = description.length | |
if ('properties' in schema) { | |
o.documentation += '| attribute | description | type | object |\n'; | |
o.documentation += '|---|---|---|---|\n'; | |
for (let p in schema.properties) { | |
len = Math.max(len, String(p).length); | |
} | |
for (let p in schema.properties) { | |
let props = schema.properties[p]; | |
let desc = props.description; | |
delete props.description; | |
delete props.$id; | |
o.documentation += '| ' + String(p) + ' | ' + desc + ' | ' + props.type + ' | '; | |
if (Object.keys(props).length) { | |
desc += ' ' + JSON.stringify(props); | |
o.documentation += JSON.stringify(props); | |
} | |
o.prop(p, desc); | |
let att = '+' + p + ":" + ' '.repeat(1 + len - String(p).length) + props.type; | |
if (props.type === 'array') { | |
let typ = props.items.type; | |
att += ' of ' + typ; | |
} | |
len2 = Math.max(len2, att.length) | |
attributes += att + '\n'; | |
o.documentation += ' |\n'; | |
} | |
} else if ('enum' in schema){ | |
schema.enum.forEach(function(p) { | |
len = Math.max(len, String(p).length); | |
}); | |
schema.enum.forEach(function(p) { | |
o.prop(p, ''); | |
let att = '-' + p; | |
len2 = Math.max(len2, att.length) | |
attributes += att + '\n'; | |
}); | |
} | |
o.prop('attributes', attributes); | |
len2 += 2; | |
o.labelExpression = " ".repeat((len2 - o.name.length) / 2) + "${name}\n" + | |
".".repeat(len2) + | |
"\n" + | |
" ".repeat((len2 - description.length) / 2) + "${property:objectDescription}\n" + | |
"_".repeat(len2) + | |
"\n\n" + | |
"${property:attributes}"; | |
o.fontName = "Courier New"; | |
o.fontSize = 9; | |
o.textAlignment = TEXT_ALIGNMENT.LEFT; | |
o.bounds = {width: (len2 - 2) * 8.5, height: (o.labelValue.split('\n').length + 2) * 14} | |
console.log("Done.") | |
} else { | |
console.log("No schema to process.") | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment