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

feat: migrated the whole template to react #207

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions filters/all.js → helpers/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,3 +435,5 @@ function getTopicInfo(channelName, channel) {
function indent(numTabs) {
return " ".repeat(numTabs * 4);
}

module.exports = filter;
15 changes: 15 additions & 0 deletions helpers/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const sanitizeFilename = require('sanitize-filename');

function convertToFilename(string, options = { replacement: '-', maxLength: 255 }) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you take this direction? did you face some issues while testing changes?

let sanitizedString = sanitizeFilename(string);

sanitizedString = sanitizedString.replace(/[\s]+/g, options.replacement);

if (sanitizedString.length > options.maxLength) {
sanitizedString = sanitizedString.slice(0, options.maxLength);
}

return sanitizedString;
}

module.exports = convertToFilename;
6 changes: 0 additions & 6 deletions hooks/post-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,5 @@ module.exports = {
}
}
}

// If there are no schemas, we expect to find an anonymous one embedded in a payload. If we do have schemas we assume we don't need this.
// This will turn out to be a bug if we ever have a file with schemas, but which also has an anonymous schema embedded in an operation.
if (hasSchema) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it not needed because of convertToFilename implementation? why so?

or basically why template/payload.py is no longer needed

fs.unlinkSync(path.resolve(generator.targetDir, 'payload.py'));
}
}
}
2 changes: 1 addition & 1 deletion lib/templateUtil.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// This contains functions taht are common to both the all.js filter and the post-process.js hook.
var _ = require('lodash');
const _ = require('lodash');

class TemplateUtil {

Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"homepage": "https://github.com/asyncapi/python-paho-template#readme",
"dependencies": {
"@asyncapi/generator-filters": "^2.1.0",
"@asyncapi/generator-react-sdk": "^1.1.2",
"js-yaml": "^3.13.1",
"lodash": "^4.17.15"
},
Expand All @@ -42,6 +43,7 @@
"@semantic-release/release-notes-generator": "^9.0.1",
"conventional-changelog-conventionalcommits": "^4.2.3",
"eslint": "^6.8.0",
"sanitize-filename": "^1.6.3",
"semantic-release": "^21.0.1"
},
"release": {
Expand Down Expand Up @@ -73,6 +75,7 @@
"required": false
}
},
"renderer": "react",
"filters": [
"@asyncapi/generator-filters"
]
Expand Down
35 changes: 0 additions & 35 deletions partials/model-class

This file was deleted.

52 changes: 52 additions & 0 deletions partials/model-class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const _ = require('lodash');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of partials people should rather opt in to use components reference for reusable components - more natively common word in the context of react and react components concept

const getTypeInfo = require('../helpers/all').getTypeInfo;
const indent1 = require('../helpers/all').indent1;
const indent2 = require('../helpers/all').indent2;
const indent3 = require('../helpers/all').indent3;
const indent4 = require('../helpers/all').indent4;

function modelClass(name, properties, required, indentLevel = 1) {
const className = _.upperFirst(name);

let classDefinition = `${indent1(indentLevel)}class ${className}(Entity):\n`;

Object.entries(properties).forEach(([propName, prop]) => {
const typeInfo = getTypeInfo([propName, prop]);

if (typeInfo.recursive) {
classDefinition += modelClass(
typeInfo.innerType, typeInfo.properties, prop.required(), indentLevel + 1
);
} else if (typeInfo.generalType === 'enum') {
classDefinition += `${indent2(indentLevel)}class ${typeInfo.type}(str, Enum):\n`;
typeInfo.enum.forEach((value) => {
classDefinition += `${indent3(indentLevel)}${value} = '${value}'\n`;
});
classDefinition += `${indent2(indentLevel)}\n`;
}
});

classDefinition += `${indent2(indentLevel)}def __init__(self,\n`;

Object.entries(properties).forEach(([propName, prop], index) => {
const typeInfo = getTypeInfo([propName, prop]);
const separator = index === 0 ? '' : ',\n';
classDefinition += `${separator}${indent4(indentLevel)}${typeInfo.pythonName}: ${typeInfo.pythonType}`;
});

classDefinition += `):\n`;

Object.entries(properties).forEach(([propName, prop]) => {
const typeInfo = getTypeInfo([propName, prop]);
classDefinition += `${indent3(indentLevel)}self.${typeInfo.pythonName} = ${typeInfo.pythonName}\n`;
});

if (Object.keys(properties).length === 0) {
classDefinition += `${indent3(indentLevel)}pass\n`;
}

classDefinition += `${indent1(indentLevel)}\n`;
return classDefinition;
}

module.exports = { modelClass }
7 changes: 0 additions & 7 deletions template/$$schema$$.py

This file was deleted.

6 changes: 0 additions & 6 deletions template/README.md

This file was deleted.

15 changes: 15 additions & 0 deletions template/README.md.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { File } = require('@asyncapi/generator-react-sdk');

function ReadmeFile({ asyncapi }) {
return (
<File name={'README.md'}>
{`# ${ asyncapi.info().title() }

## Version ${ asyncapi.info().version() }

${ asyncapi.info().description() }`}
</File>
);
}

module.exports = ReadmeFile;
16 changes: 16 additions & 0 deletions template/config-template.in.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { File } = require('@asyncapi/generator-react-sdk');

function ReadmeFile({ asyncapi }) {
return (
<File name={'config-template.in'}>
{`[DEFAULT]
host=
password=
port=
username=
`}
</File>
);
}

module.exports = ReadmeFile;
5 changes: 0 additions & 5 deletions template/config-template.ini

This file was deleted.

15 changes: 13 additions & 2 deletions template/entity.py → template/entity.py.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import json
const { File } = require('@asyncapi/generator-react-sdk');

function ReadmeFile({ asyncapi }) {
return (
<File name={'entity.py'}>
{`import json

class Entity():
"""This is the base class for the model classes. It provides json serialization methods."""


@classmethod
def from_json(cls, data):
jsonObj = json.loads(data)
return cls(**jsonObj)

def to_json(self):
return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=2)
return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=2)`}
</File>
);
}

module.exports = ReadmeFile;
70 changes: 0 additions & 70 deletions template/main.py

This file was deleted.

Loading
Loading