-
-
Notifications
You must be signed in to change notification settings - Fork 362
refactor: move RegisterMessageHandler and MethodGenerator component to @asyncapi/generator-components
#1663
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,79 @@ | ||
| import { Text } from '@asyncapi/generator-react-sdk'; | ||
|
|
||
| /** | ||
| * @typedef {'python' | 'javascript' | 'dart'} Language | ||
| * Supported programming languages. | ||
| */ | ||
|
|
||
| /** | ||
| * Configuration for method syntax based on programming language. | ||
| * @type {Record<Language, { returnType?: string, openingTag?: string, closingTag?: string, indentSize?: number }>} | ||
| */ | ||
| const defaultMethodConfig = { | ||
| python: { returnType: 'def', openingTag: ':', indentSize: 2 }, | ||
| javascript: { openingTag: '{', closingTag: '}', indentSize: 2 }, | ||
| dart: { returnType: 'void', openingTag: '{', closingTag: '}', indentSize: 2 } | ||
| }; | ||
|
|
||
| /** | ||
| * Generic Method rendering component. | ||
| * | ||
| * @param {Object} props - Component props. | ||
| * @param {Language} props.language - Programming language used for method formatting. | ||
| * @param {string} props.methodName - Name of the method. | ||
| * @param {string[]} [props.methodParams=[]] - Method parameters. | ||
| * @param {string} [props.methodDocs=''] - Optional documentation string. | ||
| * @param {string} [props.methodLogic=''] - Core method logic. | ||
| * @param {string} [props.preExecutionCode=''] - Code before main logic. | ||
| * @param {string} [props.postExecutionCode=''] - Code after main logic. | ||
| * @param {number} [props.indent=2] - Indentation for the method block. | ||
| * @param {number} [props.newLines=1] - Number of new lines after method. | ||
| * @param {{ returnType?: string, openingTag?: string, closingTag?: string, indentSize?: number }} [props.customMethodConfig] - Optional custom syntax configuration for the current language. | ||
| */ | ||
| export function MethodGenerator({ | ||
| language, | ||
| methodName, | ||
| methodParams = [], | ||
| methodDocs = '', | ||
| methodLogic = '', | ||
| preExecutionCode = '', | ||
| postExecutionCode = '', | ||
| indent = 2, | ||
| newLines = 1, | ||
| customMethodConfig | ||
| }) { | ||
| const { | ||
| returnType = '', | ||
| openingTag = '', | ||
| closingTag = '', | ||
| indentSize = 2 | ||
| } = customMethodConfig || defaultMethodConfig[language]; | ||
|
|
||
Adi-204 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const params = methodParams.join(', '); | ||
|
|
||
| let completeCode = methodLogic; | ||
|
|
||
| if (preExecutionCode) { | ||
| completeCode = `${preExecutionCode}\n${completeCode}`; | ||
| } | ||
| if (postExecutionCode) { | ||
| completeCode = `${completeCode}\n${postExecutionCode}`; | ||
| } | ||
|
|
||
| const innerIndent = ' '.repeat(indentSize); | ||
| const indentedLogic = completeCode | ||
| .split('\n') | ||
| .map(line => (line ? `${innerIndent}${line}` : '')) | ||
| .join('\n'); | ||
|
|
||
| const methodCode = `${methodDocs} | ||
| ${returnType} ${methodName}(${params}) ${openingTag} | ||
| ${indentedLogic} | ||
| ${closingTag}`; | ||
|
|
||
| return ( | ||
| <Text indent={indent} newLines={newLines}> | ||
| {methodCode} | ||
| </Text> | ||
| ); | ||
| } | ||
Adi-204 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
63 changes: 63 additions & 0 deletions
63
packages/components/src/components/RegisterMessageHandler.js
This file contains hidden or 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,63 @@ | ||
| import { MethodGenerator } from './MethodGenerator'; | ||
|
|
||
| /** | ||
| * @typedef {'python' | 'javascript' | 'dart'} Language | ||
| * Supported programming languages. | ||
| */ | ||
|
|
||
| /** | ||
| * Configuration for WebSocket message handler registration method logic per language. | ||
| * @type {Record<Language, { methodDocs?: string, methodLogic: string }>} | ||
| */ | ||
| const websocketMessageRegisterConfig = { | ||
| python: { | ||
| methodLogic: `if callable(handler): | ||
| self.message_handlers.append(handler) | ||
| else: | ||
| print("Message handler must be callable")` | ||
| }, | ||
Adi-204 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| javascript: { | ||
| methodDocs: '// Method to register custom message handlers', | ||
| methodLogic: `if (typeof handler === 'function') { | ||
| this.messageHandlers.push(handler); | ||
| } else { | ||
| console.warn('Message handler must be a function'); | ||
| }` | ||
| }, | ||
| dart: { | ||
| methodDocs: '/// Method to register custom message handlers', | ||
| methodLogic: '_messageHandlers.add(handler);' | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Renders a WebSocket message handler registration method with optional pre and post execution logic. | ||
| * | ||
| * @param {Object} props - Component props. | ||
| * @param {Language} props.language - Programming language used for method formatting. | ||
| * @param {string} props.methodName='registerMessageHandler' - Name of the method to generate. | ||
| * @param {string[]} props.methodParams=[] - List of parameters for the method. | ||
| * @param {string} props.preExecutionCode - Code to insert before the main function logic. | ||
| * @param {string} props.postExecutionCode - Code to insert after the main function logic. | ||
| * @returns {JSX.Element} Rendered method block with appropriate formatting. | ||
| */ | ||
| export function RegisterMessageHandler({ language, methodName = 'registerMessageHandler', methodParams = [], preExecutionCode = '', postExecutionCode = '' }) { | ||
| const { | ||
| methodDocs = '', | ||
| methodLogic = '' | ||
| } = websocketMessageRegisterConfig[language]; | ||
|
|
||
| return ( | ||
| <MethodGenerator | ||
| language={language} | ||
| methodName={methodName} | ||
| methodParams={methodParams} | ||
| methodDocs={methodDocs} | ||
| methodLogic={methodLogic} | ||
| preExecutionCode={preExecutionCode} | ||
| postExecutionCode={postExecutionCode} | ||
| indent={2} | ||
| newLines={2} | ||
| /> | ||
| ); | ||
| } | ||
This file contains hidden or 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,6 @@ | ||
| export { Models } from './components/Models'; | ||
| export { FileHeaderInfo } from './components/FileHeaderInfo'; | ||
| export { CloseConnection } from './components/CloseConnection'; | ||
| export { DependencyProvider } from './components/DependencyProvider'; | ||
| export { DependencyProvider } from './components/DependencyProvider'; | ||
| export { RegisterMessageHandler } from './components/RegisterMessageHandler'; | ||
| export { MethodGenerator } from './components/MethodGenerator'; |
111 changes: 111 additions & 0 deletions
111
packages/components/test/components/MethodGenerator.test.js
This file contains hidden or 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,111 @@ | ||
| import { render } from '@asyncapi/generator-react-sdk'; | ||
| import { MethodGenerator } from '../../src/index'; | ||
|
|
||
| describe('MethodGenerator', () => { | ||
| test('renders default python method', () => { | ||
| const result = render( | ||
| <MethodGenerator | ||
| language="python" | ||
| methodName="connect" | ||
| methodParams={['self', 'url']} | ||
| methodLogic="print('Connecting...')" | ||
| /> | ||
| ); | ||
| expect(result.trim()).toMatchSnapshot(); | ||
| }); | ||
Adi-204 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| test('renders default javascript method', () => { | ||
| const result = render( | ||
| <MethodGenerator | ||
| language="javascript" | ||
| methodName="connect" | ||
| methodParams={['url']} | ||
| methodLogic="console.log('Connecting...');" | ||
| /> | ||
| ); | ||
| expect(result.trim()).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| test('renders default dart method', () => { | ||
| const result = render( | ||
| <MethodGenerator | ||
| language="dart" | ||
| methodName="connect" | ||
| methodParams={['String url']} | ||
| methodLogic="print('Connecting...');" | ||
| /> | ||
| ); | ||
| expect(result.trim()).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| test('renders with customMethodConfig override', () => { | ||
| const result = render( | ||
| <MethodGenerator | ||
| language="javascript" | ||
| customMethodConfig={{ returnType: 'async function', openingTag: '{', closingTag: '}', indentSize: 4 }} | ||
| methodName="fetchData" | ||
| methodLogic="await getData();" | ||
| /> | ||
| ); | ||
| expect(result.trim()).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| test('renders with methodDocs', () => { | ||
| const result = render( | ||
| <MethodGenerator | ||
| language="python" | ||
| methodName="processData" | ||
| methodDocs='"""Process the input data."""' | ||
| methodLogic="pass" | ||
| /> | ||
| ); | ||
| expect(result.trim()).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| test('renders with preExecutionCode and postExecutionCode', () => { | ||
| const result = render( | ||
| <MethodGenerator | ||
| language="python" | ||
| methodName="registerHandler" | ||
| methodParams={['self', 'handler']} | ||
| preExecutionCode="# Before handler registration" | ||
| methodLogic="self.handlers.append(handler)" | ||
| postExecutionCode="# After handler registration" | ||
| /> | ||
| ); | ||
| expect(result.trim()).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| test('renders with custom indentation', () => { | ||
| const result = render( | ||
| <MethodGenerator | ||
| language="javascript" | ||
| methodName="customIndent" | ||
| methodLogic="console.log('Indented');" | ||
| customMethodConfig={{ openingTag: '{', closingTag: '}', indentSize: 6 }} | ||
| /> | ||
| ); | ||
| expect(result.trim()).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| test('renders method with no logic', () => { | ||
| const result = render( | ||
| <MethodGenerator | ||
| language="dart" | ||
| methodName="emptyMethod" | ||
| /> | ||
| ); | ||
| expect(result.trim()).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| test('renders method with multiline logic', () => { | ||
| const result = render( | ||
| <MethodGenerator | ||
| language="javascript" | ||
| methodName="multiLineLogic" | ||
| methodLogic={'console.log(\'Line 1\');\nconsole.log(\'Line 2\');'} | ||
| /> | ||
| ); | ||
| expect(result.trim()).toMatchSnapshot(); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.