Skip to content

Commit

Permalink
[swift5] Map file and binary to Data
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed May 25, 2021
1 parent 6c40192 commit 9ea1bd6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig
public static final String USE_BACKTICK_ESCAPES = "useBacktickEscapes";
public static final String GENERATE_MODEL_ADDITIONAL_PROPERTIES = "generateModelAdditionalProperties";
public static final String HASHABLE_MODELS = "hashableModels";
public static final String MAP_FILE_BINARY_TO_DATA = "mapFileBinaryToData";
protected static final String LIBRARY_ALAMOFIRE = "alamofire";
protected static final String LIBRARY_URLSESSION = "urlsession";
protected static final String RESPONSE_LIBRARY_PROMISE_KIT = "PromiseKit";
Expand All @@ -84,6 +85,7 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig
protected boolean useBacktickEscapes = false;
protected boolean generateModelAdditionalProperties = true;
protected boolean hashableModels = true;
protected boolean mapFileBinaryToData = false;
protected String[] responseAs = new String[0];
protected String sourceFolder = swiftPackagePath;
protected HashSet objcReservedWords;
Expand Down Expand Up @@ -120,7 +122,6 @@ public Swift5ClientCodegen() {
"Bool",
"Void",
"String",
"URL",
"Data",
"Date",
"Character",
Expand Down Expand Up @@ -286,6 +287,10 @@ public Swift5ClientCodegen() {
"Make hashable models (default: true)")
.defaultValue(Boolean.TRUE.toString()));

cliOptions.add(new CliOption(MAP_FILE_BINARY_TO_DATA,
"Map File and Binary to Data (default: false)")
.defaultValue(Boolean.FALSE.toString()));

supportedLibraries.put(LIBRARY_URLSESSION, "[DEFAULT] HTTP client: URLSession");
supportedLibraries.put(LIBRARY_ALAMOFIRE, "HTTP client: Alamofire");

Expand Down Expand Up @@ -470,6 +475,15 @@ public void processOpts() {
}
additionalProperties.put(HASHABLE_MODELS, hashableModels);

if (additionalProperties.containsKey(MAP_FILE_BINARY_TO_DATA)) {
setMapFileBinaryToData(convertPropertyToBooleanAndWriteBack(MAP_FILE_BINARY_TO_DATA));
}
additionalProperties.put(MAP_FILE_BINARY_TO_DATA, mapFileBinaryToData);
if (mapFileBinaryToData) {
typeMapping.put("file", "Data");
typeMapping.put("binary", "Data");
}

setLenientTypeCast(convertPropertyToBooleanAndWriteBack(LENIENT_TYPE_CAST));

// make api and model doc path available in mustache template
Expand Down Expand Up @@ -547,6 +561,14 @@ public void processOpts() {

}

public boolean isMapFileBinaryToData() {
return mapFileBinaryToData;
}

public void setMapFileBinaryToData(boolean mapFileBinaryToData) {
this.mapFileBinaryToData = mapFileBinaryToData;
}

@Override
protected boolean isReservedWord(String word) {
return word != null && reservedWords.contains(word); //don't lowercase as super does
Expand Down Expand Up @@ -1121,7 +1143,7 @@ public String constructExampleCode(CodegenParameter codegenParameter, HashMap<St
if (StringUtils.isEmpty(codegenParameter.example)) {
return codegenParameter.example;
} else {
return "987";
return "Data([9, 8, 7])";
}
}
} else { // model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public Map<String, String> createOptions() {
.put(Swift5ClientCodegen.SWIFT_PACKAGE_PATH, SWIFT_PACKAGE_PATH_VALUE)
.put(Swift5ClientCodegen.GENERATE_MODEL_ADDITIONAL_PROPERTIES, GENERATE_MODEL_ADDITIONAL_PROPERTIES_VALUE)
.put(Swift5ClientCodegen.HASHABLE_MODELS, HASHABLE_MODELS_VALUE)
.put(Swift5ClientCodegen.MAP_FILE_BINARY_TO_DATA, "false")
.build();
}

Expand Down

0 comments on commit 9ea1bd6

Please sign in to comment.