Skip to content

Commit

Permalink
Add string replace function for C generator (#908)
Browse files Browse the repository at this point in the history
* Add string replace function for C generator

* Fixed replacement for variable only

* Fixed problem for different datatypes of paramName

* store return value of modified path

* set str_replace variable to be same as original variable.

* [C] Fixed coding style issues
  • Loading branch information
zhemant authored and wing328 committed Sep 4, 2018
1 parent 21df220 commit b9f47cb
Show file tree
Hide file tree
Showing 2 changed files with 339 additions and 273 deletions.
76 changes: 47 additions & 29 deletions modules/openapi-generator/src/main/resources/C-libcurl/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
{{/imports}}

#define MAX_BUFFER_LENGTH 4096
#define intToStr(dst, src) \
do {\
char dst[64];\
snprintf(dst, 64, "%ld", (long int)(src));\
}while(0)

{{#operations}}
{{#operation}}
Expand All @@ -19,19 +24,30 @@
//
{{/notes}}
{{#returnType}}{{{.}}}_t{{/returnType}}{{^returnType}}void{{/returnType}} *{{{classname}}}_{{{operationId}}}(apiClient_t *apiClient{{#allParams}}, {{{dataType}}} {{paramName}}{{/allParams}}) {
list_t *localVarQueryParameters,
list_t *localVarHeaderParameters,
list_t *localVarFormParameters,
char *localVarBodyParameters,
list_t *localVarQueryParameters,
list_t *localVarHeaderParameters,
list_t *localVarFormParameters,
char *localVarBodyParameters,
// create the path
char *localVarPath = malloc(MAX_BUFFER_LENGTH);
snprintf(localVarPath, MAX_BUFFER_LENGTH, "{{{path}}}");
char *localVarPath = malloc(MAX_BUFFER_LENGTH);
snprintf(localVarPath, MAX_BUFFER_LENGTH, "{{{path}}}");
{{#pathParams}}
// TODO path parameter {{paramName}} ({{baseName}}) not yet supported
// TODO base path = {{{basePath}}}
replace_str(localVarPath, "{" + "{{baseName}}" + "}", {{paramName}})// TODO need to revise
char* baseNameMod = malloc(strlen({{baseName}})+2); //baseNameMod free not yet implemented
snprintf(baseNameMod, strlen(baseName)+3, "%s%s%s", "{", {{baseName}}, "}");
{{#paramName}}
{{#isLong}}
char buff[64];
intToStr(buf, {{paramName}});
localVarPath = strReplace(localVarPath, baseNameMod, buff);
{{/isLong}}
{{#isString}}
localVarPath = strReplace(localVarPath, baseNameMod, {{paramName}});
{{/isString}}
{{/paramName}}
{{/pathParams}}

{{#headerParams}}
Expand All @@ -57,25 +73,25 @@
localVarBodyParameters = cJSON_Print({{{paramName}}}JSONObject);

{{/bodyParam}}
apiClient_invoke(apiClient,
"pet",
localVarPath,
localVarQueryParameters,
localVarHeaderParameters,
localVarFormParameters,
localVarBodyParameters,
"{{{httpMethod}}}");
apiClient_invoke(apiClient,
"pet",
localVarPath,
localVarQueryParameters,
localVarHeaderParameters,
localVarFormParameters,
localVarBodyParameters,
"{{{httpMethod}}}");

free(apiClient->dataReceived);
{{#allParams}}
{{^bodyParam}}
free({{{paramName}}}String);
{{/bodyParam}}
{{#bodyParam}}
free(apiClient->dataReceived);
{{#allParams}}
{{^bodyParam}}
free({{{paramName}}}String);
{{/bodyParam}}
{{#bodyParam}}
free(localVarBodyParameters);
cJSON_Delete()
{{/bodyParam}}
{{/allParams}}
cJSON_Delete()
{{/bodyParam}}
{{/allParams}}
{{#returnType}}
localVar{{{returnType}}} = {{complexType}}_parseFromJSON(apiClient->dataReceived);
if(localVar{{{returnType}}} == NULL) {
Expand All @@ -85,13 +101,15 @@
cJSON_Delete(jsonObject);
}

return localVar{{{returnType}}};
{{/returnType}}
return localVar{{{returnType}}};
{{/returnType}}
{{^returnType}}
return;
{{/returnType}}
return;
{{/returnType}}

}

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


Loading

0 comments on commit b9f47cb

Please sign in to comment.