-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
[C] fix issues on enums #5477
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
[C] fix issues on enums #5477
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e568f73
PR to solve 2 open issues on enums:
michelealbano c3fa511
PR to solve 2 open issues on enums:
michelealbano 54a95d8
Enums decorated: with {{projectName}}_{{classVarName}}_{{enumName}}_ …
michelealbano 1400a8e
Changes to the c client:
michelealbano bfdd7cf
Changes to the c client:
michelealbano 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
Next
Next commit
PR to solve 2 open issues on enums:
Issue 5091 needs to generate enums also when the enum is directly in a param to a API call, instead than in a model. I did that by copying and adapting enum code from *model* to *api* Issue 4293 needs to decorate enums, for when enum names or enum values are repeated over the yaml definition.
- Loading branch information
commit e568f73b3934109594a5f7e105efd4794516ef6f
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, the first value when the code is generated will be the actual enum value and its respective number will be 0, so if the enum is mandatory or optional in both cases the enum will be sent. But to avoid this we can make the array start with { NULL , and rest of values}. This will make NULL as 0, so when the enum is optional you can actually skip sending the enum value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same for model-body.mustache?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, same for model-body and model-header. If you can push that also it would be great.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NULL is a token and cannot be used. I am worried about using NOTHING or UNDEFINED because they could be also values in the enum.
How do you suggest to call the first "NULL" element?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are my opinions and things which I have learned while using the code generator.
you could do something like this:
typedef enum { {{projectName}}_{{classVarName}}_NULL = 0,{{#enumVars}} {{projectName}}_{{classVarName}}_{{{value}}} {{^-last}},{{/-last}}{{/enumVars}} } {{classname}}_e;In case of keywords, you have to escape them. So how it will be is like in ToString and FromString, the actual values will be present as these are sent to the destination, and these values are in string format so they will not create problems. and in model-header, where the enum is defined, it will contain escaped/prefixed values then it will not conflict with C keywords or duplicate values.
Here is an model-body code snippet
model-header snippet:
There is a reason I am using
{{projectName}}_{{classVarName}}_{{{value}}}, the codebase I have contains a lot of repeated enum values definitions and C keywords, to overcome all these issues I have prefixed everything thing(functions, enums, etc) with these prefixes.Also for your second comment, string compare will not create problems as enums in the code works as int values and if there is strcmp used anywhere in the code except FromString function then it is not good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The second comment was about using NULL, "" or "NULL", and in fact with "NULL" everything is fine.
The approach
typedef enum { {{projectName}}_{{classVarName}}_NULL = 0,{{#enumVars}} {{projectName}}_{{classVarName}}_{{{value}}} {{^-last}},{{/-last}}{{/enumVars}} } {{classname}}_e;it does not work in the use case where the values are repeated among two enums used in the same model, e.g.:
Thus, we need at least {{enumName}} in the decoration.
Then, if there is repetition between different models, and we need also {{classVarName}}.
If we put there also
{{projectName}}, an example of enum name is nowopenweathermap_api__200_SECURE_NOT_SECURE. Isn't it a little heavy? Would it be better without {{projectName}}?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be ok now. Anyway, I have not covered classes enums. I will work on that on a future PR, after somebody provides YAMLs to play against.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zhemant Is it ok the way I changed the code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michelealbano Yes it is okay. Sorry for late replies