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

[C++] [Qt5] Add initial version of File upload and download for Qt5 client #3853

Merged
merged 3 commits into from
Sep 17, 2019
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 @@ -26,7 +26,7 @@ public class CppQt5AbstractCodegen extends AbstractCppCodegen implements Codegen
protected Set<String> systemIncludes = new HashSet<String>();

protected Set<String> nonFrameworkPrimitives = new HashSet<String>();

public CppQt5AbstractCodegen() {
super();
// set modelNamePrefix as default for QHttpEngine Server
Expand Down Expand Up @@ -61,10 +61,10 @@ public CppQt5AbstractCodegen() {
"double")
);
nonFrameworkPrimitives.addAll(languageSpecificPrimitives);

foundationClasses.addAll(
Arrays.asList(
"QString",
"QString",
"QDate",
"QDateTime",
"QByteArray")
Expand All @@ -78,7 +78,7 @@ public CppQt5AbstractCodegen() {
typeMapping.put("integer", "qint32");
typeMapping.put("long", "qint64");
typeMapping.put("boolean", "bool");
typeMapping.put("number", "double");
typeMapping.put("number", "double");
typeMapping.put("array", "QList");
typeMapping.put("map", "QMap");
typeMapping.put("object", PREFIX + "Object");
Expand All @@ -90,8 +90,8 @@ public CppQt5AbstractCodegen() {
// modifications on multiple templates)
typeMapping.put("UUID", "QString");
typeMapping.put("URI", "QString");
typeMapping.put("file", "QIODevice");
typeMapping.put("binary", "QIODevice");
typeMapping.put("file", "QByteArray");
typeMapping.put("binary", "QByteArray");
importMapping = new HashMap<String, String>();
namespaces = new HashMap<String, String>();

Expand All @@ -101,7 +101,6 @@ public CppQt5AbstractCodegen() {
systemIncludes.add("QDate");
systemIncludes.add("QDateTime");
systemIncludes.add("QByteArray");
systemIncludes.add("QIODevice");
}
@Override
public void processOpts() {
Expand All @@ -119,7 +118,7 @@ public void processOpts() {
additionalProperties().put("prefix", modelNamePrefix);
}
}

@Override
public String toModelImport(String name) {
if( name.isEmpty() ) {
Expand All @@ -140,7 +139,7 @@ public String toModelImport(String name) {

return "#include \"" + folder + name + ".h\"";
}

/**
* Optional - type declaration. This is a String which is used by the templates to instantiate your
* types. There is typically special handling for different property types
Expand All @@ -160,9 +159,9 @@ public String getTypeDeclaration(Schema p) {
Schema inner = ModelUtils.getAdditionalProperties(p);
return getSchemaType(p) + "<QString, " + getTypeDeclaration(inner) + ">";
} else if (ModelUtils.isBinarySchema(p)) {
return getSchemaType(p) + "*";
return getSchemaType(p);
} else if (ModelUtils.isFileSchema(p)) {
return getSchemaType(p) + "*";
return getSchemaType(p);
}
if (foundationClasses.contains(openAPIType)) {
return openAPIType;
Expand All @@ -174,7 +173,7 @@ public String getTypeDeclaration(Schema p) {
}

@Override
@SuppressWarnings("rawtypes")
@SuppressWarnings("rawtypes")
public String toDefaultValue(Schema p) {
if (ModelUtils.isBooleanSchema(p)) {
return "false";
Expand Down Expand Up @@ -211,15 +210,15 @@ public String toDefaultValue(Schema p) {
public String toModelFilename(String name) {
return toModelName(name);
}

/**
* Optional - OpenAPI type conversion. This is used to map OpenAPI types in a `Schema` into
* either language specific types via `typeMapping` or into complex models if there is not a mapping.
*
* @return a string value of the type or complex model for this property
*/
@Override
@SuppressWarnings("rawtypes")
@SuppressWarnings("rawtypes")
public String getSchemaType(Schema p) {
String openAPIType = super.getSchemaType(p);

Expand All @@ -242,7 +241,7 @@ public String getSchemaType(Schema p) {
public String toVarName(String name) {
// sanitize name
String varName = name;
varName = sanitizeName(name);
varName = sanitizeName(name);

// if it's all uppper case, convert to lower case
if (varName.matches("^[A-Z_]*$")) {
Expand Down Expand Up @@ -270,7 +269,7 @@ public String toParamName(String name) {
public String getTypeDeclaration(String str) {
return str;
}

@Override
protected boolean needToImport(String type) {
return StringUtils.isNotBlank(type) && !defaultIncludes.contains(type)
Expand All @@ -283,7 +282,7 @@ protected boolean needToImport(String type) {
public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> objs, List<Object> allModels) {
Map<String, Object> objectMap = (Map<String, Object>) objs.get("operations");
List<CodegenOperation> operations = (List<CodegenOperation>) objectMap.get("operation");

List<Map<String, String>> imports = (List<Map<String, String>>) objs.get("imports");
Map<String, CodegenModel> codegenModels = new HashMap<String, CodegenModel> ();
for(Object moObj : allModels) {
Expand All @@ -298,7 +297,7 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
operation.vendorExtensions.put("returnsEnum", true);
}
}
// Check all return parameter baseType if there is a necessity to include, include it if not
// Check all return parameter baseType if there is a necessity to include, include it if not
// already done
if (operation.returnBaseType != null && needToImport(operation.returnBaseType)) {
if(!isIncluded(operation.returnBaseType, imports)) {
Expand All @@ -308,7 +307,7 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
List<CodegenParameter> params = new ArrayList<CodegenParameter>();
if (operation.allParams != null)params.addAll(operation.allParams);

// Check all parameter baseType if there is a necessity to include, include it if not
// Check all parameter baseType if there is a necessity to include, include it if not
// already done
for(CodegenParameter param : params) {
if(param.isPrimitiveType && needToImport(param.baseType)) {
Expand All @@ -321,7 +320,7 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
// We use QString to pass path params, add it to include
if(!isIncluded("QString", imports)) {
imports.add(createMapping("import", "QString"));
}
}
}
}
if(isIncluded("QMap", imports)) {
Expand All @@ -332,7 +331,7 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
}
return objs;
}

@Override
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
return postProcessModelsEnum(objs);
Expand All @@ -342,18 +341,18 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
public String toEnumValue(String value, String datatype) {
return escapeText(value);
}

@Override
public boolean isDataTypeString(String dataType) {
return "QString".equals(dataType);
}

private Map<String, String> createMapping(String key, String value) {
Map<String, String> customImport = new HashMap<String, String>();
customImport.put(key, toModelImport(value));
return customImport;
}

private boolean isIncluded(String type, List<Map<String, String>> imports) {
boolean included = false;
String inclStr = toModelImport(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@ public CppQt5ClientCodegen() {
supportingFiles.add(new SupportingFile("helpers-body.mustache", sourceFolder, PREFIX + "Helpers.cpp"));
supportingFiles.add(new SupportingFile("HttpRequest.h.mustache", sourceFolder, PREFIX + "HttpRequest.h"));
supportingFiles.add(new SupportingFile("HttpRequest.cpp.mustache", sourceFolder, PREFIX + "HttpRequest.cpp"));
supportingFiles.add(new SupportingFile("HttpFileElement.h.mustache", sourceFolder, PREFIX + "HttpFileElement.h"));
supportingFiles.add(new SupportingFile("HttpFileElement.cpp.mustache", sourceFolder, PREFIX + "HttpFileElement.cpp"));
supportingFiles.add(new SupportingFile("object.mustache", sourceFolder, PREFIX + "Object.h"));
supportingFiles.add(new SupportingFile("enum.mustache", sourceFolder, PREFIX + "Enum.h"));
supportingFiles.add(new SupportingFile("enum.mustache", sourceFolder, PREFIX + "Enum.h"));
if (optionalProjectFileFlag) {
supportingFiles.add(new SupportingFile("Project.mustache", sourceFolder, "client.pri"));
}
typeMapping.put("file", PREFIX + "HttpRequestInputFileElement");
typeMapping.put("binary", PREFIX +"HttpRequestInputFileElement");
importMapping.put(PREFIX + "HttpRequestInputFileElement", "#include \"" + PREFIX + "HttpRequest.h\"");
typeMapping.put("file", PREFIX + "HttpFileElement");
importMapping.put(PREFIX + "HttpFileElement", "#include \"" + PREFIX + "HttpFileElement.h\"");
}

@Override
Expand All @@ -95,19 +96,18 @@ public void processOpts() {
} else {
additionalProperties.put(CodegenConstants.OPTIONAL_PROJECT_FILE, optionalProjectFileFlag);
}

if (additionalProperties.containsKey("modelNamePrefix")) {
supportingFiles.clear();
supportingFiles.add(new SupportingFile("helpers-header.mustache", sourceFolder, modelNamePrefix + "Helpers.h"));
supportingFiles.add(new SupportingFile("helpers-body.mustache", sourceFolder, modelNamePrefix + "Helpers.cpp"));
supportingFiles.add(new SupportingFile("HttpRequest.h.mustache", sourceFolder, modelNamePrefix + "HttpRequest.h"));
supportingFiles.add(new SupportingFile("HttpRequest.cpp.mustache", sourceFolder, modelNamePrefix + "HttpRequest.cpp"));
supportingFiles.add(new SupportingFile("object.mustache", sourceFolder, modelNamePrefix + "Object.h"));
supportingFiles.add(new SupportingFile("enum.mustache", sourceFolder, modelNamePrefix + "Enum.h"));
supportingFiles.add(new SupportingFile("enum.mustache", sourceFolder, modelNamePrefix + "Enum.h"));

typeMapping.put("file", modelNamePrefix + "HttpRequestInputFileElement");
typeMapping.put("binary", modelNamePrefix + "HttpRequestInputFileElement");
importMapping.put(modelNamePrefix + "HttpRequestInputFileElement", "#include \"" + modelNamePrefix + "HttpRequest.h\"");
typeMapping.put("file", modelNamePrefix + "HttpFileElement");
importMapping.put(modelNamePrefix + "HttpFileElement", "#include \"" + modelNamePrefix + "HttpFileElement.h\"");
if (optionalProjectFileFlag) {
supportingFiles.add(new SupportingFile("Project.mustache", sourceFolder, modelNamePrefix + "client.pri"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public CppQt5QHttpEngineServerCodegen() {
apiTemplateFiles.put(
"apirequest.h.mustache", // the template to use
".h"); // the extension for each file to write

apiTemplateFiles.put(
"apirequest.cpp.mustache", // the template to use
".cpp"); // the extension for each file to write
Expand All @@ -86,11 +86,13 @@ public CppQt5QHttpEngineServerCodegen() {
* will use the resource stream to attempt to read the templates.
*/
embeddedTemplateDir = templateDir = "cpp-qt5-qhttpengine-server";

supportingFiles.add(new SupportingFile("helpers-header.mustache", sourceFolder + MODEL_DIR, PREFIX + "Helpers.h"));
supportingFiles.add(new SupportingFile("helpers-body.mustache", sourceFolder + MODEL_DIR, PREFIX + "Helpers.cpp"));
supportingFiles.add(new SupportingFile("object.mustache", sourceFolder + MODEL_DIR, PREFIX + "Object.h"));
supportingFiles.add(new SupportingFile("object.mustache", sourceFolder + MODEL_DIR, PREFIX + "Object.h"));
supportingFiles.add(new SupportingFile("enum.mustache", sourceFolder + MODEL_DIR, PREFIX + "Enum.h"));
supportingFiles.add(new SupportingFile("HttpFileElement.h.mustache", sourceFolder + MODEL_DIR, PREFIX + "HttpFileElement.h"));
supportingFiles.add(new SupportingFile("HttpFileElement.cpp.mustache", sourceFolder + MODEL_DIR, PREFIX + "HttpFileElement.cpp"));
supportingFiles.add(new SupportingFile("apirouter.h.mustache", sourceFolder + APIHANDLER_DIR, PREFIX + "ApiRouter.h"));
supportingFiles.add(new SupportingFile("apirouter.cpp.mustache", sourceFolder + APIHANDLER_DIR, PREFIX + "ApiRouter.cpp"));

Expand All @@ -102,6 +104,8 @@ public CppQt5QHttpEngineServerCodegen() {
supportingFiles.add(new SupportingFile("CMakeLists.txt.mustache", sourceFolder, "CMakeLists.txt"));
supportingFiles.add(new SupportingFile("Dockerfile.mustache", sourceFolder, "Dockerfile"));
supportingFiles.add(new SupportingFile("LICENSE.txt.mustache", sourceFolder, "LICENSE.txt"));
typeMapping.put("file", PREFIX + "HttpFileElement");
importMapping.put(PREFIX + "HttpFileElement", "#include \"" + PREFIX + "HttpFileElement.h\"");

}

Expand All @@ -115,16 +119,21 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("helpers-body.mustache", sourceFolder + MODEL_DIR, modelNamePrefix + "Helpers.cpp"));
supportingFiles.add(new SupportingFile("object.mustache", sourceFolder + MODEL_DIR, modelNamePrefix + "Object.h"));
supportingFiles.add(new SupportingFile("enum.mustache", sourceFolder + MODEL_DIR, modelNamePrefix + "Enum.h"));
supportingFiles.add(new SupportingFile("HttpFileElement.h.mustache", sourceFolder + MODEL_DIR, modelNamePrefix + "HttpFileElement.h"));
supportingFiles.add(new SupportingFile("HttpFileElement.cpp.mustache", sourceFolder + MODEL_DIR, modelNamePrefix + "HttpFileElement.cpp"));
supportingFiles.add(new SupportingFile("apirouter.h.mustache", sourceFolder + APIHANDLER_DIR, modelNamePrefix + "ApiRouter.h"));
supportingFiles.add(new SupportingFile("apirouter.cpp.mustache", sourceFolder + APIHANDLER_DIR, modelNamePrefix + "ApiRouter.cpp"));

supportingFiles.add(new SupportingFile("apirouter.cpp.mustache", sourceFolder + APIHANDLER_DIR, modelNamePrefix + "ApiRouter.cpp"));


supportingFiles.add(new SupportingFile("main.cpp.mustache", sourceFolder + SRC_DIR, "main.cpp"));
supportingFiles.add(new SupportingFile("src-CMakeLists.txt.mustache", sourceFolder + SRC_DIR, "CMakeLists.txt"));
supportingFiles.add(new SupportingFile("README.md.mustache", sourceFolder, "README.MD"));
supportingFiles.add(new SupportingFile("Makefile.mustache", sourceFolder, "Makefile"));
supportingFiles.add(new SupportingFile("CMakeLists.txt.mustache", sourceFolder, "CMakeLists.txt"));
supportingFiles.add(new SupportingFile("Dockerfile.mustache", sourceFolder, "Dockerfile"));
supportingFiles.add(new SupportingFile("LICENSE.txt.mustache", sourceFolder, "LICENSE.txt"));
typeMapping.put("file", modelNamePrefix + "HttpFileElement");
importMapping.put(modelNamePrefix + "HttpFileElement", "#include \"" + modelNamePrefix + "HttpFileElement.h\"");
}
}

Expand Down Expand Up @@ -160,7 +169,7 @@ public String getName() {
public String getHelp() {
return "Generates a Qt5 C++ Server using the QHTTPEngine HTTP Library.";
}

/**
* Location to write model files. You can use the modelPackage() as defined when the class is
* instantiated
Expand All @@ -182,7 +191,7 @@ public String apiFileFolder() {
private String requestFileFolder() {
return outputFolder + "/" + sourceFolder + APIREQUEST_DIR + "/" + apiPackage().replace("::", File.separator);
}

@Override
public String apiFilename(String templateName, String tag) {
String result = super.apiFilename(templateName, tag);
Expand All @@ -193,7 +202,7 @@ public String apiFilename(String templateName, String tag) {
}
return result;
}

@Override
public String toApiFilename(String name) {
return modelNamePrefix + sanitizeName(camelize(name)) + "ApiHandler";
Expand Down
Loading