better enum naming for symbol (java)

This commit is contained in:
wing328
2016-08-04 16:37:48 +08:00
parent d5bf43b824
commit 670f103859
7 changed files with 70 additions and 49 deletions

View File

@@ -106,7 +106,7 @@ public class DefaultCodegen {
// How to encode special characters like $ // How to encode special characters like $
// They are translated to words like "Dollar" and prefixed with ' // They are translated to words like "Dollar" and prefixed with '
// Then translated back during JSON encoding and decoding // Then translated back during JSON encoding and decoding
protected Map<Character, String> specialCharReplacements = new HashMap<Character, String>(); protected Map<String, String> specialCharReplacements = new HashMap<String, String>();
public List<CliOption> cliOptions() { public List<CliOption> cliOptions() {
return cliOptions; return cliOptions;
@@ -789,21 +789,35 @@ public class DefaultCodegen {
*/ */
protected void initalizeSpecialCharacterMapping() { protected void initalizeSpecialCharacterMapping() {
// Initialize special characters // Initialize special characters
specialCharReplacements.put('$', "Dollar"); specialCharReplacements.put("$", "Dollar");
specialCharReplacements.put('^', "Caret"); specialCharReplacements.put("^", "Caret");
specialCharReplacements.put('|', "Pipe"); specialCharReplacements.put("|", "Pipe");
specialCharReplacements.put('=', "Equal"); specialCharReplacements.put("=", "Equal");
specialCharReplacements.put('*', "Star"); specialCharReplacements.put("*", "Star");
specialCharReplacements.put('-', "Minus"); specialCharReplacements.put("-", "Minus");
specialCharReplacements.put('&', "Ampersand"); specialCharReplacements.put("&", "Ampersand");
specialCharReplacements.put('%', "Percent"); specialCharReplacements.put("%", "Percent");
specialCharReplacements.put('#', "Hash"); specialCharReplacements.put("#", "Hash");
specialCharReplacements.put('@', "At"); specialCharReplacements.put("@", "At");
specialCharReplacements.put('!', "Exclamation"); specialCharReplacements.put("!", "Exclamation");
specialCharReplacements.put('+', "Plus"); specialCharReplacements.put("+", "Plus");
specialCharReplacements.put(':', "Colon"); specialCharReplacements.put(":", "Colon");
specialCharReplacements.put('>', "GreaterThan"); specialCharReplacements.put(">", "Greater_Than");
specialCharReplacements.put('<', "LessThan"); specialCharReplacements.put("<", "Less_Than");
specialCharReplacements.put("<=", "Less_Than_Or_Equal_To");
specialCharReplacements.put(">=", "Greater_Than_Or_Equal_To");
specialCharReplacements.put("!=", "Greater_Than_Or_Equal_To");
}
/**
* Return the symbol name of a symbol
*
* @param input Symbol (e.g. $)
* @return Symbol name (e.g. Dollar)
*/
protected String getSymbolName(String input) {
return specialCharReplacements.get(input);
} }
/** /**

View File

@@ -729,6 +729,11 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
@Override @Override
public String toEnumVarName(String value, String datatype) { public String toEnumVarName(String value, String datatype) {
// for symbol, e.g. $, #
if (getSymbolName(value) != null) {
return getSymbolName(value).toUpperCase();
}
// number // number
if ("Integer".equals(datatype) || "Long".equals(datatype) || if ("Integer".equals(datatype) || "Long".equals(datatype) ||
"Float".equals(datatype) || "Double".equals(datatype)) { "Float".equals(datatype) || "Double".equals(datatype)) {

View File

@@ -49,8 +49,10 @@ public class HaskellServantCodegen extends DefaultCodegen implements CodegenConf
public HaskellServantCodegen() { public HaskellServantCodegen() {
super(); super();
// override the mapping for "-" (Minus) to keep the original mapping in Haskell // override the mapping to keep the original mapping in Haskell
specialCharReplacements.put('-', "Dash"); specialCharReplacements.put("-", "Dash");
specialCharReplacements.put(">", "GreaterThan");
specialCharReplacements.put("<", "LessThan");
// set the output folder here // set the output folder here
outputFolder = "generated-code/haskell-servant"; outputFolder = "generated-code/haskell-servant";
@@ -203,9 +205,9 @@ public class HaskellServantCodegen extends DefaultCodegen implements CodegenConf
List<Map<String, Object>> replacements = new ArrayList<>(); List<Map<String, Object>> replacements = new ArrayList<>();
Object[] replacementChars = specialCharReplacements.keySet().toArray(); Object[] replacementChars = specialCharReplacements.keySet().toArray();
for(int i = 0; i < replacementChars.length; i++) { for(int i = 0; i < replacementChars.length; i++) {
Character c = (Character) replacementChars[i]; String c = (String) replacementChars[i];
Map<String, Object> o = new HashMap<>(); Map<String, Object> o = new HashMap<>();
o.put("char", Character.toString(c)); o.put("char", c);
o.put("replacement", "'" + specialCharReplacements.get(c)); o.put("replacement", "'" + specialCharReplacements.get(c));
o.put("hasMore", i != replacementChars.length - 1); o.put("hasMore", i != replacementChars.length - 1);
replacements.add(o); replacements.add(o);

View File

@@ -1146,11 +1146,11 @@ definitions:
EnumArrays: EnumArrays:
type: object type: object
properties: properties:
just_enum: just_symbol:
type: string type: string
enum: enum:
- bird - ">="
- eagle - "$"
array_enum: array_enum:
type: array type: array
items: items:

View File

@@ -4,16 +4,16 @@
## Properties ## Properties
Name | Type | Description | Notes Name | Type | Description | Notes
------------ | ------------- | ------------- | ------------- ------------ | ------------- | ------------- | -------------
**justEnum** | [**JustEnumEnum**](#JustEnumEnum) | | [optional] **justSymbol** | [**JustSymbolEnum**](#JustSymbolEnum) | | [optional]
**arrayEnum** | [**List&lt;ArrayEnumEnum&gt;**](#List&lt;ArrayEnumEnum&gt;) | | [optional] **arrayEnum** | [**List&lt;ArrayEnumEnum&gt;**](#List&lt;ArrayEnumEnum&gt;) | | [optional]
<a name="JustEnumEnum"></a> <a name="JustSymbolEnum"></a>
## Enum: JustEnumEnum ## Enum: JustSymbolEnum
Name | Value Name | Value
---- | ----- ---- | -----
BIRD | &quot;bird&quot; GREATER_THAN_OR_EQUAL_TO | &quot;&gt;&#x3D;&quot;
EAGLE | &quot;eagle&quot; DOLLAR | &quot;$&quot;
<a name="List<ArrayEnumEnum>"></a> <a name="List<ArrayEnumEnum>"></a>

View File

@@ -39,18 +39,18 @@ import java.util.List;
public class EnumArrays { public class EnumArrays {
/** /**
* Gets or Sets justEnum * Gets or Sets justSymbol
*/ */
public enum JustEnumEnum { public enum JustSymbolEnum {
@SerializedName("bird") @SerializedName(">=")
BIRD("bird"), GREATER_THAN_OR_EQUAL_TO(">="),
@SerializedName("eagle") @SerializedName("$")
EAGLE("eagle"); DOLLAR("$");
private String value; private String value;
JustEnumEnum(String value) { JustSymbolEnum(String value) {
this.value = value; this.value = value;
} }
@@ -60,8 +60,8 @@ public class EnumArrays {
} }
} }
@SerializedName("just_enum") @SerializedName("just_symbol")
private JustEnumEnum justEnum = null; private JustSymbolEnum justSymbol = null;
/** /**
* Gets or Sets arrayEnum * Gets or Sets arrayEnum
@@ -88,22 +88,22 @@ public class EnumArrays {
@SerializedName("array_enum") @SerializedName("array_enum")
private List<ArrayEnumEnum> arrayEnum = new ArrayList<ArrayEnumEnum>(); private List<ArrayEnumEnum> arrayEnum = new ArrayList<ArrayEnumEnum>();
public EnumArrays justEnum(JustEnumEnum justEnum) { public EnumArrays justSymbol(JustSymbolEnum justSymbol) {
this.justEnum = justEnum; this.justSymbol = justSymbol;
return this; return this;
} }
/** /**
* Get justEnum * Get justSymbol
* @return justEnum * @return justSymbol
**/ **/
@ApiModelProperty(example = "null", value = "") @ApiModelProperty(example = "null", value = "")
public JustEnumEnum getJustEnum() { public JustSymbolEnum getJustSymbol() {
return justEnum; return justSymbol;
} }
public void setJustEnum(JustEnumEnum justEnum) { public void setJustSymbol(JustSymbolEnum justSymbol) {
this.justEnum = justEnum; this.justSymbol = justSymbol;
} }
public EnumArrays arrayEnum(List<ArrayEnumEnum> arrayEnum) { public EnumArrays arrayEnum(List<ArrayEnumEnum> arrayEnum) {
@@ -139,13 +139,13 @@ public class EnumArrays {
return false; return false;
} }
EnumArrays enumArrays = (EnumArrays) o; EnumArrays enumArrays = (EnumArrays) o;
return Objects.equals(this.justEnum, enumArrays.justEnum) && return Objects.equals(this.justSymbol, enumArrays.justSymbol) &&
Objects.equals(this.arrayEnum, enumArrays.arrayEnum); Objects.equals(this.arrayEnum, enumArrays.arrayEnum);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(justEnum, arrayEnum); return Objects.hash(justSymbol, arrayEnum);
} }
@Override @Override
@@ -153,7 +153,7 @@ public class EnumArrays {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("class EnumArrays {\n"); sb.append("class EnumArrays {\n");
sb.append(" justEnum: ").append(toIndentedString(justEnum)).append("\n"); sb.append(" justSymbol: ").append(toIndentedString(justSymbol)).append("\n");
sb.append(" arrayEnum: ").append(toIndentedString(arrayEnum)).append("\n"); sb.append(" arrayEnum: ").append(toIndentedString(arrayEnum)).append("\n");
sb.append("}"); sb.append("}");
return sb.toString(); return sb.toString();

View File

@@ -112,7 +112,7 @@ removeFieldLabelPrefix forParsing prefix =
} }
where where
replaceSpecialChars field = foldl (&) field (map mkCharReplacement specialChars) replaceSpecialChars field = foldl (&) field (map mkCharReplacement specialChars)
specialChars = [("#", "'Hash"), ("!", "'Exclamation"), ("&", "'Ampersand"), ("@", "'At"), ("$", "'Dollar"), ("%", "'Percent"), ("*", "'Star"), ("+", "'Plus"), ("-", "'Dash"), (":", "'Colon"), ("^", "'Caret"), ("|", "'Pipe"), (">", "'GreaterThan"), ("=", "'Equal"), ("<", "'LessThan")] specialChars = [("#", "'Hash"), ("!", "'Exclamation"), ("&", "'Ampersand"), ("@", "'At"), ("$", "'Dollar"), ("%", "'Percent"), ("*", "'Star"), ("+", "'Plus"), (">=", "'Greater_Than_Or_Equal_To"), ("-", "'Dash"), ("<=", "'Less_Than_Or_Equal_To"), ("!=", "'Greater_Than_Or_Equal_To"), (":", "'Colon"), ("^", "'Caret"), ("|", "'Pipe"), (">", "'GreaterThan"), ("=", "'Equal"), ("<", "'LessThan")]
mkCharReplacement (replaceStr, searchStr) = T.unpack . replacer (T.pack searchStr) (T.pack replaceStr) . T.pack mkCharReplacement (replaceStr, searchStr) = T.unpack . replacer (T.pack searchStr) (T.pack replaceStr) . T.pack
replacer = if forParsing then flip T.replace else T.replace replacer = if forParsing then flip T.replace else T.replace