Skip to content

Commit

Permalink
add better enum support to swift
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 committed May 3, 2016
1 parent acb34e3 commit 982a035
Show file tree
Hide file tree
Showing 10 changed files with 412 additions and 830 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,10 @@ public CodegenProperty fromProperty(String name, Property p) {
swiftEnums.add(map);
}
codegenProperty.allowableValues.put("values", swiftEnums);
codegenProperty.datatypeWithEnum =
StringUtils.left(codegenProperty.datatypeWithEnum, codegenProperty.datatypeWithEnum.length() - "Enum".length());
codegenProperty.datatypeWithEnum = toEnumName(codegenProperty);
//codegenProperty.datatypeWithEnum =
// StringUtils.left(codegenProperty.datatypeWithEnum, codegenProperty.datatypeWithEnum.length() - "Enum".length());

// Ensure that the enum type doesn't match a reserved word or
// the variable name doesn't match the generated enum type or the
// Swift compiler will generate an error
Expand Down Expand Up @@ -483,4 +485,59 @@ public void setUnwrapRequired(boolean unwrapRequired) {
public void setResponseAs(String[] responseAs) {
this.responseAs = responseAs;
}

@Override
public String toEnumValue(String value, String datatype) {
if ("int".equals(datatype) || "double".equals(datatype) || "float".equals(datatype)) {
return value;
} else {
return "\'" + escapeText(value) + "\'";
}
}

@Override
public String toEnumDefaultValue(String value, String datatype) {
return datatype + "_" + value;
}

@Override
public String toEnumVarName(String name, String datatype) {
// number
if ("int".equals(datatype) || "double".equals(datatype) || "float".equals(datatype)) {
String varName = new String(name);
varName = varName.replaceAll("-", "MINUS_");
varName = varName.replaceAll("\\+", "PLUS_");
varName = varName.replaceAll("\\.", "_DOT_");
return varName;
}

// string
String enumName = sanitizeName(underscore(name).toUpperCase());
enumName = enumName.replaceFirst("^_", "");
enumName = enumName.replaceFirst("_$", "");

if (enumName.matches("\\d.*")) { // starts with number
return "_" + enumName;
} else {
return enumName;
}
}

@Override
public String toEnumName(CodegenProperty property) {
String enumName = underscore(toModelName(property.name)).toUpperCase();

This comment has been minimized.

Copy link
@cjolif

cjolif May 11, 2016

Contributor

Typically Swift Enum names are first letter upper case and then lower case. See: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Enumerations.html#//apple_ref/doc/uid/TP40014097-CH12-ID146

That was the case before this commit and is not anymore the case. I'm not sure why? Shouldn't we backtrack on this?

This comment has been minimized.

Copy link
@wing328

wing328 May 11, 2016

Author Contributor

Please open an issue to track this.

We should add a test case for the Swift code generator to cover this moving forward.

Let me know if you've cycle to contribute the fix and/or the test case.

This comment has been minimized.

Copy link
@cjolif

cjolif May 11, 2016

Contributor

See #2835. If I get the cycles, I'll provide a fix.


if (enumName.matches("\\d.*")) { // starts with number
return "_" + enumName;
} else {
return enumName;
}
}

@Override
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
// process enum in models
return postProcessModelsEnum(objs);
}

}
Loading

0 comments on commit 982a035

Please sign in to comment.