[C] Update README and adding object.c/h files (#2377)

* modify handing reserved keyword and set isEnum in local codegen to avoid conflict with other codegen

* update README mustache

* added object header and body mustache
This commit is contained in:
Hemant Zope
2019-03-19 15:41:34 +01:00
committed by William Cheng
parent 43fcd19509
commit 7ad479e13d
4 changed files with 91 additions and 6 deletions

View File

@@ -205,6 +205,9 @@ public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConf
supportingFiles.add(new SupportingFile("cJSON.c.mustache", "external", "cJSON.c"));
supportingFiles.add(new SupportingFile("cJSON.h.mustache", "external", "cJSON.h"));
// Object files in model folder
supportingFiles.add(new SupportingFile("object-body.mustache", "model", "object.c"));
supportingFiles.add(new SupportingFile("object-header.mustache", "model", "object.h"));
}
@Override
@@ -333,6 +336,9 @@ public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConf
@Override
public String toParamName(String name) {
// should be the same as variable name
if (name.matches("^\\d.*")) {
name = escapeReservedWord(name);
}
name = name.replaceAll("-","_");
return name;
}
@@ -412,11 +418,15 @@ public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConf
@Override
public String toEnumValue(String value, String datatype) {
value = value.replaceAll("-","_");
if (isReservedWord(value)) {
value = escapeReservedWord(value);
}
if ("Integer".equals(datatype) || "Float".equals(datatype)) {
return value;
} else {
if (value.matches("\\d.*")) { // starts with number
return "N" + escapeText(value);
return escapeReservedWord(escapeText(value));
} else {
return escapeText(value);
}
@@ -444,7 +454,7 @@ public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConf
enumName = enumName.replaceFirst("_$", "");
if (enumName.matches("\\d.*")) { // starts with number
return "N" + enumName;
return escapeReservedWord(enumName);
} else {
return enumName;
}
@@ -457,7 +467,7 @@ public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConf
enumName = enumName.replaceFirst("_$", "");
if (enumName.matches("\\d.*")) { // starts with number
return "N" + enumName;
return escapeReservedWord(enumName);
} else {
return enumName;
}
@@ -502,7 +512,10 @@ public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConf
@Override
public String toModelImport(String name) {
return "#include \"" +"../model/" + name + ".h\"";
if (importMapping.containsKey(name)) {
return "#include \"" +"../model/" + importMapping.get(name) + ".h\"";
} else
return "#include \"" +"../model/" + name + ".h\"";
}
@Override
@@ -595,6 +608,18 @@ public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConf
return input.replace("=end", "=_end").replace("=begin", "=_begin");
}
@Override
public CodegenProperty fromProperty(String name, Schema p) {
CodegenProperty cm = super.fromProperty(name,p);
Schema ref = ModelUtils.getReferencedSchema(openAPI, p);
if (ref != null) {
if (ref.getEnum() != null) {
cm.isEnum = true;
}
}
return cm;
}
@Override
public void postProcessFile(File file, String fileType) {

View File

@@ -40,6 +40,7 @@ sudo make install
```
## Compile the sample:
This will compile the generated code and create a library in build folder which has to be linked to the codes where API will be used.
```bash
mkdir build
cd build
@@ -51,15 +52,17 @@ make
sudo make install
```
## How to use compiled library
Considering the test/source code which uses the API is written in main.c(respective api include is written and all objects necessary are defined)
Considering the test/source code which uses the API is written in main.c(respective api include is written and all objects necessary are defined and created)
To compile main.c use following command
To compile main.c(considering the file is present in build folder) use following command
-L - locaiton of the library(not required if cmake with normal installation is performed)
-l library name
```bash
gcc main.c -L. -lpetstore -o main
```
once compile, you can run it with ``` ./main ```
Note: You dont need to specify includes for models and include folder seperately as they are path linked. You just have to import the api.h file in your code, the include linking will work.
## Author

View File

@@ -0,0 +1,31 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "object.h"
object_t *object_create() {
object_t *object = malloc(sizeof(object_t));
return object;
}
void object_free(object_t *object) {
free (object);
}
cJSON *object_convertToJSON(object_t *object) {
cJSON *item = cJSON_CreateObject();
return item;
fail:
cJSON_Delete(item);
return NULL;
}
object_t *object_parseFromJSON(char *jsonString){
object_t *object = NULL;
return object;
end:
return NULL;
}

View File

@@ -0,0 +1,26 @@
/*
* object.h
*/
#ifndef _object_H_
#define _object_H_
#include <string.h>
#include "../external/cJSON.h"
#include "../include/list.h"
#include "../include/keyValuePair.h"
typedef struct object_t {
void *temporary;
} object_t;
object_t *object_create();
void object_free(object_t *object);
object_t *object_parseFromJSON(char *jsonString);
cJSON *object_convertToJSON(object_t *object);
#endif /* _object_H_ */