Skip to content

Commit

Permalink
feat golang: moved test objects from string literal to object
Browse files Browse the repository at this point in the history
  • Loading branch information
Владислав Муранов committed Nov 5, 2024
1 parent b95d20f commit bae0c8c
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 112 deletions.
169 changes: 95 additions & 74 deletions examples/generate-go-asyncapi-comments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,87 +4,108 @@ import {
GO_COMMON_PRESET,
GoCommonPresetOptions
} from '../../src';
import { Parser } from '@asyncapi/parser';

const options: GoCommonPresetOptions = { addJsonTag: true };
const generator = new GoGenerator({
presets: [GO_DESCRIPTION_PRESET, { preset: GO_COMMON_PRESET, options }]
});

const AsyncAPIDocumentText = `
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'
`;


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 parser = new Parser();
const { document } = await parser.parse(AsyncAPIDocumentText);
const models = await generator.generate(document);
const models = await generator.generate(asyncAPIDocument);
for (const model of models) {
console.log(model.result);
}
Expand Down
89 changes: 51 additions & 38 deletions test/generators/go/GoGenerator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
GO_COMMON_PRESET,
GoCommonPresetOptions
} from '../../../src/generators';
import { Parser } from '@asyncapi/parser';

describe('GoGenerator', () => {
let generator: GoGenerator;
Expand Down Expand Up @@ -52,47 +51,61 @@ describe('GoGenerator', () => {
});

test('should render `required` for required properties', async () => {
const AsyncAPIDocumentText = `
asyncapi: 3.0.0
info:
title: example
version: 0.0.1
channels:
root:
address: /
messages:
exampleRequest:
summary: example operation
payload:
title: exampleStruct
type: object
required:
- msgUid
additionalProperties: false
properties:
id:
type: integer
msgUid:
type: string
evtName:
type: string
operations:
exampleOperation:
title: This is an example operation
summary: To do something
channel:
$ref: '#/channels/root'
action: send
messages:
- $ref: '#/channels/root/messages/exampleRequest'
`;
const asyncAPIDocument = {
asyncapi: '3.0.0',
info: {
title: 'example',
version: '0.0.1'
},
channels: {
root: {
address: '/',
messages: {
exampleRequest: {
summary: 'example operation',
payload: {
title: 'exampleStruct',
type: 'object',
required: ['msgUid'],
additionalProperties: false,
properties: {
id: {
type: 'integer'
},
msgUid: {
type: 'string'
},
evtName: {
type: 'string'
}
}
}
}
}
}
},
operations: {
exampleOperation: {
title: 'This is an example operation',
summary: 'To do something',
channel: {
$ref: '#/channels/root'
},
action: 'send',
messages: [
{
$ref: '#/channels/root/messages/exampleRequest'
}
]
}
}
};

const options: GoCommonPresetOptions = { addJsonTag: true };
const generatorWithTags = new GoGenerator({
presets: [{ preset: GO_COMMON_PRESET, options }]
});
const parser = new Parser();
const { document } = await parser.parse(AsyncAPIDocumentText);
const models = await generatorWithTags.generate(document);
const models = await generatorWithTags.generate(asyncAPIDocument);
expect(models).toHaveLength(1);
expect(models[0].result).toMatchSnapshot();
});
Expand Down

0 comments on commit bae0c8c

Please sign in to comment.