Skip to content

Commit 3272a05

Browse files
jonaslagoniTertiumOrganum1Владислав Муранов
authored
feat: fixed data tags, description preset for Go, add goIncludeComments and goIncludeTags flags (#2152)
Co-authored-by: TertiumOrganum1 <51286827+TertiumOrganum1@users.noreply.github.com> Co-authored-by: Владислав Муранов <v.muranov@sirena2000.ru>
1 parent df532bd commit 3272a05

File tree

22 files changed

+423
-110
lines changed

22 files changed

+423
-110
lines changed

docs/languages/Go.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ There are special use-cases that each language supports; this document pertains
44
<!-- toc is generated with GitHub Actions do not remove toc markers -->
55
<!-- toc -->
66

7-
- [Generate serializer and deserializer functionality](#generate-serializer-and-deserializer-functionality)
8-
* [To and from JSON](#to-and-from-json)
9-
* [To and from XML](#to-and-from-xml)
10-
* [To and from binary](#to-and-from-binary)
7+
- [Go](#go)
8+
- [Generate serializer and deserializer functionality](#generate-serializer-and-deserializer-functionality)
9+
- [To and from JSON](#to-and-from-json)
10+
- [JSON Tags](#json-tags)
11+
- [To and from XML](#to-and-from-xml)
12+
- [To and from binary](#to-and-from-binary)
13+
- [Rendering comments from description and example fields](#rendering-comments-from-description-and-example-fields)
1114

1215
<!-- tocstop -->
1316

@@ -24,7 +27,7 @@ Here are all the supported presets and the libraries they use for converting to
2427

2528
#### JSON Tags
2629

27-
To generate go models that work correctly with JSON marshal functions we need to generate appropriate JSON `struct-tags`, use the preset `GO_COMMON_PRESET` and provide the option `addJsonTag: true`.
30+
To generate go models that work correctly with JSON marshal functions we need to generate appropriate JSON `struct-tags`, use the preset `GO_COMMON_PRESET` and provide the option `addJsonTag: true` (added in CLI by default).
2831

2932
check out this [example for a live demonstration](../../examples/go-json-tags/)
3033

@@ -33,3 +36,9 @@ Currently not supported, [let everyone know you need it](https://github.com/asyn
3336

3437
### To and from binary
3538
Currently not supported, [let everyone know you need it](https://github.com/asyncapi/modelina/issues/new?assignees=&labels=enhancement&template=enhancement.md)!
39+
40+
## Rendering comments from description and example fields
41+
42+
You can use the `GO_DESCRIPTION_PRESET` to generate comments from description fields in your model.
43+
44+
See [this example](../../examples/generate-go-asyncapi-comments) for how this can be used.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Go Data Models from AsyncAPI
2+
3+
A basic example of how to use Modelina and output a Go data model from AsyncAPI, including data tags and comments from description.
4+
5+
## How to run this example
6+
7+
Run this example using:
8+
9+
```sh
10+
npm i && npm run start
11+
```
12+
13+
If you are on Windows, use the `start:windows` script instead:
14+
15+
```sh
16+
npm i && npm run start:windows
17+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Should be able to render Go Models and should log expected output to console 1`] = `
4+
Array [
5+
"// Payload for updating stock information
6+
type StockUpdatePayload struct {
7+
ProductId string \`json:\\"productId\\" binding:\\"required\\"\`
8+
// The updated quantity of the product
9+
Quantity int \`json:\\"quantity,omitempty\\"\`
10+
// Warehouse location of the product
11+
Location string \`json:\\"location,omitempty\\"\`
12+
}",
13+
]
14+
`;
15+
16+
exports[`Should be able to render Go Models and should log expected output to console 2`] = `
17+
Array [
18+
"// Payload for low stock alerts
19+
type LowStockPayload struct {
20+
ProductId string \`json:\\"productId\\" binding:\\"required\\"\`
21+
// The stock level threshold
22+
Threshold int \`json:\\"threshold\\" binding:\\"required\\"\`
23+
// The current stock level
24+
CurrentStock int \`json:\\"currentStock,omitempty\\"\`
25+
}",
26+
]
27+
`;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const spy = jest.spyOn(global.console, 'log').mockImplementation(() => {
2+
return;
3+
});
4+
import { generate } from './index';
5+
6+
describe('Should be able to render Go Models', () => {
7+
afterAll(() => {
8+
jest.restoreAllMocks();
9+
});
10+
test('and should log expected output to console', async () => {
11+
await generate();
12+
expect(spy.mock.calls.length).toEqual(2);
13+
expect(spy.mock.calls[0]).toMatchSnapshot();
14+
expect(spy.mock.calls[1]).toMatchSnapshot();
15+
});
16+
});
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import {
2+
GoGenerator,
3+
GO_DESCRIPTION_PRESET,
4+
GO_COMMON_PRESET,
5+
GoCommonPresetOptions
6+
} from '../../src';
7+
8+
const options: GoCommonPresetOptions = { addJsonTag: true };
9+
const generator = new GoGenerator({
10+
presets: [GO_DESCRIPTION_PRESET, { preset: GO_COMMON_PRESET, options }]
11+
});
12+
13+
const asyncAPIDocument = {
14+
asyncapi: '3.0.0',
15+
info: {
16+
title: 'inventoryService',
17+
version: '2.1.0'
18+
},
19+
channels: {
20+
inventory: {
21+
address: '/inventory',
22+
messages: {
23+
updateStock: {
24+
summary: 'Update stock levels',
25+
payload: {
26+
title: 'stockUpdatePayload',
27+
type: 'object',
28+
description: 'Payload for updating stock information',
29+
required: ['productId'],
30+
additionalProperties: false,
31+
properties: {
32+
productId: {
33+
type: 'string'
34+
},
35+
quantity: {
36+
type: 'integer',
37+
description: 'The updated quantity of the product'
38+
},
39+
location: {
40+
type: 'string',
41+
description: 'Warehouse location of the product'
42+
}
43+
}
44+
}
45+
}
46+
}
47+
},
48+
alerts: {
49+
address: '/alerts',
50+
messages: {
51+
lowStockAlert: {
52+
summary: 'Low stock level alert',
53+
payload: {
54+
title: 'lowStockPayload',
55+
type: 'object',
56+
description: 'Payload for low stock alerts',
57+
required: ['productId', 'threshold'],
58+
additionalProperties: false,
59+
properties: {
60+
productId: {
61+
type: 'string'
62+
},
63+
threshold: {
64+
type: 'integer',
65+
description: 'The stock level threshold'
66+
},
67+
currentStock: {
68+
type: 'integer',
69+
description: 'The current stock level'
70+
}
71+
}
72+
}
73+
}
74+
}
75+
}
76+
},
77+
operations: {
78+
updateInventory: {
79+
title: 'Update Inventory Operation',
80+
summary: 'Operation to update inventory stock levels',
81+
channel: {
82+
$ref: '#/channels/inventory'
83+
},
84+
action: 'send',
85+
messages: [
86+
{
87+
$ref: '#/channels/inventory/messages/updateStock'
88+
}
89+
]
90+
},
91+
notifyLowStock: {
92+
title: 'Notify Low Stock Operation',
93+
summary: 'Operation to notify when stock is low',
94+
channel: {
95+
$ref: '#/channels/alerts'
96+
},
97+
action: 'receive',
98+
messages: [
99+
{
100+
$ref: '#/channels/alerts/messages/lowStockAlert'
101+
}
102+
]
103+
}
104+
}
105+
};
106+
107+
export async function generate(): Promise<void> {
108+
const models = await generator.generate(asyncAPIDocument);
109+
for (const model of models) {
110+
console.log(model.result);
111+
}
112+
}
113+
114+
if (require.main === module) {
115+
generate();
116+
}

examples/generate-go-asyncapi-comments/package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"config": {
3+
"example_name": "generate-go-asyncapi-comments"
4+
},
5+
"scripts": {
6+
"install": "cd ../.. && npm i",
7+
"start": "../../node_modules/.bin/ts-node --cwd ../../ ./examples/$npm_package_config_example_name/index.ts",
8+
"start:windows": "..\\..\\node_modules\\.bin\\ts-node --cwd ..\\..\\ .\\examples\\%npm_package_config_example_name%\\index.ts",
9+
"test": "../../node_modules/.bin/jest --config=../../jest.config.js ./examples/$npm_package_config_example_name/index.spec.ts",
10+
"test:windows": "..\\..\\node_modules\\.bin\\jest --config=..\\..\\jest.config.js examples/%npm_package_config_example_name%/index.spec.ts"
11+
}
12+
}

examples/generate-go-enums/__snapshots__/index.spec.ts.snap

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
exports[`Should be able to render Go Enums and should log expected output to console 1`] = `
44
Array [
5-
"// Root represents a Root model.
6-
type Root struct {
5+
"type Root struct {
76
Cities *Cities
87
Options *Options
98
}",
@@ -12,8 +11,7 @@ type Root struct {
1211

1312
exports[`Should be able to render Go Enums and should log expected output to console 2`] = `
1413
Array [
15-
"// Cities represents an enum of Cities.
16-
type Cities uint
14+
"type Cities uint
1715
1816
const (
1917
CitiesLondon Cities = iota
@@ -41,8 +39,7 @@ var ValuesToCities = map[any]Cities{
4139

4240
exports[`Should be able to render Go Enums and should log expected output to console 3`] = `
4341
Array [
44-
"// Options represents an enum of Options.
45-
type Options uint
42+
"type Options uint
4643
4744
const (
4845
OptionsNumber_123 Options = iota

examples/generate-go-models/__snapshots__/index.spec.ts.snap

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
exports[`Should be able to render Go Models and should log expected output to console 1`] = `
44
Array [
5-
"// Root represents a Root model.
6-
type Root struct {
5+
"type Root struct {
76
Email string
87
}",
98
]

examples/go-json-tags/__snapshots__/index.spec.ts.snap

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
exports[`Should be able to render json-tags in struct and should log expected output to console 1`] = `
44
Array [
5-
"// Root represents a Root model.
6-
type Root struct {
5+
"type Root struct {
76
Cities *Cities \`json:\\"cities,omitempty\\"\`
87
Options *Options \`json:\\"options,omitempty\\"\`
98
}",
@@ -12,8 +11,7 @@ type Root struct {
1211
1312
exports[`Should be able to render json-tags in struct and should log expected output to console 2`] = `
1413
Array [
15-
"// Cities represents an enum of Cities.
16-
type Cities uint
14+
"type Cities uint
1715
1816
const (
1917
CitiesLondon Cities = iota
@@ -54,8 +52,7 @@ func (op Cities) MarshalJSON() ([]byte, error) {
5452
5553
exports[`Should be able to render json-tags in struct and should log expected output to console 3`] = `
5654
Array [
57-
"// Options represents an enum of Options.
58-
type Options uint
55+
"type Options uint
5956
6057
const (
6158
OptionsNumber_123 Options = iota

0 commit comments

Comments
 (0)