Skip to content

Commit

Permalink
feat: use new generic filters (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
derberg authored Apr 29, 2020
1 parent 5ce0d87 commit 19a47fe
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 150 deletions.
5 changes: 4 additions & 1 deletion .tp-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
"required": false
}
},
"generator": ">=0.40.1 <2.0.0"
"generator": ">=0.41.0 <2.0.0",
"filters": [
"@asyncapi/generator-filters"
]
}
229 changes: 86 additions & 143 deletions filters/all.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,84 @@
const _ = require('lodash');
const Markdown = require('markdown-it');
const OpenAPISampler = require('openapi-sampler');

module.exports = ({ Nunjucks }) => {
Nunjucks.addFilter('split', (string, separator) => {
if (typeof string !== 'string') return string;
const regex = new RegExp(separator, 'g');
return string.split(regex);
});

Nunjucks.addFilter('firstKey', (obj) => {
if (!obj) return '';
return Object.keys(obj)[0];
});

Nunjucks.addFilter('isExpandable', (obj) => {
if (
obj.type() === 'object' ||
obj.type() === 'array' ||
(obj.oneOf() && obj.oneOf().length) ||
(obj.anyOf() && obj.anyOf().length) ||
(obj.allOf() && obj.allOf().length) ||
obj.items() ||
obj.additionalItems() ||
(obj.properties() && Object.keys(obj.properties()).length) ||
obj.additionalProperties() ||
(obj.extensions() && Object.keys(obj.extensions()).filter(e => !e.startsWith('x-parser-')).length) ||
obj.patternProperties()
) return true;

return false;
});

/**
* Check if there is a channel which does not have one of the tags specified.
*/
Nunjucks.addFilter('containTags', (object, tagsToCheck) => {
if (!object) {
throw new Error("object for containsTag was not provided?");
}

if (!tagsToCheck) {
throw new Error("tagsToCheck for containsTag was not provided?");
const filter = module.exports;

function isExpandable(obj) {
if (
obj.type() === 'object' ||
obj.type() === 'array' ||
(obj.oneOf() && obj.oneOf().length) ||
(obj.anyOf() && obj.anyOf().length) ||
(obj.allOf() && obj.allOf().length) ||
obj.items() ||
obj.additionalItems() ||
(obj.properties() && Object.keys(obj.properties()).length) ||
obj.additionalProperties() ||
(obj.extensions() && Object.keys(obj.extensions()).filter(e => !e.startsWith('x-parser-')).length) ||
obj.patternProperties()
) return true;

return false;
}
filter.isExpandable = isExpandable;

function nonParserExtensions(schema) {
if (!schema || !schema.extensions || typeof schema.extensions !== 'function') return new Map();
const extensions = Object.entries(schema.extensions());
return new Map(extensions.filter(e => !e[0].startsWith('x-parser-')).filter(Boolean));
}
filter.nonParserExtensions = nonParserExtensions;

/**
* Check if there is a channel which does not have one of the tags specified.
*/
function containTags(object, tagsToCheck) {
if (!object) {
throw new Error("object for containsTag was not provided?");
}

if (!tagsToCheck) {
throw new Error("tagsToCheck for containsTag was not provided?");
}

//Ensure if only 1 tag are provided it is converted to array.
if (tagsToCheck && !Array.isArray(tagsToCheck)) {
tagsToCheck = [tagsToCheck];
}

//Check if pubsub contain one of the tags to check.
let check = (tag) => {
let found = false;
for (let tagToCheckIndex in tagsToCheck) {
let tagToCheck = tagsToCheck[tagToCheckIndex]._json;
if (tagToCheck.name === tag.name) {
found = true;
}
}
return found;
};

//Ensure if only 1 tag are provided it is converted to array.
if (tagsToCheck && !Array.isArray(tagsToCheck)) {
tagsToCheck = [tagsToCheck];
//Ensure tags are checked for the group tags
let containTags = object._json.tags ? object._json.tags.find(check) != null : false;
return containTags;
};
filter.containTags = containTags;

/**
* Check if there is a channel which does not have one of the tags specified.
*/
function containNoTag(channels, tagsToCheck) {
if (!channels) {
throw new Error("Channels for containNoTag was not provided?");
}
for (let channelIndex in channels) {
let channel = channels[channelIndex]._json;
//Check if the channel contains publish or subscribe which does not contain tags
if (channel.publish && (!channel.publish.tags || channel.publish.tags.length == 0) ||
channel.subscribe && (!channel.subscribe.tags || channel.subscribe.tags.length == 0)
) {
//one does not contain tags
return true;
}

//Check if pubsub contain one of the tags to check.
//Check if channel publish or subscribe does not contain one of the tags to check.
let check = (tag) => {
let found = false;
for (let tagToCheckIndex in tagsToCheck) {
Expand All @@ -61,99 +90,13 @@ module.exports = ({ Nunjucks }) => {
return found;
};

//Ensure tags are checked for the group tags
let containTags = object._json.tags ? object._json.tags.find(check) != null : false;
return containTags;
});

/**
* Check if there is a channel which does not have one of the tags specified.
*/
Nunjucks.addFilter('containNoTag', (channels, tagsToCheck) => {
if (!channels) {
throw new Error("Channels for containNoTag was not provided?");
}
for (let channelIndex in channels) {
let channel = channels[channelIndex]._json;
//Check if the channel contains publish or subscribe which does not contain tags
if (channel.publish && (!channel.publish.tags || channel.publish.tags.length == 0) ||
channel.subscribe && (!channel.subscribe.tags || channel.subscribe.tags.length == 0)
) {
//one does not contain tags
return true;
}

//Check if channel publish or subscribe does not contain one of the tags to check.
let check = (tag) => {
let found = false;
for (let tagToCheckIndex in tagsToCheck) {
let tagToCheck = tagsToCheck[tagToCheckIndex]._json;
if (tagToCheck.name === tag.name) {
found = true;
}
}
return found;
};

//Ensure pubsub tags are checked for the group tags
let publishContainsNoTag = channel.publish && channel.publish.tags ? channel.publish.tags.find(check) == null : false;
if (publishContainsNoTag === true) return true;
let subscribeContainsNoTag = channel.subscribe && channel.subscribe.tags ? channel.subscribe.tags.find(check) == null : false;
if (subscribeContainsNoTag === true) return true;
}
return false;
});

Nunjucks.addFilter('isArray', (arr) => {
return Array.isArray(arr);
});

Nunjucks.addFilter('isObject', (obj) => {
return typeof obj === 'object' && obj !== null;
});

Nunjucks.addFilter('contains', (array, element) => {
if (!array || !Array.isArray(array)) return false;
return array.includes(element);
});

Nunjucks.addFilter('log', (anything) => {
console.log(anything);
});

Nunjucks.addFilter('markdown2html', (md) => {
return Markdown().render(md || '');
});

Nunjucks.addFilter('getPayloadExamples', (msg) => {
if (Array.isArray(msg.examples()) && msg.examples().find(e => e.payload)) {
// Instead of flat or flatmap use this.
return _.flatMap(msg.examples().map(e => e.payload).filter(Boolean));
}

if (msg.payload() && msg.payload().examples()) {
return msg.payload().examples();
}
});

Nunjucks.addFilter('getHeadersExamples', (msg) => {
if (Array.isArray(msg.examples()) && msg.examples().find(e => e.headers)) {
// Instead of flat or flatmap use this.
return _.flatMap(msg.examples().map(e => e.headers).filter(Boolean));
}

if (msg.headers() && msg.headers().examples()) {
return msg.headers().examples();
}
});

Nunjucks.addFilter('generateExample', (schema) => {
return JSON.stringify(OpenAPISampler.sample(schema) || '', null, 2);
});

Nunjucks.addFilter('nonParserExtensions', (schema) => {
if (!schema || !schema.extensions || typeof schema.extensions !== 'function') return new Map();
const extensions = Object.entries(schema.extensions());
return new Map(extensions.filter(e => !e[0].startsWith('x-parser-')).filter(Boolean));
});
//Ensure pubsub tags are checked for the group tags
let publishContainsNoTag = channel.publish && channel.publish.tags ? channel.publish.tags.find(check) == null : false;
if (publishContainsNoTag === true) return true;
let subscribeContainsNoTag = channel.subscribe && channel.subscribe.tags ? channel.subscribe.tags.find(check) == null : false;
if (subscribeContainsNoTag === true) return true;
}
return false;
};
filter.containNoTag = containNoTag;

10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
"access": "public"
},
"dependencies": {
"lodash": "^4.17.15",
"markdown-it": "^10.0.0",
"openapi-sampler": "^1.0.0-beta.15"
"@asyncapi/generator-filters": "^1.0.0"
},
"devDependencies": {
"@semantic-release/commit-analyzer": "^8.0.1",
Expand Down
4 changes: 2 additions & 2 deletions partials/schema-prop.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
<div class="children {% if odd %}bg-grey-lightest{% else %}bg-grey-lighter{% endif %} py-4 rounded">
{% if prop.properties() %}
{% for pName, p in prop.properties() %}
{{ schemaProp(p, pName, odd=(not odd), required=(prop.required() | contains(pName))) }}
{{ schemaProp(p, pName, odd=(not odd), required=(prop.required() | includes(pName))) }}
{% endfor %}
{% endif %}

Expand Down Expand Up @@ -145,7 +145,7 @@
<p class="pl-6 mb-2 text-xs font-bold uppercase text-grey-darker">Items:</p>
{% if prop.items() | isArray %}
{% for it in prop.items() %}
{{ schemaProp(it, loop.index0, odd=(not odd), required=(prop.required() | contains(pName))) }}
{{ schemaProp(it, loop.index0, odd=(not odd), required=(prop.required() | includes(pName))) }}
{% endfor %}
{% else %}
{{ schemaProp(prop.items(), '0', odd=(not odd)) }}
Expand Down
2 changes: 1 addition & 1 deletion partials/servers.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ <h5 class="text-sm text-grey mt-1">Security:</h5>
<ul class="list-reset">
{% for sec in server.security() %}
<li>
{% set def = asyncapi.components().securityScheme(sec.json() | firstKey) %}
{% set def = asyncapi.components().securityScheme(sec.json() | keys | head) %}

<span class="font-bold no-underline text-grey-dark text-xs uppercase mr-1">
{% if def.type() === 'apiKey' %}
Expand Down

0 comments on commit 19a47fe

Please sign in to comment.