diff --git a/src/main/java/io/vertx/codegen/CamelCase.java b/src/main/java/io/vertx/codegen/CamelCase.java
deleted file mode 100644
index aba96e7a3..000000000
--- a/src/main/java/io/vertx/codegen/CamelCase.java
+++ /dev/null
@@ -1,43 +0,0 @@
-package io.vertx.codegen;
-
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-
-/**
- * @author Julien Viet
- */
-public class CamelCase extends Case {
-
- @Override
- public String name() {
- return "CAMEL";
- }
-
- @Override
- public String format(Iterable atoms) {
- StringBuilder sb = new StringBuilder();
- for (String atom : atoms) {
- if (atom.length() > 0) {
- char c = atom.charAt(0);
- if (Character.isLowerCase(c)) {
- sb.append(Character.toUpperCase(c));
- sb.append(atom, 1, atom.length());
- } else {
- sb.append(atom);
- }
- }
- }
- return sb.toString();
- }
-
- @Override
- public List parse(String name) {
- String[] atoms = name.split("(?Julien Viet
+ * @author Richard Gomez
*/
-public abstract class Case {
+public enum Case {
/**
- * Camel case starting with a lower case, for instance {@literal fooBar}.
+ * Camel case starting with an upper case, for instance {@literal FooBar}.
*/
- public static final Case LOWER_CAMEL = new LowerCamelCase();
+ UPPER_CAMEL() {
+ @Override
+ public String format(Iterable atoms) {
+ StringBuilder sb = new StringBuilder();
- /**
- * Camel case, for instance {@literal FooBar}.
- */
- public static final Case CAMEL = new CamelCase();
+ for (String atom : atoms) {
+ if (atom.length() > 0) {
+ char c = atom.charAt(0);
+ if (Character.isLowerCase(c)) {
+ sb.append(Character.toUpperCase(c));
+ sb.append(atom, 1, atom.length());
+ } else {
+ sb.append(atom);
+ }
+ }
+ }
+ return sb.toString();
+ }
+
+ @Override
+ public List parse(String name) {
+ String[] atoms = name.split("(? atoms) {
+ StringBuilder sb = new StringBuilder();
+ Iterator it = atoms.iterator();
+
+ // Ensure the first atom is lower case (e.g. ['Foo', 'Bar'] -> ['foo', 'Bar'])
+ while (it.hasNext()) {
+ String atom = it.next();
+ if (atom.length() > 0) {
+ sb.append(atom.toLowerCase());
+ break;
+ }
+ }
+
+ while (it.hasNext()) {
+ String atom = it.next();
+ if (atom.length() > 0) {
+ char c = atom.charAt(0);
+ if (Character.isLowerCase(c)) {
+ sb.append(Character.toUpperCase(c));
+ sb.append(atom, 1, atom.length());
+ } else {
+ sb.append(atom);
+ }
+ }
+ }
+ return sb.toString();
+ }
+
+ @Override
+ public List parse(final String name) {
+ String[] atoms = name.split("(? atoms) {
+ StringBuilder sb = new StringBuilder();
+
+ for (String atom : atoms) {
+ if (atom.length() > 0) {
+ if (sb.length() > 0) {
+ sb.append('-');
+ }
+ sb.append(atom.toLowerCase());
+ }
+ }
+ return sb.toString();
+ }
+
+ @Override
+ public List parse(String name) {
+ if (!validator.matcher(name).matches()) {
+ throw new IllegalArgumentException("Invalid kebab case:" + name);
+ }
+ return split(name, "\\-");
+ }
+ },
/**
* Snake case, for instance {@literal foo_bar}.
*/
- public static final Case SNAKE = new SnakeCase();
+ SNAKE() {
+ private final Pattern validator = Pattern.compile("(?:\\p{Alnum}|(?:(?<=\\p{Alnum})_(?=\\p{Alnum})))*");
- public String name() {
- throw new UnsupportedOperationException();
- }
+ @Override
+ public String format(Iterable atoms) {
+ StringBuilder sb = new StringBuilder();
- public String format(Iterable atoms) {
- throw new UnsupportedOperationException();
- }
+ for (String atom : atoms) {
+ if (atom.length() > 0) {
+ if (sb.length() > 0) {
+ sb.append('_');
+ }
+ sb.append(atom.toLowerCase());
+ }
+ }
+ return sb.toString();
+ }
+
+ @Override
+ public List parse(String name) {
+ if (!validator.matcher(name).matches()) {
+ throw new IllegalArgumentException("Invalid snake case:" + name);
+ }
+ return split(name, "_");
+ }
+ },
+
+ /**
+ * Java full qualified case, for instance {@literal foo.bar}
+ */
+ QUALIFIED() {
+ private final Pattern validator = Pattern.compile("(?:\\p{Alnum}|(?:(?<=\\p{Alnum})\\.(?=\\p{Alnum})))*");
+
+ @Override
+ public String format(Iterable atoms) {
+ StringBuilder sb = new StringBuilder();
+
+ for (String atom : atoms) {
+ if (atom.length() > 0) {
+ if (sb.length() > 0) {
+ sb.append('.');
+ }
+ sb.append(atom);
+ }
+ }
+ return sb.toString();
+ }
+
+ @Override
+ public List parse(String name) {
+ if (!validator.matcher(name).matches()) {
+ throw new IllegalArgumentException("Invalid qualified case:" + name);
+ }
+ return split(name, "\\.");
+ }
+ };
+
+
+ /**
+ * Format the {@code atoms} into the specified case.
+ *
+ * @param atoms the name atoms
+ * @return the name converted to the desired case
+ */
+ public abstract String format(Iterable atoms);
/**
* Parse the {@code name} argument and returns a list of the name atoms.
*
* @param name the name to parse
- * @return the name atoms
+ * @return the name atoms converted
* @throws IllegalArgumentException if the name has a syntax error
*/
- public List parse(String name) {
- throw new UnsupportedOperationException();
- }
+ public abstract List parse(String name);
/**
* Convert a name from this case to the {@literal dest} case
+ *
* @param dest the destination case
* @param name the name to convert
* @return the converted name
@@ -75,11 +216,11 @@ protected static List split(String s, String regex) {
}
/**
- * Useful for formatting or parsing string, eg:CASE_CAMEL.format(CASE_SNAKE.parse("foo_bar")),it will return fooBar
+ * Useful for formatting or parsing string, eg:CASE_UPPER_CAMEL.format(CASE_SNAKE.parse("foo_bar")),it will return fooBar
*/
public static Map vars() {
HashMap vars = new HashMap<>();
- for (Case _case : Arrays.asList(CAMEL, QUALIFIED, SNAKE, KEBAB, LOWER_CAMEL)) {
+ for (Case _case : Arrays.asList(UPPER_CAMEL, LOWER_CAMEL, KEBAB, SNAKE, QUALIFIED)) {
vars.put("CASE_" + _case.name(), _case);
}
return vars;
diff --git a/src/main/java/io/vertx/codegen/DataObjectModel.java b/src/main/java/io/vertx/codegen/DataObjectModel.java
index de5a49a81..2c9f71e8e 100644
--- a/src/main/java/io/vertx/codegen/DataObjectModel.java
+++ b/src/main/java/io/vertx/codegen/DataObjectModel.java
@@ -46,6 +46,7 @@ public class DataObjectModel implements Model {
private boolean generateConverter;
private boolean inheritConverter;
private boolean publicConverter;
+ private Case nameCase;
private int constructors;
private boolean deprecated;
private Text deprecatedDesc;
@@ -145,21 +146,28 @@ public boolean isPublicConverter() {
return publicConverter;
}
+ public Case getNameCase() {
+ return nameCase;
+ }
+
public boolean hasEmptyConstructor() {
return (constructors & 1) == 1;
}
+
/**
* @return {@code true} if the class has a {@code @Deprecated} annotation
*/
public boolean isDeprecated() {
return deprecated;
}
+
/**
* @return the description of deprecated
*/
public Text getDeprecatedDesc() {
return deprecatedDesc;
}
+
@Override
public Map getVars() {
Map vars = Model.super.getVars();
@@ -168,6 +176,7 @@ public Map getVars() {
vars.put("generateConverter", generateConverter);
vars.put("inheritConverter", inheritConverter);
vars.put("publicConverter", publicConverter);
+ vars.put("nameCase", nameCase);
vars.put("concrete", concrete);
vars.put("isClass", isClass);
vars.put("properties", propertyMap.values());
@@ -201,6 +210,7 @@ private void traverse() {
DataObject ann = modelElt.getAnnotation(DataObject.class);
this.generateConverter = ann.generateConverter();
this.publicConverter = ann.publicConverter();
+ this.nameCase = ann.nameCase();
this.inheritConverter = ann.inheritConverter();
this.isClass = modelElt.getKind() == ElementKind.CLASS;
this.concrete = isClass && !modelElt.getModifiers().contains(Modifier.ABSTRACT);
@@ -307,7 +317,6 @@ private void processConstructor(ExecutableElement constrElt) {
}
-
@SuppressWarnings("unchecked")
private void processMethods(List methodsElt) {
diff --git a/src/main/java/io/vertx/codegen/KebabCase.java b/src/main/java/io/vertx/codegen/KebabCase.java
deleted file mode 100644
index 7fc46c1ef..000000000
--- a/src/main/java/io/vertx/codegen/KebabCase.java
+++ /dev/null
@@ -1,39 +0,0 @@
-package io.vertx.codegen;
-
-import java.util.List;
-import java.util.regex.Pattern;
-
-/**
- * @author Julien Viet
- */
-public class KebabCase extends Case {
-
- private final Pattern validator = Pattern.compile("(?:\\p{Alnum}|(?:(?<=\\p{Alnum})-(?=\\p{Alnum})))*");
-
- @Override
- public String name() {
- return "KEBAB";
- }
-
- @Override
- public String format(Iterable atoms) {
- StringBuilder sb = new StringBuilder();
- for (String atom : atoms) {
- if (atom.length() > 0) {
- if (sb.length() > 0) {
- sb.append('-');
- }
- sb.append(atom.toLowerCase());
- }
- }
- return sb.toString();
- }
-
- @Override
- public List parse(String name) {
- if (!validator.matcher(name).matches()) {
- throw new IllegalArgumentException("Invalid kebab case:" + name);
- }
- return split(name, "\\-");
- }
-}
diff --git a/src/main/java/io/vertx/codegen/LowerCamelCase.java b/src/main/java/io/vertx/codegen/LowerCamelCase.java
deleted file mode 100644
index a4dbe0111..000000000
--- a/src/main/java/io/vertx/codegen/LowerCamelCase.java
+++ /dev/null
@@ -1,40 +0,0 @@
-package io.vertx.codegen;
-
-import java.util.Iterator;
-
-/**
- * @author Julien Viet
- */
-public class LowerCamelCase extends Case {
-
- @Override
- public String name() {
- return "LOWER_CAMEL";
- }
-
- @Override
- public String format(Iterable atoms) {
- StringBuilder sb = new StringBuilder();
- Iterator it = atoms.iterator();
- while (it.hasNext()) {
- String atom = it.next();
- if (atom.length() > 0) {
- sb.append(atom.toLowerCase());
- break;
- }
- }
- while (it.hasNext()) {
- String atom = it.next();
- if (atom.length() > 0) {
- char c = atom.charAt(0);
- if (Character.isLowerCase(c)) {
- sb.append(Character.toUpperCase(c));
- sb.append(atom, 1, atom.length());
- } else {
- sb.append(atom);
- }
- }
- }
- return sb.toString();
- }
-}
diff --git a/src/main/java/io/vertx/codegen/MethodInfo.java b/src/main/java/io/vertx/codegen/MethodInfo.java
index 1aad15c52..203badf53 100644
--- a/src/main/java/io/vertx/codegen/MethodInfo.java
+++ b/src/main/java/io/vertx/codegen/MethodInfo.java
@@ -78,7 +78,7 @@ public MethodInfo setName(String name) {
}
public String getName(Case _case) {
- return _case.format(Case.CAMEL.parse(name));
+ return _case.format(Case.UPPER_CAMEL.parse(name));
}
public MethodKind getKind() {
diff --git a/src/main/java/io/vertx/codegen/ParamInfo.java b/src/main/java/io/vertx/codegen/ParamInfo.java
index 684caeee8..8685817e5 100644
--- a/src/main/java/io/vertx/codegen/ParamInfo.java
+++ b/src/main/java/io/vertx/codegen/ParamInfo.java
@@ -54,7 +54,7 @@ public String getName() {
}
public String getName(Case _case) {
- return _case.format(Case.CAMEL.parse(name));
+ return _case.format(Case.UPPER_CAMEL.parse(name));
}
public Text getDescription() {
diff --git a/src/main/java/io/vertx/codegen/PropertyInfo.java b/src/main/java/io/vertx/codegen/PropertyInfo.java
index d94f65a17..f8845c89f 100644
--- a/src/main/java/io/vertx/codegen/PropertyInfo.java
+++ b/src/main/java/io/vertx/codegen/PropertyInfo.java
@@ -75,6 +75,17 @@ public String getName() {
return name;
}
+ /**
+ * @return the property name formatted as the specified case
+ */
+ public String getName(Case _case) {
+ if (_case == Case.LOWER_CAMEL) {
+ return name;
+ }
+
+ return Case.LOWER_CAMEL.to(_case, name);
+ }
+
/**
* @return the property type
*/
@@ -169,7 +180,6 @@ public boolean isJsonifiable() {
}
/**
- *
* @return {@code true} if the property has a {@code @Deprecated} annotation
*/
public boolean isDeprecated() {
diff --git a/src/main/java/io/vertx/codegen/QualifiedCase.java b/src/main/java/io/vertx/codegen/QualifiedCase.java
deleted file mode 100644
index 2c92d3ccc..000000000
--- a/src/main/java/io/vertx/codegen/QualifiedCase.java
+++ /dev/null
@@ -1,39 +0,0 @@
-package io.vertx.codegen;
-
-import java.util.List;
-import java.util.regex.Pattern;
-
-/**
- * @author Julien Viet
- */
-public class QualifiedCase extends Case {
-
- private final Pattern validator = Pattern.compile("(?:\\p{Alnum}|(?:(?<=\\p{Alnum})\\.(?=\\p{Alnum})))*");
-
- @Override
- public String name() {
- return "QUALIFIED";
- }
-
- @Override
- public String format(Iterable atoms) {
- StringBuilder sb = new StringBuilder();
- for (String atom : atoms) {
- if (atom.length() > 0) {
- if (sb.length() > 0) {
- sb.append('.');
- }
- sb.append(atom);
- }
- }
- return sb.toString();
- }
-
- @Override
- public List parse(String name) {
- if (!validator.matcher(name).matches()) {
- throw new IllegalArgumentException("Invalid qualified case:" + name);
- }
- return split(name, "\\.");
- }
-}
diff --git a/src/main/java/io/vertx/codegen/SnakeCase.java b/src/main/java/io/vertx/codegen/SnakeCase.java
deleted file mode 100644
index 8a8730153..000000000
--- a/src/main/java/io/vertx/codegen/SnakeCase.java
+++ /dev/null
@@ -1,37 +0,0 @@
-package io.vertx.codegen;
-
-import java.util.List;
-import java.util.regex.Pattern;
-
-/**
- * @author Julien Viet
- */
-public class SnakeCase extends Case {
-
- @Override
- public String name() {
- return "SNAKE";
- }
-
- @Override
- public String format(Iterable atoms) {
- StringBuilder sb = new StringBuilder();
- for (String atom : atoms) {
- if (atom.length() > 0) {
- if (sb.length() > 0) {
- sb.append('_');
- }
- sb.append(atom.toLowerCase());
- }
- }
- return sb.toString();
- }
- private final Pattern validator = Pattern.compile("(?:\\p{Alnum}|(?:(?<=\\p{Alnum})_(?=\\p{Alnum})))*");
- @Override
- public List parse(String name) {
- if (!validator.matcher(name).matches()) {
- throw new IllegalArgumentException("Invalid snake case:" + name);
- }
- return split(name, "_");
- }
-}
diff --git a/src/main/java/io/vertx/codegen/annotations/DataObject.java b/src/main/java/io/vertx/codegen/annotations/DataObject.java
index 24f964d77..000b92412 100644
--- a/src/main/java/io/vertx/codegen/annotations/DataObject.java
+++ b/src/main/java/io/vertx/codegen/annotations/DataObject.java
@@ -1,5 +1,7 @@
package io.vertx.codegen.annotations;
+import io.vertx.codegen.Case;
+
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@@ -72,4 +74,9 @@
* @return wether the generated converter should be public or package private
*/
boolean publicConverter() default true;
+
+ /**
+ * @return which case the converter should use for names
+ */
+ Case nameCase() default Case.LOWER_CAMEL;
}
diff --git a/src/main/java/io/vertx/codegen/annotations/DataObjectProperty.java b/src/main/java/io/vertx/codegen/annotations/DataObjectProperty.java
new file mode 100644
index 000000000..a7188c3c0
--- /dev/null
+++ b/src/main/java/io/vertx/codegen/annotations/DataObjectProperty.java
@@ -0,0 +1,22 @@
+package io.vertx.codegen.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Declares what case the json converter will use.
+ *
+ * @author Richard Gomez
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+public @interface DataObjectProperty {
+
+ /**
+ * @return the name used to serialize the property by the converter
+ */
+ String value();
+
+}
diff --git a/src/main/java/io/vertx/codegen/generators/dataobjecthelper/DataObjectHelperGen.java b/src/main/java/io/vertx/codegen/generators/dataobjecthelper/DataObjectHelperGen.java
index 78e3075c0..ac88121a6 100644
--- a/src/main/java/io/vertx/codegen/generators/dataobjecthelper/DataObjectHelperGen.java
+++ b/src/main/java/io/vertx/codegen/generators/dataobjecthelper/DataObjectHelperGen.java
@@ -1,8 +1,8 @@
package io.vertx.codegen.generators.dataobjecthelper;
-import io.vertx.codegen.Generator;
-import io.vertx.codegen.DataObjectModel;
-import io.vertx.codegen.PropertyInfo;
+import io.vertx.codegen.*;
+import io.vertx.codegen.annotations.DataObjectProperty;
+import io.vertx.codegen.type.AnnotationValueInfo;
import io.vertx.codegen.type.ClassKind;
import java.io.PrintWriter;
@@ -68,38 +68,38 @@ private void generateToJson(String visibility, boolean inheritConverter, DataObj
ClassKind propKind = prop.getType().getKind();
if (propKind.basic) {
if (propKind == ClassKind.STRING) {
- genPropToJson("", "", prop, writer);
+ genPropToJson("", "", prop, model.getNameCase(), writer);
} else {
switch (prop.getType().getSimpleName()) {
case "char":
case "Character":
- genPropToJson("Character.toString(", ")", prop, writer);
+ genPropToJson("Character.toString(", ")", prop, model.getNameCase(), writer);
break;
default:
- genPropToJson("", "", prop, writer);
+ genPropToJson("", "", prop, model.getNameCase(), writer);
}
}
} else {
switch (propKind) {
case API:
if (prop.getType().getName().equals("io.vertx.core.buffer.Buffer")) {
- genPropToJson("java.util.Base64.getEncoder().encodeToString(", ".getBytes())", prop, writer);
+ genPropToJson("java.util.Base64.getEncoder().encodeToString(", ".getBytes())", prop, model.getNameCase(), writer);
}
break;
case ENUM:
- genPropToJson("", ".name()", prop, writer);
+ genPropToJson("", ".name()", prop, model.getNameCase(), writer);
break;
case JSON_OBJECT:
case JSON_ARRAY:
case OBJECT:
- genPropToJson("", "", prop, writer);
+ genPropToJson("", "", prop, model.getNameCase(), writer);
break;
case DATA_OBJECT:
- genPropToJson("", ".toJson()", prop, writer);
+ genPropToJson("", ".toJson()", prop, model.getNameCase(), writer);
break;
case OTHER:
if (prop.getType().getName().equals(Instant.class.getName())) {
- genPropToJson("DateTimeFormatter.ISO_INSTANT.format(", ")", prop, writer);
+ genPropToJson("DateTimeFormatter.ISO_INSTANT.format(", ")", prop, model.getNameCase(), writer);
}
break;
}
@@ -110,19 +110,32 @@ private void generateToJson(String visibility, boolean inheritConverter, DataObj
writer.print(" }\n");
}
- private void genPropToJson(String before, String after, PropertyInfo prop, PrintWriter writer) {
+ private void genPropToJson(String before, String after, PropertyInfo prop, Case _case, PrintWriter writer) {
String indent = " ";
+
+ String name = prop.getName(_case);
+ AnnotationValueInfo propertyAnnotation = prop.getAnnotation(DataObjectProperty.class.getName());
+ if (propertyAnnotation != null) {
+ String propertyName = (String)propertyAnnotation.getMember("value");
+
+ if (propertyName.trim().length() == 0) {
+ throw new GenException(null, "DataObjectProperty value for " + prop.getName() + " cannot be empty");
+ }
+
+ name = propertyName;
+ }
+
if (prop.isList() || prop.isSet()) {
writer.print(indent + "if (obj." + prop.getGetterMethod() + "() != null) {\n");
writer.print(indent + " JsonArray array = new JsonArray();\n");
writer.print(indent + " obj." + prop.getGetterMethod() + "().forEach(item -> array.add(" + before + "item" + after + "));\n");
- writer.print(indent + " json.put(\"" + prop.getName() + "\", array);\n");
+ writer.print(indent + " json.put(\"" + name + "\", array);\n");
writer.print(indent + "}\n");
} else if (prop.isMap()) {
writer.print(indent + "if (obj." + prop.getGetterMethod() + "() != null) {\n");
writer.print(indent + " JsonObject map = new JsonObject();\n");
writer.print(indent + " obj." + prop.getGetterMethod() + "().forEach((key, value) -> map.put(key, " + before + "value" + after + "));\n");
- writer.print(indent + " json.put(\"" + prop.getName() + "\", map);\n");
+ writer.print(indent + " json.put(\"" + name + "\", map);\n");
writer.print(indent + "}\n");
} else {
String sp = "";
@@ -130,7 +143,7 @@ private void genPropToJson(String before, String after, PropertyInfo prop, Print
sp = " ";
writer.print(indent + "if (obj." + prop.getGetterMethod() + "() != null) {\n");
}
- writer.print(indent + sp + "json.put(\"" + prop.getName() + "\", " + before + "obj." + prop.getGetterMethod() + "()" + after + ");\n");
+ writer.print(indent + sp + "json.put(\"" + name + "\", " + before + "obj." + prop.getGetterMethod() + "()" + after + ");\n");
if (prop.getType().getKind() != ClassKind.PRIMITIVE) {
writer.print(indent + "}\n");
}
@@ -146,40 +159,40 @@ private void generateFromson(String visibility, boolean inheritConverter, DataOb
ClassKind propKind = prop.getType().getKind();
if (propKind.basic) {
if (propKind == ClassKind.STRING) {
- genPropFromJson("String", "(String)", "", prop, writer);
+ genPropFromJson("String", "(String)", "", prop, model.getNameCase(), writer);
} else {
switch (prop.getType().getSimpleName()) {
case "boolean":
case "Boolean":
- genPropFromJson("Boolean", "(Boolean)", "", prop, writer);
+ genPropFromJson("Boolean", "(Boolean)", "", prop, model.getNameCase(), writer);
break;
case "byte":
case "Byte":
- genPropFromJson("Number", "((Number)", ").byteValue()", prop, writer);
+ genPropFromJson("Number", "((Number)", ").byteValue()", prop, model.getNameCase(), writer);
break;
case "short":
case "Short":
- genPropFromJson("Number", "((Number)", ").shortValue()", prop, writer);
+ genPropFromJson("Number", "((Number)", ").shortValue()", prop, model.getNameCase(), writer);
break;
case "int":
case "Integer":
- genPropFromJson("Number", "((Number)", ").intValue()", prop, writer);
+ genPropFromJson("Number", "((Number)", ").intValue()", prop, model.getNameCase(), writer);
break;
case "long":
case "Long":
- genPropFromJson("Number", "((Number)", ").longValue()", prop, writer);
+ genPropFromJson("Number", "((Number)", ").longValue()", prop, model.getNameCase(), writer);
break;
case "float":
case "Float":
- genPropFromJson("Number", "((Number)", ").floatValue()", prop, writer);
+ genPropFromJson("Number", "((Number)", ").floatValue()", prop, model.getNameCase(), writer);
break;
case "double":
case "Double":
- genPropFromJson("Number", "((Number)", ").doubleValue()", prop, writer);
+ genPropFromJson("Number", "((Number)", ").doubleValue()", prop, model.getNameCase(), writer);
break;
case "char":
case "Character":
- genPropFromJson("String", "((String)", ").charAt(0)", prop, writer);
+ genPropFromJson("String", "((String)", ").charAt(0)", prop, model.getNameCase(), writer);
break;
}
}
@@ -187,27 +200,27 @@ private void generateFromson(String visibility, boolean inheritConverter, DataOb
switch (propKind) {
case API:
if (prop.getType().getName().equals("io.vertx.core.buffer.Buffer")) {
- genPropFromJson("String", "io.vertx.core.buffer.Buffer.buffer(java.util.Base64.getDecoder().decode((String)", "))", prop, writer);
+ genPropFromJson("String", "io.vertx.core.buffer.Buffer.buffer(java.util.Base64.getDecoder().decode((String)", "))", prop, model.getNameCase(), writer);
}
break;
case JSON_OBJECT:
- genPropFromJson("JsonObject", "((JsonObject)", ").copy()", prop, writer);
+ genPropFromJson("JsonObject", "((JsonObject)", ").copy()", prop, model.getNameCase(), writer);
break;
case JSON_ARRAY:
- genPropFromJson("JsonArray", "((JsonArray)", ").copy()", prop, writer);
+ genPropFromJson("JsonArray", "((JsonArray)", ").copy()", prop, model.getNameCase(), writer);
break;
case DATA_OBJECT:
- genPropFromJson("JsonObject", "new " + prop.getType().getName() + "((JsonObject)", ")", prop, writer);
+ genPropFromJson("JsonObject", "new " + prop.getType().getName() + "((JsonObject)", ")", prop, model.getNameCase(), writer);
break;
case ENUM:
- genPropFromJson("String", prop.getType().getName() + ".valueOf((String)", ")", prop, writer);
+ genPropFromJson("String", prop.getType().getName() + ".valueOf((String)", ")", prop, model.getNameCase(), writer);
break;
case OBJECT:
- genPropFromJson("Object", "", "", prop, writer);
+ genPropFromJson("Object", "", "", prop, model.getNameCase(), writer);
break;
case OTHER:
if (prop.getType().getName().equals(Instant.class.getName())) {
- genPropFromJson("String", "Instant.from(DateTimeFormatter.ISO_INSTANT.parse((String)", "))", prop, writer);
+ genPropFromJson("String", "Instant.from(DateTimeFormatter.ISO_INSTANT.parse((String)", "))", prop, model.getNameCase(), writer);
}
break;
default:
@@ -220,9 +233,22 @@ private void generateFromson(String visibility, boolean inheritConverter, DataOb
writer.print(" }\n");
}
- private void genPropFromJson(String cast, String before, String after, PropertyInfo prop, PrintWriter writer) {
+ private void genPropFromJson(String cast, String before, String after, PropertyInfo prop, Case _case, PrintWriter writer) {
String indent = " ";
- writer.print(indent + "case \"" + prop.getName() + "\":\n");
+
+ String name = prop.getName(_case);
+ AnnotationValueInfo propertyAnnotation = prop.getAnnotation(DataObjectProperty.class.getName());
+ if (propertyAnnotation != null) {
+ String propertyName = (String)propertyAnnotation.getMember("value");
+
+ if (propertyName.trim().length() == 0) {
+ throw new GenException(null, "DataObjectProperty value for " + prop.getName() + " cannot be empty");
+ }
+
+ name = propertyName;
+ }
+
+ writer.print(indent + "case \"" + name + "\":\n");
if (prop.isList() || prop.isSet()) {
writer.print(indent + " if (member.getValue() instanceof JsonArray) {\n");
if (prop.isSetter()) {
@@ -259,7 +285,7 @@ private void genPropFromJson(String cast, String before, String after, PropertyI
} else {
if (prop.isSetter()) {
writer.print(indent + " if (member.getValue() instanceof " + cast + ") {\n");
- writer.print(indent + " obj." + prop.getSetterMethod()+ "(" + before + "member.getValue()" + after + ");\n");
+ writer.print(indent + " obj." + prop.getSetterMethod() + "(" + before + "member.getValue()" + after + ");\n");
writer.print(indent + " }\n");
}
}
diff --git a/src/main/java/io/vertx/codegen/type/ClassTypeInfo.java b/src/main/java/io/vertx/codegen/type/ClassTypeInfo.java
index 8210b8090..b9ac23fdd 100644
--- a/src/main/java/io/vertx/codegen/type/ClassTypeInfo.java
+++ b/src/main/java/io/vertx/codegen/type/ClassTypeInfo.java
@@ -75,7 +75,7 @@ public String getPackageName() {
}
public String getSimpleName(Case _case) {
- return _case.format(Case.CAMEL.parse(simpleName));
+ return _case.format(Case.UPPER_CAMEL.parse(simpleName));
}
@Override
diff --git a/src/test/asciidoc/dataobjects.adoc b/src/test/asciidoc/dataobjects.adoc
index 44a8c69d6..3534b042e 100644
--- a/src/test/asciidoc/dataobjects.adoc
+++ b/src/test/asciidoc/dataobjects.adoc
@@ -44,6 +44,62 @@
|[[parentProperty]]`@parentProperty`|`String`|-
|===
+[[IndividualPropertyValueDataObject]]
+== IndividualPropertyValueDataObject
+
+++++
+++++
+'''
+
+[cols=">25%,25%,50%"]
+[frame="topbot"]
+|===
+^|Name | Type ^| Description
+|[[bazString]]`@bazString`|`String`|-
+|===
+
+[[IndividualPropertyValueEmptyDataObject]]
+== IndividualPropertyValueEmptyDataObject
+
+++++
+++++
+'''
+
+[cols=">25%,25%,50%"]
+[frame="topbot"]
+|===
+^|Name | Type ^| Description
+|[[bazString]]`@bazString`|`String`|-
+|===
+
+[[KebabCaseDataObject]]
+== KebabCaseDataObject
+
+++++
+++++
+'''
+
+[cols=">25%,25%,50%"]
+[frame="topbot"]
+|===
+^|Name | Type ^| Description
+|[[stringName]]`@stringName`|`String`|-
+|===
+
+[[LowerCamelCaseDataObject]]
+== LowerCamelCaseDataObject
+
+++++
+++++
+'''
+
+[cols=">25%,25%,50%"]
+[frame="topbot"]
+|===
+^|Name | Type ^| Description
+|[[stringName]]`@stringName`|`String`|-
+|===
+
[[NoConverterDataObject]]
== NoConverterDataObject
@@ -85,6 +141,20 @@
|[[values]]`@values`|`Array of String`|-
|===
+[[SnakeCaseDataObject]]
+== SnakeCaseDataObject
+
+++++
+++++
+'''
+
+[cols=">25%,25%,50%"]
+[frame="topbot"]
+|===
+^|Name | Type ^| Description
+|[[stringName]]`@stringName`|`String`|-
+|===
+
[[TestDataObject]]
== TestDataObject
@@ -190,3 +260,17 @@
|[[stringValues]]`@stringValues`|`Array of String`|-
|===
+[[UpperCamelCaseDataObject]]
+== UpperCamelCaseDataObject
+
+++++
+++++
+'''
+
+[cols=">25%,25%,50%"]
+[frame="topbot"]
+|===
+^|Name | Type ^| Description
+|[[stringName]]`@stringName`|`String`|-
+|===
+
diff --git a/src/test/generated/io/vertx/test/codegen/converter/IndividualPropertyValueDataObjectConverter.java b/src/test/generated/io/vertx/test/codegen/converter/IndividualPropertyValueDataObjectConverter.java
new file mode 100644
index 000000000..57f20f8fe
--- /dev/null
+++ b/src/test/generated/io/vertx/test/codegen/converter/IndividualPropertyValueDataObjectConverter.java
@@ -0,0 +1,65 @@
+package io.vertx.test.codegen.converter;
+
+import io.vertx.core.json.JsonObject;
+import io.vertx.core.json.JsonArray;
+import java.time.Instant;
+import java.time.format.DateTimeFormatter;
+
+/**
+ * Converter for {@link io.vertx.test.codegen.converter.IndividualPropertyValueDataObject}.
+ * NOTE: This class has been automatically generated from the {@link io.vertx.test.codegen.converter.IndividualPropertyValueDataObject} original class using Vert.x codegen.
+ */
+public class IndividualPropertyValueDataObjectConverter {
+
+ public static void fromJson(Iterable> json, IndividualPropertyValueDataObject obj) {
+ for (java.util.Map.Entry member : json) {
+ switch (member.getKey()) {
+ case "map-of-bar":
+ if (member.getValue() instanceof JsonObject) {
+ java.util.Map map = new java.util.LinkedHashMap<>();
+ ((Iterable>)member.getValue()).forEach(entry -> {
+ if (entry.getValue() instanceof Object)
+ map.put(entry.getKey(), entry.getValue());
+ });
+ obj.setBarMap(map);
+ }
+ break;
+ case "string_of_baz":
+ if (member.getValue() instanceof String) {
+ obj.setBazString((String)member.getValue());
+ }
+ break;
+ case "listOfFoo":
+ if (member.getValue() instanceof JsonArray) {
+ java.util.ArrayList list = new java.util.ArrayList<>();
+ ((Iterable