Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

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
michelealbano committed Mar 12, 2020
commit e568f73b3934109594a5f7e105efd4794516ef6f
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,95 @@
snprintf(dst, 256, "%ld", (long int)(src));\
}while(0)

{{#operations}}
{{#operation}}
{{#allParams}}

{{#isEnum}}
// Functions for enum {{enumName}} for {{{classname}}}_{{{operationId}}}


char* {{enumName}}_ToString({{baseName}}_e {{enumName}}){
char *{{enumName}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} };
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok!

Copy link
Contributor Author

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?

Copy link
Contributor

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.

Copy link
Contributor Author

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?

Copy link
Contributor

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

char* {{classname}}_ToString({{classname}}_e {{classname}}){
char *{{classname}}Array[] =  { "NULL", {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} };
    return {{classname}}Array[{{classname}}];
}

{{classname}}_e {{classname}}_FromString(char* {{classname}}){
    int stringToReturn = 0;
    char *{{classname}}Array[] =  {"NULL",  {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} };
    size_t sizeofArray = sizeof({{classname}}Array) / sizeof({{classname}}Array[0]);
    while(stringToReturn < sizeofArray) {
        if(strcmp({{classname}}, {{classname}}Array[stringToReturn]) == 0) {
            return stringToReturn;
        }
        stringToReturn++;
    }
    return 0;
}

model-header snippet:

typedef enum  { {{projectName}}_{{classVarName}}_NULL = 0,{{#enumVars}} {{projectName}}_{{classVarName}}_{{{value}}} {{^-last}},{{/-last}}{{/enumVars}} } {{classname}}_e;

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.

Copy link
Contributor Author

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.:

  schemas:
    '200':
      title: Successful response
      type: object
      properties:
        weather:
          type: string
          description: (more info Weather condition codes)
        secure:
          type: string
          enum:
            - NOT_SECURE
            - CERTIFICATE
            - TOKEN
        secure2:
          type: string
          enum:
            - NOT_SECURE
            - CERTIFICATE
            - TOKEN

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 now openweathermap_api__200_SECURE_NOT_SECURE. Isn't it a little heavy? Would it be better without {{projectName}}?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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?

Copy link
Contributor

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

return {{enumName}}Array[{{enumName}}];
}

{{baseName}}_e {{enumName}}_FromString(char* {{enumName}}){
int stringToReturn = 0;
char *{{enumName}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} };
size_t sizeofArray = sizeof({{enumName}}Array) / sizeof({{enumName}}Array[0]);
while(stringToReturn < sizeofArray) {
if(strcmp({{enumName}}, {{enumName}}Array[stringToReturn]) == 0) {
return stringToReturn;
}
stringToReturn++;
}
return 0;
}

cJSON *{{enumName}}_convertToJSON({{baseName}}_e {{enumName}}) {
cJSON *item = cJSON_CreateObject();
{{#isString}}
if(cJSON_AddStringToObject(item, "{{{paramName}}}", {{{enumName}}}_ToString({{{enumName}}})) == NULL) {
goto fail;
}
{{/isString}}
{{#isNumeric}}
if(cJSON_AddNumberToObject(item, "{{{paramName}}}", {{{enumName}}}) == NULL) {
goto fail;
}
{{/isNumeric}}
return item;
fail:
cJSON_Delete(item);
return NULL;
}

{{baseName}}_e {{enumName}}_parseFromJSON(cJSON *{{enumName}}JSON){

{{baseName}}_e *{{enumName}} = NULL;

{{#isEnum}}
{{#isNumeric}}
cJSON *{{{enumName}}}Var = cJSON_GetObjectItemCaseSensitive({{enumName}}JSON, "{{{paramName}}}");
if(!cJSON_IsNumber({{{enumName}}}Var))
{
goto end;
}
{{/isNumeric}}
{{#isString}}
{{{baseName}}}_e {{enumName}}Variable;
cJSON *{{{enumName}}}Var = cJSON_GetObjectItemCaseSensitive({{enumName}}JSON, "{{{paramName}}}");
if(!cJSON_IsString({{{enumName}}}Var) || ({{{enumName}}}Var->valuestring == NULL)){
goto end;
}
{{enumName}}Variable = {{enumName}}_FromString({{{enumName}}}Var->valuestring);
{{/isString}}
{{/isEnum}}
return {{enumName}}Variable;
end:
return 0;
}
{{/isEnum}}

{{/allParams}}
{{/operation}}
{{/operations}}















{{#operations}}
{{#operation}}
{{#summary}}
Expand Down Expand Up @@ -143,7 +232,8 @@
{{^isListContainer}}
keyQuery_{{{paramName}}} = strdup("{{{baseName}}}");
valueQuery_{{{paramName}}} = {{#isString}}{{^isEnum}}strdup({{/isEnum}}{{/isString}}({{{paramName}}}){{#isString}}{{^isEnum}}){{/isEnum}}{{/isString}};
keyPairQuery_{{paramName}} = keyValuePair_create(keyQuery_{{{paramName}}}, {{#isEnum}}(void *){{/isEnum}}{{^isString}}&{{/isString}}valueQuery_{{{paramName}}});
keyPairQuery_{{paramName}} = keyValuePair_create(keyQuery_{{{paramName}}}, {{#isEnum}}(void *)strdup({{enumName}}_ToString(
{{/isEnum}}{{^isString}}&{{/isString}}valueQuery_{{{paramName}}}{{#isEnum}})){{/isEnum}});
list_addElement(localVarQueryParameters,keyPairQuery_{{paramName}});
{{/isListContainer}}
}
Expand Down Expand Up @@ -334,10 +424,12 @@
free(keyQuery_{{{paramName}}});
keyQuery_{{{paramName}}} = NULL;
}
{{^isEnum}}
if(valueQuery_{{{paramName}}}){
free(valueQuery_{{{paramName}}});
valueQuery_{{{paramName}}} = NULL;
}
{{/isEnum}}
if(keyPairQuery_{{{paramName}}}){
keyValuePair_free(keyPairQuery_{{{paramName}}});
keyPairQuery_{{{paramName}}} = NULL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,32 @@
{{/imports}}


{{#operations}}
{{#operation}}
{{#allParams}}

{{#isEnum}}
// Enum {{enumName}} for {{{classname}}}_{{{operationId}}}

{{#allowableValues}}
typedef enum { {{#enumVars}} {{enumName}}_{{{value}}}{{#first}} = 0{{/first}}{{^-last}},{{/-last}}{{/enumVars}} } {{baseName}}_e;
{{/allowableValues}}

char* {{enumName}}_ToString({{baseName}}_e {{enumName}});

{{baseName}}_e {{enumName}}_FromString(char* {{enumName}});

cJSON *{{enumName}}_convertToJSON({{baseName}}_e {{enumName}});

{{baseName}}_e {{enumName}}_parseFromJSON(cJSON *{{enumName}}JSON);

{{/isEnum}}

{{/allParams}}
{{/operation}}
{{/operations}}


{{#operations}}
{{#operation}}
{{#summary}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@


{{#isEnum}}
char* {{classname}}_ToString({{classname}}_e {{classname}}){
char *{{classname}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} };
char* {{classFilename}}_{{classname}}_ToString({{classFilename}}_{{classname}}_e {{classname}}) {
char *{{classname}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} };
return {{classname}}Array[{{classname}}];
}

{{classname}}_e {{classname}}_FromString(char* {{classname}}){
{{classFilename}}_{{classname}}_e {{classFilename}}_{{classname}}_FromString(char* {{classname}}) {
int stringToReturn = 0;
char *{{classname}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} };
size_t sizeofArray = sizeof({{classname}}Array) / sizeof({{classname}}Array[0]);
Expand All @@ -25,10 +25,10 @@ char *{{classname}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{
return 0;
}

cJSON *{{classname}}_convertToJSON({{classname}}_e {{classname}}) {
cJSON *item = cJSON_CreateObject();
cJSON *{{classFilename}}_{{classname}}_convertToJSON({{classFilename}}_{{classname}}_e {{classname}}) {
cJSON *item = cJSON_CreateObject();
{{#isString}}
if(cJSON_AddStringToObject(item, "{{{classname}}}", {{{classname}}}_ToString({{{classname}}})) == NULL) {
if(cJSON_AddStringToObject(item, "{{{classname}}}", {{classFilename}}_{{{classname}}}_ToString({{{classname}}})) == NULL) {
goto fail;
}
{{/isString}}
Expand All @@ -38,48 +38,46 @@ cJSON *item = cJSON_CreateObject();
}
{{/isNumeric}}
return item;
fail:
fail:
cJSON_Delete(item);
return NULL;
}

{{classname}}_e {{classname}}_parseFromJSON(cJSON *{{classname}}JSON){

{{classname}}_e *{{classname}} = NULL;

{{classFilename}}_{{classname}}_e {{classFilename}}_{{classname}}_parseFromJSON(cJSON *{{classname}}JSON) {
{{classFilename}}_{{classname}}_e *{{classname}} = NULL;
{{#isEnum}}
{{#isNumeric}}
cJSON *{{{classname}}}Var = cJSON_GetObjectItemCaseSensitive({{classname}}JSON, "{{{classname}}}");
if(!cJSON_IsNumber({{{classname}}}Var))
{
goto end;
}
cJSON *{{{classname}}}Var = cJSON_GetObjectItemCaseSensitive({{classname}}JSON, "{{{classname}}}");
if(!cJSON_IsNumber({{{classname}}}Var))
{
goto end;
}
{{/isNumeric}}
{{#isString}}
{{{classname}}}_e {{classname}}Variable;
cJSON *{{{classname}}}Var = cJSON_GetObjectItemCaseSensitive({{classname}}JSON, "{{{classname}}}");
if(!cJSON_IsString({{{classname}}}Var) || ({{{classname}}}Var->valuestring == NULL)){
goto end;
}
{{classname}}Variable = {{classname}}_FromString({{{classname}}}Var->valuestring);
{{classFilename}}_{{{classname}}}_e {{classname}}Variable;
cJSON *{{{classname}}}Var = cJSON_GetObjectItemCaseSensitive({{classname}}JSON, "{{{classname}}}");
if(!cJSON_IsString({{{classname}}}Var) || ({{{classname}}}Var->valuestring == NULL)){
goto end;
}
{{classname}}Variable = {{classFilename}}_{{classname}}_FromString({{{classname}}}Var->valuestring);
{{/isString}}
{{/isEnum}}
return {{classname}}Variable;
return {{classname}}Variable;
end:
return 0;
return 0;
}
{{/isEnum}}
{{^isEnum}}
{{#vars}}
{{^isContainer}}
{{^isModel}}
{{#isEnum}}
char* {{name}}{{classname}}_ToString({{name}}_e {{name}}){
char* {{name}}{{classname}}_ToString({{classFilename}}_{{name}}_e {{name}}){
char *{{name}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} };
return {{name}}Array[{{name}}];
}

{{name}}_e {{name}}{{classname}}_FromString(char* {{name}}){
{{classFilename}}_{{name}}_e {{name}}{{classname}}_FromString(char* {{name}}){
int stringToReturn = 0;
char *{{name}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} };
size_t sizeofArray = sizeof({{name}}Array) / sizeof({{name}}Array[0]);
Expand All @@ -98,12 +96,12 @@ return 0;
{{#items}}
{{^isModel}}
{{#isEnum}}
char* {{name}}{{classname}}_ToString({{name}}_e {{name}}){
char* {{name}}{{classname}}_ToString({{classFilename}}_{{name}}_e {{name}}){
char *{{name}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} };
return {{name}}Array[{{name}} - 1];
}

{{name}}_e {{name}}{{classname}}_FromString(char* {{name}}){
{{classFilename}}_{{name}}_e {{name}}{{classname}}_FromString(char* {{name}}){
int stringToReturn = 0;
char *{{name}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} };
size_t sizeofArray = sizeof({{name}}Array) / sizeof({{name}}Array[0]);
Expand All @@ -127,7 +125,7 @@ return 0;
{{^isPrimitiveType}}
{{#isModel}}
{{#isEnum}}
{{datatype}}_e {{name}}{{#hasMore}},{{/hasMore}}
{{classFilename}}_{{datatype}}_e {{name}}{{#hasMore}},{{/hasMore}}
{{/isEnum}}
{{^isEnum}}
{{datatype}}_t *{{name}}{{#hasMore}},{{/hasMore}}
Expand All @@ -152,7 +150,7 @@ return 0;
{{/isBoolean}}
{{#isEnum}}
{{#isString}}
{{name}}_e {{name}}{{#hasMore}},{{/hasMore}}
{{classFilename}}_{{name}}_e {{name}}{{#hasMore}},{{/hasMore}}
{{/isString}}
{{/isEnum}}
{{^isEnum}}
Expand Down Expand Up @@ -409,7 +407,7 @@ cJSON *{{classname}}_convertToJSON({{classname}}_t *{{classname}}) {
listEntry_t *{{{name}}}ListEntry;
if ({{{classname}}}->{{{name}}}) {
list_ForEach({{{name}}}ListEntry, {{classname}}->{{{name}}}) {
cJSON *itemLocal = {{complexType}}_convertToJSON({{#isEnum}}{{#items}}({{datatypeWithEnum}}_e){{/items}}{{/isEnum}}{{{name}}}ListEntry->data);
cJSON *itemLocal = {{complexType}}_convertToJSON({{#isEnum}}{{#items}}({{classFilename}}_{{datatypeWithEnum}}_e){{/items}}{{/isEnum}}{{{name}}}ListEntry->data);
if(itemLocal == NULL) {
goto fail;
}
Expand Down Expand Up @@ -490,7 +488,7 @@ fail:
{{/isBoolean}}
{{#isEnum}}
{{#isString}}
{{{name}}}_e {{name}}Variable;
{{classFilename}}_{{{name}}}_e {{name}}Variable;
{{^required}}if ({{{name}}}) { {{/required}}
if(!cJSON_IsString({{{name}}}))
{
Expand Down Expand Up @@ -548,7 +546,7 @@ fail:
{{^isPrimitiveType}}
{{#isModel}}
{{#isEnum}}
{{datatypeWithEnum}}_e {{name}}_local_nonprim_enum;
{{classFilename}}_{{datatypeWithEnum}}_e {{name}}_local_nonprim_enum;
{{^required}}if ({{{name}}}) { {{/required}}
{{{name}}}_local_nonprim_enum = {{datatypeWithEnum}}_parseFromJSON({{{name}}}); //enum model
{{/isEnum}}
Expand Down Expand Up @@ -625,7 +623,7 @@ fail:
if(!cJSON_IsObject({{{name}}}_local_nonprimitive)){
goto end;
}
{{#isEnum}}{{#items}}{{datatypeWithEnum}}_e {{/items}}{{/isEnum}}{{^isEnum}}{{complexType}}_t *{{/isEnum}}{{{name}}}Item = {{complexType}}_parseFromJSON({{{name}}}_local_nonprimitive);
{{#isEnum}}{{#items}}{{classFilename}}_{{datatypeWithEnum}}_e {{/items}}{{/isEnum}}{{^isEnum}}{{complexType}}_t *{{/isEnum}}{{{name}}}Item = {{complexType}}_parseFromJSON({{{name}}}_local_nonprimitive);

list_addElement({{{name}}}List, {{#isEnum}}{{#items}}(void *){{/items}}{{/isEnum}}{{{name}}}Item);
}
Expand Down
Loading