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

Unable to compile CSharp code with models containing reserved keywords #31

Open
ragi-dayananda opened this issue Jun 1, 2023 · 4 comments

Comments

@ragi-dayananda
Copy link
Contributor

Context

The generated C# code cannot be compiled when models contain any reserved .NET keywords.
Eg, 'fixed', 'void', 'event', 'virtual' etc.. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/

enum invoice_amount_type {
   o fixed
   o maximum
}

enum issuing_card_type {
   o physical
   o virtual
}

enum invoice_status {
   o deleted
   o draft
   o open
   o paid
   o uncollectible
   o void
}

Expected Behavior

Any generated csharp code should be compilable.

Possible Fix

We can add a trailing underscore char to the reserved keyword.

Steps to Reproduce

Generate cshar code for any of the model provided in the above example. Add the generated models to any csharp project and try t build the project.

Your Environment

  • Version used: 3.11.1
  • Operating System and version (desktop or mobile): windows 11 desktop
@ragi-dayananda ragi-dayananda changed the title Unable to Compile C# Code with Models Containing Reserved Keywords Unable to compile CSharp code with models containing reserved keywords Jun 1, 2023
@dselman
Copy link
Sponsor Contributor

dselman commented Jun 1, 2023

We've various escaping functions implemented for the different code gen target languages. It would be great to have a generic function that all targets could use that was configured with a list of reserved key words / identifier syntax so we can remove some of this duplication and improve quality.

1 similar comment
@dselman
Copy link
Sponsor Contributor

dselman commented Jun 1, 2023

We've various escaping functions implemented for the different code gen target languages. It would be great to have a generic function that all targets could use that was configured with a list of reserved key words / identifier syntax so we can remove some of this duplication and improve quality.

@subhajit20
Copy link
Contributor

subhajit20 commented Mar 12, 2024

hey @dselman it already has a function called toCSharpIdentifier which detect if the keyword is reserved.
@dselman Am I right?

@apoorvsxna
Copy link
Contributor

apoorvsxna commented Mar 22, 2024

Hey there @ragi-dayananda @dselman . Just looked through the csharpvisitor.js file and found that the suggested fix is already implemented.

I think the model being used in the issue example has incorrect syntax. Models should include a namespace declaration at the beginning, which is absent in the example.

On adding a namespace decalaration to the given model,

namespace org.example

enum invoice_amount_type {
   o fixed
   o maximum
}

enum issuing_card_type {
   o physical
   o virtual
}

enum invoice_status {
   o deleted
   o draft
   o open
   o paid
   o uncollectible
   o void
}

concept YourConcept {
  o String myField
}

it compiles just fine.

Screenshot (92)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants