Skip to content
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

Improve the code generator functions #1338

Closed
kaushik-rishi opened this issue May 29, 2023 · 8 comments
Closed

Improve the code generator functions #1338

kaushik-rishi opened this issue May 29, 2023 · 8 comments
Labels
enhancement New feature or request stale

Comments

@kaushik-rishi
Copy link
Contributor

Reason/Context

The current code generators we have under
https://github.com/asyncapi/modelina/tree/master/modelina-website/src/helpers/GeneratorCode are working based on template literals, they donot update the imports of presets, using regular expressions to replace new lines, joining presets using ',' and performing logics based on length of presets array IMHO is degrading the quality and understandability of the code (although it's very readable).

Description

Please try answering few of those questions

  • What changes have to be introduced?
    • We must update the imports as well
    • Use a better methodology to generate the javascript generator code which could eliminate the usage of regular expressions and ad-hoc array length based logic and worry about tab spacing and new line characters.
  • Will this be a breaking change? No
  • How could it be implemented/designed?
    • Add imports as well to the code generator
    • We can use a JS formatter API and make the code generation more cleaner
      In place of this, we could have better code looking like this
import { ModelinaCSharpOptions } from '../../types';
import { format } from 'prettier/standalone';
import parserBabel from 'prettier/parser-babel';

export function getCSharpGeneratorCode(
  generatorOptions: ModelinaCSharpOptions
) {
  const optionString: string[] = [];
  const optionStringPresets: string[] = [];
  const importsString: string[] = [];

  if (generatorOptions.csharpArrayType) {
    optionString.push(`collectionType: '${generatorOptions.csharpArrayType}'`);
  }
  if (generatorOptions.csharpAutoImplemented) {
    optionString.push(`autoImplementedProperties: ${generatorOptions.csharpAutoImplemented}`);
  }
  if (generatorOptions.csharpOverwriteHashcode) {
    importsString.push('CSHARP_COMMON_PRESET')
    optionStringPresets.push(`{
      preset: CSHARP_COMMON_PRESET,
      options: {
        equal: false,
        hashCode: true
      }
    }`)
  }

  if (generatorOptions.csharpIncludeJson) {
    importsString.push('CSHARP_JSON_SERIALIZER_PRESET')
    optionStringPresets.push('CSHARP_JSON_SERIALIZER_PRESET')
  }
  if (generatorOptions.csharpIncludeNewtonsoft) {
    importsString.push('CSHARP_NEWTONSOFT_SERIALIZER_PRESET')
    optionStringPresets.push('CSHARP_NEWTONSOFT_SERIALIZER_PRESET')
  }

  const presetOptions = optionStringPresets.length > 0 ? `presets: [${optionStringPresets.join(',')}]` : '';
  let fullOptions = '';
  if (optionStringPresets.length > 0 || optionString.length > 0) {
    fullOptions = `{ ${optionString.join(',')} , ${presetOptions} }`;
  }

  return format(`// Use the following code as starting point
    // To generate the models exactly as displayed in the playground
    import { CSharpGenerator, ${importsString.length ? importsString.join(',') : ''} } from '@asyncapi/modelina';

    const generator = new CSharpGenerator(${fullOptions});`, 
    {
      semi: true,
      trailingComma: "none",
      parser: "babel",
      plugins: [parserBabel],
    }
  );
}
  • where the imports are working, as well as the formatting is taken care of without using tab spaces, new lines and regular expressions replace functions
@kaushik-rishi kaushik-rishi added the enhancement New feature or request label May 29, 2023
@github-actions
Copy link
Contributor

Welcome to AsyncAPI. Thanks a lot for reporting your first issue. Please check out our contributors guide and the instructions about a basic recommended setup useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

@kaushik-rishi
Copy link
Contributor Author

I specifically took the example of csharp because it has a lot of options and i've worked on adding a couple of them.

@kaushik-rishi
Copy link
Contributor Author

@jonaslagoni Please let me know if this is a bad idea 🙂
Happy to take constructive criticism.

@kaushik-rishi
Copy link
Contributor Author

@jonaslagoni
Copy link
Member

#1309 actually clean up a lot of the duplicate code and structure.

Generally, this code never changes so adding a new library just to replace 20 lines of code that rarely changes, does not seem that relevant IMO 🙂

But it's also hard to see the full extent that it would simplify, so feel free to create a draft PR proposing the change if you feel it's a needed change 👍

@kaushik-rishi
Copy link
Contributor Author

kaushik-rishi commented May 31, 2023

@jonaslagoni
Pretty Hefty PR 😄 (#1309)

Gained some knowledge by reading the code changes.

I'll brainstorm again, i'm thinking if it's a neccessary change or not 🤔

Also, I'll probably open another issue, regarding adding the imports in Code generator, they're missing right there.

@kaushik-rishi kaushik-rishi changed the title Imrove the code generator functions Improve the code generator functions Jun 7, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Oct 6, 2023

This issue has been automatically marked as stale because it has not had recent activity 😴

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience ❤️

@github-actions github-actions bot added the stale label Oct 6, 2023
@jonaslagoni
Copy link
Member

jonaslagoni commented Jan 2, 2024

Closing as no further followup and it seemed to have already been fixed 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request stale
Projects
None yet
Development

No branches or pull requests

2 participants