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

Better format in C templates #1224

Merged
merged 2 commits into from
Oct 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public CLibcurlClientCodegen() {
typeMapping.put("array", "list");
typeMapping.put("map", "list_t*");
typeMapping.put("date-time", "char");

// remove modelPackage and apiPackage added by default
Iterator<CliOption> itr = cliOptions.iterator();
while (itr.hasNext()) {
Expand All @@ -173,7 +173,7 @@ public void processOpts() {
super.processOpts();

if (StringUtils.isEmpty(System.getenv("C_POST_PROCESS_FILE"))) {
LOGGER.info("Environment variable C_POST_PROCESS_FILE not defined so the C code may not be properly formatted by uncrustify (0.66 or later) or other code formatter. To define it, try `export C_POST_PROCESS_FILE=\"/usr/local/bin/uncrustify --no-backup\"` (Linux/Mac)");
LOGGER.info("Environment variable C_POST_PROCESS_FILE not defined so the C code may not be properly formatted by uncrustify (0.66 or later) or other code formatter. To define it, try `export C_POST_PROCESS_FILE=\"/usr/local/bin/uncrustify --no-backup\" && export UNCRUSTIFY_CONFIG=/path/to/uncrustify-rules.cfg` (Linux/Mac). Note: replace /path/to with the location of uncrustify-rules.cfg");
}

// make api and model doc path available in mustache template
Expand Down Expand Up @@ -202,13 +202,6 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("cJSON.c.mustache", "external" + File.separator + "src", "cJSON.c"));
supportingFiles.add(new SupportingFile("cJSON.h.mustache", "external" + File.separator + "include", "cJSON.h"));

// test files should not be overwritten
writeOptional(outputFolder, new SupportingFile("api_store_test.mustache", "unit-tests", "manual-StoreAPI.c"));
writeOptional(outputFolder, new SupportingFile("api_user_test.mustache", "unit-tests", "manual-UserAPI.c"));
writeOptional(outputFolder, new SupportingFile("api_pet_test.mustache", "unit-tests", "manual-PetAPI.c"));
writeOptional(outputFolder, new SupportingFile("model_order_test.mustache", "unit-tests", "manual-order.c"));
writeOptional(outputFolder, new SupportingFile("model_user_test.mustache", "unit-tests", "manual-user.c"));
//writeOptional(outputFolder, new SupportingFile("apiKey.c.mustache", "unit-tests", "apiKey.c"));
}

@Override
Expand Down Expand Up @@ -418,7 +411,7 @@ public String toEnumValue(String value, String datatype) {
if ("Integer".equals(datatype) || "Float".equals(datatype)) {
return value;
} else {
return escapeText(value) ;
return escapeText(value);
}
}

Expand Down Expand Up @@ -498,7 +491,7 @@ public String toOperationId(String operationId) {
public String toApiImport(String name) {
return apiPackage() + "/" + toApiFilename(name);
}

@Override
public String toModelImport(String name) {
return "#include \"" + name + ".h\"";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
snprintf(dst, 256, "%ld", (long int)(src));\
}while(0)



{{#operations}}
{{#operation}}
{{#summary}}
Expand All @@ -36,7 +34,7 @@
char *localVarBodyParameters = NULL;

// create the path
long sizeOfPath = strlen("{{{path}}}")+1;
long sizeOfPath = strlen("{{{path}}}")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "{{{path}}}");

Expand All @@ -47,7 +45,7 @@

{{#isLong}}
if({{baseName}} == 0){
goto end;
goto end;
}
char* localVarToReplace = malloc(sizeOfPathParams);
snprintf(localVarToReplace, sizeOfPathParams, "%s%s%s", "{", "{{baseName}}", "}");
Expand All @@ -74,8 +72,7 @@
char *keyHeader_{{{baseName}}};
char *valueHeader_{{{baseName}}};
keyValuePair_t *keyPairHeader_{{baseName}} = 0;
if ({{baseName}})
{
if ({{baseName}}) {
keyHeader_{{{baseName}}} = strdup("{{{baseName}}}");
valueHeader_{{{baseName}}} = strdup({{{baseName}}});
keyPairHeader_{{baseName}} = keyValuePair_create(keyHeader_{{{baseName}}}, valueHeader_{{{baseName}}});
Expand All @@ -86,7 +83,7 @@

// query parameters
{{#isListContainer}}
if ({{baseName}} !=NULL )
if ({{baseName}} !=NULL )
{{/isListContainer}}
{{^isListContainer}}
char *keyQuery_{{{baseName}}};
Expand Down Expand Up @@ -157,13 +154,13 @@
// Body Param
{{#isListContainer}}
//notstring
cJSON *localVar_{{baseName}};
cJSON *localVarItemJSON_{{baseName}};
cJSON *localVarSingleItemJSON_{{baseName}};
if({{baseName}} != NULL){
cJSON *localVar_{{baseName}};
cJSON *localVarItemJSON_{{baseName}};
cJSON *localVarSingleItemJSON_{{baseName}};
if ({{baseName}} != NULL) {
localVarItemJSON_{{baseName}} = cJSON_CreateObject();
localVarSingleItemJSON_{{baseName}} = cJSON_AddArrayToObject(localVarItemJSON_{{baseName}}, "{{baseName}}");
if(localVarSingleItemJSON_{{baseName}} == NULL) {
if (localVarSingleItemJSON_{{baseName}} == NULL) {
// nonprimitive container
const char *error_ptr = cJSON_GetErrorPtr();
if(error_ptr != NULL) {
Expand All @@ -176,12 +173,12 @@
list_ForEach({{baseName}}BodyListEntry, {{baseName}}) {
localVar_{{baseName}} = user_convertToJSON({{baseName}}BodyListEntry->data);
if(localVar_{{baseName}} == NULL) {
const char *error_ptr = cJSON_GetErrorPtr();
if(error_ptr != NULL) {
fprintf(stderr, "Error Before: %s\n", error_ptr);
goto end;
const char *error_ptr = cJSON_GetErrorPtr();
if(error_ptr != NULL) {
fprintf(stderr, "Error Before: %s\n", error_ptr);
goto end;
}
}
}
cJSON_AddItemToArray(localVarSingleItemJSON_{{baseName}}, localVar_{{baseName}});
}

Expand All @@ -190,22 +187,19 @@
{{/isListContainer}}
{{^isListContainer}}
cJSON *localVarSingleItemJSON_{{baseName}};
if({{baseName}} != NULL){
//string
if ({{baseName}} != NULL) {
//string
localVarSingleItemJSON_{{baseName}} = {{baseName}}_convertToJSON({{baseName}});
localVarBodyParameters = cJSON_Print(localVarSingleItemJSON_{{baseName}});
}
{{/isListContainer}}
{{/bodyParam}}
{{#produces}}

list_addElement(localVarHeaderType,"{{{mediaType}}}"); //produces
list_addElement(localVarHeaderType,"{{{mediaType}}}"); //produces
{{/produces}}
{{#consumes}}

list_addElement(localVarContentType,"{{{mediaType}}}"); //consumes
list_addElement(localVarContentType,"{{{mediaType}}}"); //consumes
{{/consumes}}

apiClient_invoke(apiClient,
localVarPath,
localVarQueryParameters,
Expand All @@ -221,8 +215,8 @@
printf("%s\n","{{message}}");
}
{{/responses}}
{{#returnType}}
{{#returnTypeIsPrimitive}}
{{#returnType}}
{{#returnTypeIsPrimitive}}
{{#returnSimpleType}}
//primitive reutrn type simple
{{returnType}}* elementToReturn = strdup(({{returnType}}*)apiClient->dataReceived);
Expand All @@ -234,9 +228,9 @@
cJSON *{{{baseName}}}VarJSON;
list_t *elementToReturn = list_create();
cJSON_ArrayForEach({{{baseName}}}VarJSON, {{baseName}}localVarJSON){
keyValuePair_t *keyPair = keyValuePair_create({{{baseName}}}VarJSON->string, cJSON_Print({{{baseName}}}VarJSON));
list_addElement(elementToReturn, keyPair);
free(keyPair->value);
keyValuePair_t *keyPair = keyValuePair_create({{{baseName}}}VarJSON->string, cJSON_Print({{{baseName}}}VarJSON));
list_addElement(elementToReturn, keyPair);
free(keyPair->value);
}
cJSON_Delete({{baseName}}localVarJSON);

Expand All @@ -252,12 +246,12 @@
cJSON *{{{baseName}}}VarJSON;
cJSON_ArrayForEach({{{baseName}}}VarJSON, {{classname}}localVarJSON)
{
if(!cJSON_IsObject({{{baseName}}}VarJSON))
{
// return 0;
}
char *localVarJSONToChar = cJSON_Print({{{baseName}}}VarJSON);
list_addElement(elementToReturn , localVarJSONToChar);
if(!cJSON_IsObject({{{baseName}}}VarJSON))
{
// return 0;
}
char *localVarJSONToChar = cJSON_Print({{{baseName}}}VarJSON);
list_addElement(elementToReturn , localVarJSONToChar);
}

cJSON_Delete( {{classname}}localVarJSON);
Expand All @@ -267,7 +261,7 @@
//nonprimitive not container
{{{returnBaseType}}}_t *elementToReturn = {{{returnBaseType}}}_parseFromJSON(apiClient->dataReceived);
if(elementToReturn == NULL) {
// return 0;
// return 0;
}

{{/returnContainer}}
Expand Down Expand Up @@ -328,7 +322,7 @@
{{/formParams}}
return elementToReturn;
end:
return NULL;
return NULL;
{{/returnType}}
{{^returnType}}
//No return type
Expand All @@ -338,7 +332,7 @@ end: apiClient_free(apiClient);
{{#hasFormParams}}list_free(localVarFormParameters);{{/hasFormParams}}
{{#hasProduces}}list_free(localVarHeaderType);{{/hasProduces}}
{{#hasConsumes}}list_free(localVarContentType);{{/hasConsumes}}
{{#pathParams}}
{{#pathParams}}
free(localVarToReplace);
{{/pathParams}}
{{#headerParams}}
Expand Down
Loading