Skip to content

Commit

Permalink
feat: use new generic filters (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
derberg authored Apr 29, 2020
1 parent 859a8ac commit 922207e
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 72 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.gradle
node_modules
output
output
.DS_Store
5 changes: 4 additions & 1 deletion .tp-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
"required": false
}
},
"generator": ">=0.40.1 <2.0.0"
"generator": ">=0.41.0 <2.0.0",
"filters": [
"@asyncapi/generator-filters"
]
}
133 changes: 63 additions & 70 deletions filters/all.js
Original file line number Diff line number Diff line change
@@ -1,82 +1,75 @@
const filter = module.exports;
const _ = require('lodash');

module.exports = ({ Nunjucks }) => {
Nunjucks.addFilter('camelCase', (str) => {
return _.camelCase(str);
});

Nunjucks.addFilter('upperFirst', (str) => {
return _.upperFirst(str);
});
function toJavaType(str){
switch(str) {
case 'integer':
case 'int32':
return 'int';
case 'long':
case 'int64':
return 'long';
case 'boolean':
return 'boolean';
case 'date':
return 'java.time.LocalDate';
case 'dateTime':
case 'date-time':
return 'java.time.LocalDateTime';
case 'string':
case 'password':
case 'byte':
return 'String';
case 'float':
return 'float';
case 'double':
return 'double';
case 'binary':
return 'byte[]';
default:
return 'Object';
}
}
filter.toJavaType = toJavaType;

Nunjucks.addFilter('toJavaType', (str) => {
switch(str) {
case 'integer':
case 'int32':
return 'int';
case 'long':
case 'int64':
return 'long';
case 'boolean':
return 'boolean';
case 'date':
return 'java.time.LocalDate';
case 'dateTime':
case 'date-time':
return 'java.time.LocalDateTime';
case 'string':
case 'password':
case 'byte':
return 'String';
case 'float':
return 'float';
case 'double':
return 'double';
case 'binary':
return 'byte[]';
default:
return 'Object';
}
});

Nunjucks.addFilter('isProtocol', (api, protocol) => {
return JSON.stringify(api.json()).includes('"protocol":"' + protocol + '"');
});

Nunjucks.addFilter('print', (str) => {
console.error(str);
});
function isProtocol(api, protocol){
return JSON.stringify(api.json()).includes('"protocol":"' + protocol + '"');
};
filter.isProtocol = isProtocol;

Nunjucks.addFilter('examplesToString', (ex) => {
let retStr = "";
ex.forEach(example => {
if (retStr !== "") {retStr += ", "}
if (typeof example == "object") {
try {
retStr += JSON.stringify(example);
} catch (ignore) {
retStr += example;
}
} else {
function examplesToString(ex){
let retStr = "";
ex.forEach(example => {
if (retStr !== "") {retStr += ", "}
if (typeof example == "object") {
try {
retStr += JSON.stringify(example);
} catch (ignore) {
retStr += example;
}
});
return retStr;
});

Nunjucks.addFilter('splitByLines', (str) => {
if (str) {
return str.split(/\r?\n|\r/).filter((s) => s !== "");
} else {
return "";
retStr += example;
}
});
return retStr;
};
filter.examplesToString = examplesToString;

Nunjucks.addFilter('isRequired', (name, list) => {
return list && list.includes(name);
});
function splitByLines(str){
if (str) {
return str.split(/\r?\n|\r/).filter((s) => s !== "");
} else {
return "";
}
};
filter.splitByLines = splitByLines;

Nunjucks.addFilter('schemeExists', (collection, scheme) => {
return _.some(collection, {'scheme': scheme});
});
function isRequired(name, list){
return list && list.includes(name);
};
filter.isRequired = isRequired;

function schemeExists(collection, scheme){
return _.some(collection, {'scheme': scheme});
};
filter.schemeExists = schemeExists;
79 changes: 79 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"access": "public"
},
"dependencies": {
"@asyncapi/generator-filters": "^1.0.0",
"lodash": "^4.17.15"
},
"devDependencies": {
Expand Down

0 comments on commit 922207e

Please sign in to comment.