Skip to content

Commit

Permalink
Derived type constraints in Markdown (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
HeikoTheissen authored Oct 10, 2024
1 parent 0caeb69 commit 257c94d
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 19 deletions.
54 changes: 36 additions & 18 deletions lib/csdl2markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,9 @@ module.exports.csdl2markdown = function (filename, csdl, referenced = {}) {
}

if (type.$Kind == "TypeDefinition") {
lines.push("**Type:** " + typeLink({ $Type: type.$UnderlyingType }));
lines.push(
"**Type:** " + typeLink({ ...type, $Type: type.$UnderlyingType }),
);
lines.push("");
}

Expand Down Expand Up @@ -610,13 +612,9 @@ module.exports.csdl2markdown = function (filename, csdl, referenced = {}) {
* @return {string} Text
*/
function applicableTermsList(applicableTerms) {
const text = [];
if (applicableTerms.length > 0) text.push("<br>Applicable Annotation Terms:<ul>");
applicableTerms.forEach((term) => {
text.push(`<li>${typeLink({ $Type: term })}</li>`);
});
if (applicableTerms.length > 0) text.push("</ul>");
return text.join("");
return applicableTerms.length > 0
? "<br>Applicable Annotation Terms:" + linkList(applicableTerms)
: "";
}

/**
Expand All @@ -625,13 +623,9 @@ module.exports.csdl2markdown = function (filename, csdl, referenced = {}) {
* @return {string} Text
*/
function allowedTermsList(allowedTerms) {
const text = [];
if (allowedTerms.length > 0) text.push("<br>Allowed terms:<ul>");
allowedTerms.forEach((term) => {
text.push(`<li>${typeLink({ $Type: term })}</li>`);
});
if (allowedTerms.length > 0) text.push("</ul>");
return text.join("");
return allowedTerms.length > 0
? "<br>Allowed Terms:" + linkList(allowedTerms)
: "";
}

/**
Expand Down Expand Up @@ -729,7 +723,7 @@ module.exports.csdl2markdown = function (filename, csdl, referenced = {}) {
if (modelElement.$Type == "com.sap.vocabularies.Common.v1.Experimental")
customFile = "Common.md";

return (
let type =
(modelElement.$Collection ? "\\[" : "") +
(customType ? "[" : "") +
(customType
Expand All @@ -741,8 +735,31 @@ module.exports.csdl2markdown = function (filename, csdl, referenced = {}) {
: np.name) +
(modelElement.$Nullable ? "?" : "") +
(customType ? "](" + customFile + "#" + np.name + ")" : "") +
(modelElement.$Collection ? "\\]" : "")
);
(modelElement.$Collection ? "\\]" : "");
if (modelElement[voc.Validation.DerivedTypeConstraint])
type +=
"<br>Allowed Derived Types:" +
linkList(modelElement[voc.Validation.DerivedTypeConstraint]);
return type;
}

/**
* a qualified name consists of a namespace or alias, a dot, and a simple name
* @param {object} list of model element names
* @return {string} list of links to these model elements
*/
function linkList(list) {
let text = "<ul>";
for (let t of list) {
let link;
if (t.startsWith("Collection(")) {
t = t.substring(11, t.length - 1);
link = "[.]";
} else link = ".";
text += `<li>${link.replace(".", typeLink({ $Type: t }))}</li>`;
}
text += "</ul>";
return text;
}

/**
Expand Down Expand Up @@ -789,6 +806,7 @@ module.exports.csdl2markdown = function (filename, csdl, referenced = {}) {
"AllowedValues",
"ApplicableTerms",
"AllowedTerms",
"DerivedTypeConstraint",
"Exclusive",
"Maximum",
"Minimum",
Expand Down
65 changes: 64 additions & 1 deletion test/csdl2markdown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ describe("Non-OASIS Vocabularies", function () {
"",
"Term|Type|Description",
":---|:---|:----------",
'Reference|AnnotationPath|<a name="Reference"></a>Reference to a description<br>Allowed terms:<ul><li>[Description](https://github.com/oasis-tcs/odata-vocabularies/blob/main/vocabularies/Org.OData.Core.V1.md#Description)</li><li>[WithoutReference](#WithoutReference)</li></ul>',
'Reference|AnnotationPath|<a name="Reference"></a>Reference to a description<br>Allowed Terms:<ul><li>[Description](https://github.com/oasis-tcs/odata-vocabularies/blob/main/vocabularies/Org.OData.Core.V1.md#Description)</li><li>[WithoutReference](#WithoutReference)</li></ul>',
"",
];
const markdown = lib.csdl2markdown(filename, vocabulary);
Expand Down Expand Up @@ -557,6 +557,69 @@ describe("Edge cases", function () {
const markdown = lib.csdl2markdown(filename, vocabulary);
assert.deepStrictEqual(markdown, expectedMarkdown);
});

it("Allowed derived types", function () {
const filename = "allowedDerivedTypes.xml";
const type = {
"@Org.OData.Core.V1.Description": "Tag or duration",
"@Org.OData.Validation.V1.DerivedTypeConstraint": [
"Org.OData.Core.V1.Tag",
"Edm.Duration",
],
};
const mdtype =
"PrimitiveType<br>Allowed Derived Types:<ul><li>[Tag](https://github.com/oasis-tcs/odata-vocabularies/blob/main/vocabularies/Org.OData.Core.V1.md#Tag)</li><li>Duration</li></ul>";
const vocabulary = {
$Version: "4.01",
$Reference: {
"https://oasis-tcs.github.io/odata-vocabularies/vocabularies/Org.OData.Core.V1.json":
{
$Include: [
{
$Namespace: "Org.OData.Core.V1",
},
],
},
},
"Allowed.v1": {
Duration: {
$Kind: "TypeDefinition",
$UnderlyingType: "Edm.PrimitiveType",
...type,
},
Event: {
$Kind: "ComplexType",
Duration: {
$Type: "Edm.PrimitiveType",
...type,
},
},
},
};
const expectedMarkdown = [
"# Allowed Vocabulary",
"**Namespace: [Allowed.v1](allowedDerivedTypes.xml)**",
"",
"",
"",
'<a name="Duration"></a>',
"## Duration",
"**Type:** " + mdtype,
"",
"Tag or duration",
"",
'<a name="Event"></a>',
"## Event",
"",
"",
"Property|Type|Description",
":-------|:---|:----------",
"Duration|" + mdtype + "|Tag or duration",
"",
];
const markdown = lib.csdl2markdown(filename, vocabulary);
assert.deepStrictEqual(markdown, expectedMarkdown);
});
});

function check(actual, expected) {
Expand Down

0 comments on commit 257c94d

Please sign in to comment.