Skip to content

Commit

Permalink
Merge branch 'master' into website_docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni authored Sep 27, 2023
2 parents ad1c033 + d7f38e4 commit 2a13e08
Show file tree
Hide file tree
Showing 24 changed files with 620 additions and 16 deletions.
14 changes: 13 additions & 1 deletion .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,9 @@
"profile": "http://markus.poerschke.nrw",
"contributions": [
"code",
"test"
"test",
"example",
"doc"
]
},
{
Expand Down Expand Up @@ -693,6 +695,16 @@
"contributions": [
"code"
]
},
{
"login": "Devansh-Bhatt",
"name": "Devansh-Bhatt",
"avatar_url": "https://avatars.githubusercontent.com/u/94732079?v=4",
"profile": "https://github.com/Devansh-Bhatt",
"contributions": [
"test",
"infra"
]
}
],
"contributorsPerLine": 7,
Expand Down
32 changes: 32 additions & 0 deletions .github/workflows/runtime-go-testing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Runtime Testing Go Models
on:
push:
pull_request:
types: [opened, reopened, synchronize, ready_for_review]
paths:
- 'src/generators/go/**'
- 'test/runtime/runtime-go/**'
- test/runtime/**go**

jobs:
test:
name: Runtime testing Go Models
if: "github.event.pull_request.draft == false &&!((github.actor == 'asyncapi-bot' && startsWith(github.event.pull_request.title, 'ci: update global workflows')) || (github.actor == 'asyncapi-bot' && startsWith(github.event.pull_request.title, 'chore(release):')) || (github.actor == 'allcontributors' && startsWith(github.event.pull_request.title, 'docs: add')))"
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: 14
- name: Build Library
run: npm install && npm run build:prod
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: 1.20
-name: Generate Go Models
run: npm run generate:runtime:go
-name: Run runtime Tests
run: npm run test:runtime:go
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[![Discussions](https://img.shields.io/github/discussions/asyncapi/modelina)](https://github.com/asyncapi/modelina/discussions)
[![Website](https://img.shields.io/website?label=website&url=https%3A%2F%2Fwww.modelina.org)](https://www.modelina.org)
[![Playground](https://img.shields.io/website?label=playground&url=https%3A%2F%2Fwww.modelina.org%2Fplayground)](https://www.modelina.org/playground) <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-63-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-64-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->

Your one-stop tool for generating accurate and well-tested models for representing the message payloads. Use it as a tool in your development workflow, or a library in a larger integrations, entirely in your control.
Expand Down Expand Up @@ -401,11 +401,14 @@ Thanks go out to these wonderful people ([emoji key](https://allcontributors.org
<td align="center" valign="top" width="14.28%"><a href="http://jfcote.github.io"><img src="https://avatars.githubusercontent.com/u/14336900?v=4?s=100" width="100px;" alt="Jean-François Côté"/><br /><sub><b>Jean-François Côté</b></sub></a><br /><a href="https://github.com/asyncapi/modelina/commits?author=JFCote" title="Code">💻</a> <a href="https://github.com/asyncapi/modelina/commits?author=JFCote" title="Tests">⚠️</a> <a href="#example-JFCote" title="Examples">💡</a> <a href="https://github.com/asyncapi/modelina/commits?author=JFCote" title="Documentation">📖</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/SumantxD"><img src="https://avatars.githubusercontent.com/u/65810424?v=4?s=100" width="100px;" alt="Sumant.xD"/><br /><sub><b>Sumant.xD</b></sub></a><br /><a href="https://github.com/asyncapi/modelina/commits?author=SumantxD" title="Tests">⚠️</a> <a href="#infra-SumantxD" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="https://github.com/asyncapi/modelina/commits?author=SumantxD" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/aryanas159"><img src="https://avatars.githubusercontent.com/u/114330931?v=4?s=100" width="100px;" alt="Aryan Singh"/><br /><sub><b>Aryan Singh</b></sub></a><br /><a href="https://github.com/asyncapi/modelina/commits?author=aryanas159" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="http://markus.poerschke.nrw"><img src="https://avatars.githubusercontent.com/u/1222377?v=4?s=100" width="100px;" alt="Markus Poerschke"/><br /><sub><b>Markus Poerschke</b></sub></a><br /><a href="https://github.com/asyncapi/modelina/commits?author=markuspoerschke" title="Code">💻</a> <a href="https://github.com/asyncapi/modelina/commits?author=markuspoerschke" title="Tests">⚠️</a></td>
<td align="center" valign="top" width="14.28%"><a href="http://markus.poerschke.nrw"><img src="https://avatars.githubusercontent.com/u/1222377?v=4?s=100" width="100px;" alt="Markus Poerschke"/><br /><sub><b>Markus Poerschke</b></sub></a><br /><a href="https://github.com/asyncapi/modelina/commits?author=markuspoerschke" title="Code">💻</a> <a href="https://github.com/asyncapi/modelina/commits?author=markuspoerschke" title="Tests">⚠️</a> <a href="#example-markuspoerschke" title="Examples">💡</a> <a href="https://github.com/asyncapi/modelina/commits?author=markuspoerschke" title="Documentation">📖</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/jamesmoey"><img src="https://avatars.githubusercontent.com/u/457472?v=4?s=100" width="100px;" alt="James Moey"/><br /><sub><b>James Moey</b></sub></a><br /><a href="https://github.com/asyncapi/modelina/commits?author=jamesmoey" title="Code">💻</a> <a href="https://github.com/asyncapi/modelina/commits?author=jamesmoey" title="Tests">⚠️</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/tomwolanski"><img src="https://avatars.githubusercontent.com/u/68085653?v=4?s=100" width="100px;" alt="tomwolanski"/><br /><sub><b>tomwolanski</b></sub></a><br /><a href="https://github.com/asyncapi/modelina/issues?q=author%3Atomwolanski" title="Bug reports">🐛</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/Ksisa"><img src="https://avatars.githubusercontent.com/u/53404771?v=4?s=100" width="100px;" alt="Kristupas"/><br /><sub><b>Kristupas</b></sub></a><br /><a href="https://github.com/asyncapi/modelina/commits?author=Ksisa" title="Code">💻</a></td>
</tr>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/Devansh-Bhatt"><img src="https://avatars.githubusercontent.com/u/94732079?v=4?s=100" width="100px;" alt="Devansh-Bhatt"/><br /><sub><b>Devansh-Bhatt</b></sub></a><br /><a href="https://github.com/asyncapi/modelina/commits?author=Devansh-Bhatt" title="Tests">⚠️</a> <a href="#infra-Devansh-Bhatt" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a></td>
</tr>
</tbody>
</table>

Expand Down
19 changes: 15 additions & 4 deletions docs/languages/Php.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,32 @@ There are special use-cases that each language supports; this document pertains

## Description Present

By default, descriptions are not rendered for the model, you can change that by applying `PHP_DESCRIPTION_PRESET`.
By default, descriptions are not rendered for the model; you can change that by applying `PHP_DESCRIPTION_PRESET`.

Check out this [example for a live demonstration](../../examples/php-generate-documentation-preset).

## Generate serializer and deserializer functionality

The most widely used usecase for Modelina is to generate models that include serilization and deserialization functionality to convert the models into payload data. This payload data can of course be many different kinds, JSON, XML, raw binary, you name it.
The most widely used usecase for Modelina is to generate models that include serialization and deserialization functionality to convert the models into payload data.
This payload data can, of course, be many different kinds, JSON, XML, raw binary, you name it.

As you normally only need one library to do this, we developers can never get enough with creating new stuff, therefore there might be one specific library you need or want to integrate with. Therefore there is not one specific preset that offers everything. Below is a list of all the supported serialization presets.
As you normally only need one library to do this, we developers can never get enough of creating new stuff, therefore, there might be one specific library you need or want to integrate with.
Therefore, there is not one specific preset that offers everything. Below is a list of all the supported serialization presets.

### To and from JSON
Currently not supported, [let everyone know you need it](https://github.com/asyncapi/modelina/issues/new?assignees=&labels=enhancement&template=enhancement.md)!

Objects in PHP can generally be serialized to JSON using the [`json_encode()` function](https://www.php.net/manual/en/function.json-encode.php).
To ensure that the data is serialized correctly, the [`JsonSerializable` interface](https://www.php.net/manual/en/class.jsonserializable.php) needs to be implemented.
This will ensure that, for example, enum values and property names are serialized correctly.

To add support of serialization to JSON, apply the `PHP_JSON_SERIALIZABLE_PRESET` preset.
Check out this [example for a live demonstration](../../examples/php-generate-json-serializable-preset).


### To and from XML

Currently not supported, [let everyone know you need it](https://github.com/asyncapi/modelina/issues/new?assignees=&labels=enhancement&template=enhancement.md)!

### To and from binary

Currently not supported, [let everyone know you need it](https://github.com/asyncapi/modelina/issues/new?assignees=&labels=enhancement&template=enhancement.md)!
12 changes: 6 additions & 6 deletions examples/integrate-with-react/package-lock.json

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

17 changes: 17 additions & 0 deletions examples/php-generate-json-serializable-preset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# PHP Generate JSON Serializable Models Example

A basic example of how to use Modelina and output PHP model that supports JSON serialization.

## How to run this example

Run this example using:

```sh
npm i && npm run start
```

If you are on Windows, use the `start:windows` script instead:

```sh
npm i && npm run start:windows
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Should be able to render PHP and should log expected output to console 1`] = `
Array [
"<?php
declare(strict_types=1);
namespace Asyncapi;
final class Root implements \\\\JsonSerializable
{
private ?string $email;
public function getEmail(): ?string { return $this->email; }
public function setEmail(?string $email): void { $this->email = $email; }
public function jsonSerialize(): array
{
return [
'email' => $this->email,
];
}
}
",
]
`;
15 changes: 15 additions & 0 deletions examples/php-generate-json-serializable-preset/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const spy = jest.spyOn(global.console, 'log').mockImplementation(() => {
return;
});
import { generate } from './index';

describe('Should be able to render PHP', () => {
afterAll(() => {
jest.restoreAllMocks();
});
test('and should log expected output to console', async () => {
await generate();
expect(spy.mock.calls.length).toEqual(1);
expect(spy.mock.calls[0]).toMatchSnapshot();
});
});
33 changes: 33 additions & 0 deletions examples/php-generate-json-serializable-preset/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {
OutputModel,
PHP_JSON_SERIALIZABLE_PRESET,
PhpGenerator
} from '../../src';

const generator: PhpGenerator = new PhpGenerator({
presets: [PHP_JSON_SERIALIZABLE_PRESET]
});
const jsonSchemaDraft7 = {
$schema: 'http://json-schema.org/draft-07/schema#',
type: 'object',
additionalProperties: false,
properties: {
email: {
type: 'string',
format: 'email'
}
}
};

export async function generate(): Promise<void> {
const models: OutputModel[] = await generator.generateCompleteModels(
jsonSchemaDraft7,
{}
);
for (const model of models) {
console.log(model.result);
}
}
if (require.main === module) {
generate();
}
10 changes: 10 additions & 0 deletions examples/php-generate-json-serializable-preset/package-lock.json

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

10 changes: 10 additions & 0 deletions examples/php-generate-json-serializable-preset/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"config" : { "example_name" : "php-generate-json-serializable-preset" },
"scripts": {
"install": "cd ../.. && npm i",
"start": "../../node_modules/.bin/ts-node --cwd ../../ ./examples/$npm_package_config_example_name/index.ts",
"start:windows": "..\\..\\node_modules\\.bin\\ts-node --cwd ..\\..\\ .\\examples\\%npm_package_config_example_name%\\index.ts",
"test": "../../node_modules/.bin/jest --config=../../jest.config.js ./examples/$npm_package_config_example_name/index.spec.ts",
"test:windows": "..\\..\\node_modules\\.bin\\jest --config=..\\..\\jest.config.js examples/%npm_package_config_example_name%/index.spec.ts"
}
}
4 changes: 2 additions & 2 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@asyncapi/modelina",
"version": "1.8.12",
"version": "1.9.0",
"description": "Library for generating data models based on inputs such as AsyncAPI, OpenAPI, or JSON Schema documents",
"license": "Apache-2.0",
"homepage": "https://www.modelina.org",
Expand Down Expand Up @@ -100,6 +100,8 @@
"test:blackbox:java": "cross-env CI=true jest ./test/blackbox/blackbox-java.spec.ts",
"test:runtime:java": "cross-env CI=true jest ./test/runtime/runtime-java.spec.ts",
"generate:runtime:java": "cross-env CI=true ts-node ./test/runtime/runtime-java.ts",
"test:runtime:go": "cross-env CI=true jest ./test/runtime/runtime-go.spec.ts",
"generate:runtime:go": "cross-env CI=true ts-node ./test/runtime/runtime-go.ts",
"test:runtime:cplusplus": "cross-env CI=true jest ./test/runtime/runtime-cplusplus.spec.ts",
"generate:runtime:cplusplus": "cross-env CI=true ts-node ./test/runtime/runtime-cplusplus.ts",
"test:runtime:kotlin": "cross-env CI=true jest ./test/runtime/runtime-kotlin.spec.ts",
Expand Down
91 changes: 91 additions & 0 deletions src/generators/php/presets/JsonSerializablePreset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { PhpPreset } from '../PhpPreset';
import { PhpRenderer } from '../PhpRenderer';
import {
ConstrainedDictionaryModel,
ConstrainedMetaModel
} from '../../../models';

function renderSelf({
content
}: {
content: string;
renderer: PhpRenderer<ConstrainedMetaModel>;
}): string {
const contentLines = content.split('\n');
contentLines[0] += ` implements \\JsonSerializable`;

return contentLines.join('\n');
}

/**
* Preset, which implements PHP’s JsonSerializable interface.
*
* Using this will allow to json serialize the model using `json_encode()`.
*
* @implements {PhpPreset}
*/
export const PHP_JSON_SERIALIZABLE_PRESET: PhpPreset = {
class: {
self({ content, renderer }): string {
return renderSelf({ content, renderer });
},
additionalContent({ renderer, model, content }): string {
const serializedProperties = Object.values(model.properties).map(
(property) => {
if (
property.property instanceof ConstrainedDictionaryModel &&
property.property.serializationType === 'unwrap'
) {
return `...$this->${property.propertyName},`;
}

return `'${property.unconstrainedPropertyName}' => $this->${property.propertyName},`;
}
);

return (
content +
renderer.renderBlock([
'public function jsonSerialize(): array',
'{',
renderer.indent(
renderer.renderBlock([
'return [',
renderer.indent(renderer.renderBlock(serializedProperties)),
'];'
])
),
'}'
])
);
}
},
enum: {
self({ content, renderer }): string {
return renderSelf({ content, renderer });
},
additionalContent({ content, model, renderer }) {
return (
content +
renderer.renderBlock([
`public function jsonSerialize(): mixed`,
'{',
renderer.indent(
renderer.renderBlock([
'return match($this) {',
renderer.indent(
renderer.renderBlock(
Object.values(model.values).map(
(value) => `self::${value.key} => ${value.value},`
)
)
),
'};'
])
),
'}'
])
);
}
}
};
1 change: 1 addition & 0 deletions src/generators/php/presets/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './DescriptionPreset';
export * from './JsonSerializablePreset';
Loading

0 comments on commit 2a13e08

Please sign in to comment.