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 $
// They are translated to words like "Dollar" and prefixed with '
// 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() {
return cliOptions;
@@ -789,21 +789,35 @@ public class DefaultCodegen {
*/
protected void initalizeSpecialCharacterMapping() {
// Initialize special characters
specialCharReplacements.put('$', "Dollar");
specialCharReplacements.put('^', "Caret");
specialCharReplacements.put('|', "Pipe");
specialCharReplacements.put('=', "Equal");
specialCharReplacements.put('*', "Star");
specialCharReplacements.put('-', "Minus");
specialCharReplacements.put('&', "Ampersand");
specialCharReplacements.put('%', "Percent");
specialCharReplacements.put('#', "Hash");
specialCharReplacements.put('@', "At");
specialCharReplacements.put('!', "Exclamation");
specialCharReplacements.put('+', "Plus");
specialCharReplacements.put(':', "Colon");
specialCharReplacements.put('>', "GreaterThan");
specialCharReplacements.put('<', "LessThan");
specialCharReplacements.put("$", "Dollar");
specialCharReplacements.put("^", "Caret");
specialCharReplacements.put("|", "Pipe");
specialCharReplacements.put("=", "Equal");
specialCharReplacements.put("*", "Star");
specialCharReplacements.put("-", "Minus");
specialCharReplacements.put("&", "Ampersand");
specialCharReplacements.put("%", "Percent");
specialCharReplacements.put("#", "Hash");
specialCharReplacements.put("@", "At");
specialCharReplacements.put("!", "Exclamation");
specialCharReplacements.put("+", "Plus");
specialCharReplacements.put(":", "Colon");
specialCharReplacements.put(">", "Greater_Than");
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
public String toEnumVarName(String value, String datatype) {
// for symbol, e.g. $, #
if (getSymbolName(value) != null) {
return getSymbolName(value).toUpperCase();
}
// number
if ("Integer".equals(datatype) || "Long".equals(datatype) ||
"Float".equals(datatype) || "Double".equals(datatype)) {

View File

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

View File

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

View File

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

View File

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

View File

@@ -112,7 +112,7 @@ removeFieldLabelPrefix forParsing prefix =
}
where
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
replacer = if forParsing then flip T.replace else T.replace