forked from asyncapi/modelina
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Revert "feat: fixed data tags, description preset for Go, add…
… goIncludeComments and goIncludeTags flags (asyncapi#2123)"" This reverts commit 83217e2.
- Loading branch information
1 parent
9ea7c23
commit 17c25ab
Showing
25 changed files
with
451 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Go Data Models from AsyncAPI | ||
|
||
A basic example of how to use Modelina and output a Go data model from AsyncAPI, including data tags and comments from description. | ||
|
||
## 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 | ||
``` |
27 changes: 27 additions & 0 deletions
27
examples/generate-go-asyncapi-comments/__snapshots__/index.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Should be able to render Go Models and should log expected output to console 1`] = ` | ||
Array [ | ||
"// Payload for updating stock information | ||
type StockUpdatePayload struct { | ||
ProductId string \`json:\\"productId\\" binding:\\"required\\"\` | ||
// The updated quantity of the product | ||
Quantity int \`json:\\"quantity,omitempty\\"\` | ||
// Warehouse location of the product | ||
Location string \`json:\\"location,omitempty\\"\` | ||
}", | ||
] | ||
`; | ||
exports[`Should be able to render Go Models and should log expected output to console 2`] = ` | ||
Array [ | ||
"// Payload for low stock alerts | ||
type LowStockPayload struct { | ||
ProductId string \`json:\\"productId\\" binding:\\"required\\"\` | ||
// The stock level threshold | ||
Threshold int \`json:\\"threshold\\" binding:\\"required\\"\` | ||
// The current stock level | ||
CurrentStock int \`json:\\"currentStock,omitempty\\"\` | ||
}", | ||
] | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const spy = jest.spyOn(global.console, 'log').mockImplementation(() => { | ||
return; | ||
}); | ||
import { generate } from './index'; | ||
|
||
describe('Should be able to render Go Models', () => { | ||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
test('and should log expected output to console', async () => { | ||
await generate(); | ||
expect(spy.mock.calls.length).toEqual(2); | ||
expect(spy.mock.calls[0]).toMatchSnapshot(); | ||
expect(spy.mock.calls[1]).toMatchSnapshot(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { | ||
GoGenerator, | ||
GO_DESCRIPTION_PRESET, | ||
GO_COMMON_PRESET, | ||
GoCommonPresetOptions | ||
} from '../../src'; | ||
|
||
const options: GoCommonPresetOptions = { addJsonTag: true }; | ||
const generator = new GoGenerator({ | ||
presets: [GO_DESCRIPTION_PRESET, { preset: GO_COMMON_PRESET, options }] | ||
}); | ||
|
||
const asyncAPIDocument = { | ||
asyncapi: '3.0.0', | ||
info: { | ||
title: 'inventoryService', | ||
version: '2.1.0' | ||
}, | ||
channels: { | ||
inventory: { | ||
address: '/inventory', | ||
messages: { | ||
updateStock: { | ||
summary: 'Update stock levels', | ||
payload: { | ||
title: 'stockUpdatePayload', | ||
type: 'object', | ||
description: 'Payload for updating stock information', | ||
required: ['productId'], | ||
additionalProperties: false, | ||
properties: { | ||
productId: { | ||
type: 'string' | ||
}, | ||
quantity: { | ||
type: 'integer', | ||
description: 'The updated quantity of the product' | ||
}, | ||
location: { | ||
type: 'string', | ||
description: 'Warehouse location of the product' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
alerts: { | ||
address: '/alerts', | ||
messages: { | ||
lowStockAlert: { | ||
summary: 'Low stock level alert', | ||
payload: { | ||
title: 'lowStockPayload', | ||
type: 'object', | ||
description: 'Payload for low stock alerts', | ||
required: ['productId', 'threshold'], | ||
additionalProperties: false, | ||
properties: { | ||
productId: { | ||
type: 'string' | ||
}, | ||
threshold: { | ||
type: 'integer', | ||
description: 'The stock level threshold' | ||
}, | ||
currentStock: { | ||
type: 'integer', | ||
description: 'The current stock level' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
operations: { | ||
updateInventory: { | ||
title: 'Update Inventory Operation', | ||
summary: 'Operation to update inventory stock levels', | ||
channel: { | ||
$ref: '#/channels/inventory' | ||
}, | ||
action: 'send', | ||
messages: [ | ||
{ | ||
$ref: '#/channels/inventory/messages/updateStock' | ||
} | ||
] | ||
}, | ||
notifyLowStock: { | ||
title: 'Notify Low Stock Operation', | ||
summary: 'Operation to notify when stock is low', | ||
channel: { | ||
$ref: '#/channels/alerts' | ||
}, | ||
action: 'receive', | ||
messages: [ | ||
{ | ||
$ref: '#/channels/alerts/messages/lowStockAlert' | ||
} | ||
] | ||
} | ||
} | ||
}; | ||
|
||
export async function generate(): Promise<void> { | ||
const models = await generator.generate(asyncAPIDocument); | ||
for (const model of models) { | ||
console.log(model.result); | ||
} | ||
} | ||
|
||
if (require.main === module) { | ||
generate(); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"config": { | ||
"example_name": "generate-go-asyncapi-comments" | ||
}, | ||
"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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
/lib | ||
/tmp | ||
/scripts | ||
/test/helpers/init.js | ||
/test/helpers/init.js | ||
/test/fixtures/generate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.