Skip to content

Commit

Permalink
PR feedback from Lukasz
Browse files Browse the repository at this point in the history
  • Loading branch information
Florence-Njeri committed Jan 9, 2024
1 parent bd6d6ee commit 46e6722
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 40 deletions.
2 changes: 1 addition & 1 deletion docs/installation-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ You can install in Linux by using `dpkg`, a package manager for debian:
For further installation instructions for different operating systems, read the [AsyncAPI CLI documentation](https://github.com/asyncapi/cli#installation).

> **Remember:**
> Each [community-developed template](https://github.com/search?q=topic%3Aasyncapi+topic%3Agenerator+topic%3Atemplate) is dependent on a certain version of the generator for it to work correctly. Before you install the generator CLI, check the template's `package.json` for the version of the generator CLI your template is compatible with. Read the [versioning docs](versioning) to learn why it's important to use certain generator versions with your templates.
> Each [community-developed template](https://github.com/search?q=topic%3Aasyncapi+topic%3Agenerator+topic%3Atemplate) is dependent on a certain version of the generator for it to work correctly. Before you install the AsyncAPI CLI, check the template's `package.json` for the version of the AsyncAPI CLI your template is compatible with. Read the [versioning docs](versioning) to learn why it's important to use certain generator versions with your templates.
### Update AsyncAPI CLI
There are several reasons why you might want to update your generator version:
Expand Down
58 changes: 24 additions & 34 deletions docs/model-generation.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
---
title: "Generating models and classes using Modelina"
title: "How to generating models and classes using Modelina"
weight: 200
---

Suppose you want to generate models for your application from the data types you have using the asyncapi generator. Then you should use Modelina instead of the asyncapi generator with a template.
This guide will walk you through the process of generating models and classes using [Modelina](https://www.asyncapi.com/tools/modelina) and the asyncapi generator. If you want to generate models for your application from the data types, you should use Modelina instead of the asyncapi generator with a template else you should use

[Modelina](https://www.asyncapi.com/tools/modelina) is an AsyncAPI library that is used to generate data models using inputs such as AsyncAPI, OpenAPI or JSON schema inputs. This library helps generate data models based on your AsyncAPI document, the model template (which defines the message payloads) via the asyncapi CLI. You can then use the generated models in your code, and you can store the generated models in a single file. This tutorial will guide you through generating a model class for a Python MQTT client using Modelina and the AsyncAPI CLI.

In this tutorial:
In this guide, you'll learn to:

1. You'll learn how to generate a model class using a Python MQTT client.
2. You'll use the Modelina library and the asyncapi CLI to generate Python data models.
3. You'll create a model schema to test your model code using an MQTT client.
1. Generate a model class using a Python MQTT client.
2. Use the Modelina library and the asyncapi CLI to generate Python data models.
3. Create a model schema to test your model code using an MQTT client.

## Prerequisites

This tutorial builds upon an existing project, the [MQTT Python project](https://github.com/derberg/python-mqtt-client-template). The project creates a custom generator template using a Python MQTT client. We'll use the same project to create a simple Modelina template and generate data models using the Python MQTT client. To get an in-depth understanding of what this project does, please go through the [generator template tutorial](https://www.asyncapi.com/docs/tools/generator/generator_template).

You should also have [Node.js and npm](https://nodejs.org/en/download/) and the [AsyncAPI CLI](https://www.asyncapi.com/docs/tools/generator/installation-guide#asyncapi-cli) installed in your machine.
Before you begin, ensure you have the following installed:
- [Node.js and npm](https://nodejs.org/en/download/).
- [AsyncAPI CLI](https://www.asyncapi.com/docs/tools/generator/installation-guide#asyncapi-cli) installed in your machine.

> :memo: **Note:**
> When building the model from scratch, you'll need to have a predefined [AsyncAPI document](https://www.asyncapi.com/docs/tools/generator/asyncapi-document) and you can also use the existing [community-maintained templates](https://www.asyncapi.com/docs/tools/generator/template#generator-templates-list) instead of creating a template from scratch.
## Getting started

First, clone the MQTT Python project from Github using the following command:
`git clone https://github.com/derberg/python-mqtt-client-template`

Open the Python MQTT project in your code editor
First, install Modelina in your project using npm:

Then, add the Modelina dependency to the `package.json` file in your project:
`npm install --save @asyncapi/modelina`
This command will then automatically add the modelina dependency to your `package.json` as shown below:

```json
"dependencies": {
Expand All @@ -39,10 +36,6 @@ Then, add the Modelina dependency to the `package.json` file in your project:
},
```

Finally, install Modelina in your project using npm:

`npm install @asyncapi/modelina`

## Generating models

With Modelina installed, you can now generate models using an AsyncAPI document as an input. In the next section, you'll create a `model.js` file to define the logic of your model(s).
Expand Down Expand Up @@ -92,37 +85,34 @@ export default async function schemaRender({ asyncapi }) {

Let's break it down. The code snippet above does the following:

1. Imports the **File** component from the [generator react SDK](https://github.com/asyncapi/generator-react-sdk). This tells the generator CLI to use the [react render engine](https://www.asyncapi.com/docs/tools/generator/react-render-engine) to generate a model file or files as output.
1. Imports the **File** component from the [generator react SDK](https://github.com/asyncapi/generator-react-sdk). This tells the AsyncAPI CLI to use the [react render engine](https://www.asyncapi.com/docs/tools/generator/react-render-engine) to generate a model file or files as output.
2. Import Modelina into your template. The Modelina library makes the **PythonGenerator** and the **FormatHelpers** used to format the generated Python code available to your model template.
3. Define an asynchronous function **schemaRender** that get's invoked everytime the generator CLI runs the **models.js** template. This function is responsible for rendering the generated schema files. It also takes the `asyncapi` object as an input parameter.
3. Define an asynchronous function **schemaRender** that get's invoked everytime the AsyncAPI CLI runs the **models.js** template. This function is responsible for rendering the generated schema files. It also takes the `asyncapi` object as an input parameter.
4. Instantiates the **PythonGenerator** model generator from Modelina. It will be used to generate Python code from the AsyncAPI document.
5. Generates an array of Python model classes from the `asyncapi` (AsyncAPI document) object received from the generator.
6. Define an empty array **files** to store the generated model files.
7. Iterates over each generated model and formats the generated model names to Pascal case.
8. Add filenames and the model schemas to the **files** array.
9. Return an array of files, each representing the generated model classes, to the generator engine.

## Model generation using the AsyncAPI CLI
## Generate models
Use the AsyncAPI CLI to generate your models using the following command:

Using the AsyncAPI CLI, generate your model by running the following command:

`asyncapi generate fromTemplate test/fixtures/asyncapi.yml ./ -o test/project --force-write --param server=dev`
```
asyncapi generate fromTemplate test/fixtures/asyncapi.yml ./ -o test/project --force-write --param server=dev
```
If successful, you should see the following output:

If successful, you should see the following output on your terminal:
```
Generation in progress. Keep calm and wait a bit... done
Check out your shiny new generated files at test/project.
```

Since you defined your model in **src/models** the generated model schema will be in the **test/project -> src/models** directory.
Navigate to **test/project** folder and you should see that your model template generated two models in the **src/models** folder.
Let's break down the previous command:

- `asyncapi generate fromTemplate` is how you use AsyncAPI generator via the AsyncAPI CLI.
- `test/fixtures/asyncapi.yml` points to your AsyncAPI document.
- `./` specifies the location of your model template.
- `-o` specifies where to output the generated data models.
Navigate to test/project/src/models to find your generated model files.

## Conclusion

Modelina provides a flexible and powerful way to generate data models from AsyncAPI, OpenAPI, or JSON Schema documents. With the AsyncAPI CLI, you can easily generate models and integrate them into your development workflow. With the ability to customize the generated models, Modelina proves to be a valuable tool in the development of event-driven architectures.

>Note:
>You can integrate this example into your own template by following the [generator template tutorial](https://www.asyncapi.com/docs/tools/generator/generator-template). Ensure that you add `@asyncapi/modelina` to template dependencies using `npm install --save @asyncapi/modelina``.
2 changes: 1 addition & 1 deletion docs/template-development.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ Newer:
<Text>Version is: **{params.version || asyncapi.info.version()}**</Text>
```
Now that you have added all the configuration options, you can start the generation process using the generator CLI. You can pass these parameters via the CLI: `--param name=value or -p name=value`.
Now that you have added all the configuration options, you can start the generation process using the AsyncAPI CLI. You can pass these parameters via the CLI: `--param name=value or -p name=value`.
The above configuration helps template users override the existing version with a new version on the command line. (Example: `-p version=2.0.0`)
## Hooks
Expand Down
2 changes: 1 addition & 1 deletion docs/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ You can store template projects on a local drive or as a `git` repository during

1. Template is provided as input to the **Generator**.
2. **asyncapi** is the original AsyncAPI document injected into your template file by default.
3. **params** are the parameters you pass to the generator CLI. Later, you can also pass these **params** further to other components.
3. **params** are the parameters you pass to the AsyncAPI CLI. Later, you can also pass these **params** further to other components.
4. The generator passes both the original **asyncapi**, the original AsyncAPI document, and the **params** to the **Template Context**.
5. Concurrently, the generator passes **Template files** to the **Render engine** as well. AsyncAPI uses two render engines — _react_ and _nunjucks_.
6. Once the Render Engine receives both the Template Files and the Template Context, it injects all the dynamic values into your react or nunjucks engine, based on the Template Files using the Template Context.
Expand Down
6 changes: 3 additions & 3 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ weight: 30
---

There are two ways to use the generator:
- [Generator CLI](#generator-cli)
- [AsyncAPI CLI](#generator-cli)
- [Generator library](#using-as-a-modulepackage)

## Generator CLI
## AsyncAPI CLI
```bash
Usage: asyncapi generate fromTemplate <asyncapi> <template> [<options>]

Expand Down Expand Up @@ -43,7 +43,7 @@ npm install <folder>
### Global templates installed with `yarn` or `npm`
You can preinstall templates globally before installing the generator CLI. The generator first tries to locate the template in local dependencies; if absent it checks where the global generator packages are installed.
You can preinstall templates globally before installing the AsyncAPI CLI. The generator first tries to locate the template in local dependencies; if absent it checks where the global generator packages are installed.
```bash
npm install -g @asyncapi/[email protected]
Expand Down

0 comments on commit 46e6722

Please sign in to comment.