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)member.getValue()).forEach( item -> { + if (item instanceof Object) + list.add(item); + }); + obj.setFooList(list); + } + break; + } + } + } + + public static void toJson(IndividualPropertyValueDataObject obj, JsonObject json) { + toJson(obj, json.getMap()); + } + + public static void toJson(IndividualPropertyValueDataObject obj, java.util.Map json) { + if (obj.getBarMap() != null) { + JsonObject map = new JsonObject(); + obj.getBarMap().forEach((key, value) -> map.put(key, value)); + json.put("map-of-bar", map); + } + if (obj.getBazString() != null) { + json.put("string_of_baz", obj.getBazString()); + } + if (obj.getFooList() != null) { + JsonArray array = new JsonArray(); + obj.getFooList().forEach(item -> array.add(item)); + json.put("listOfFoo", array); + } + } +} diff --git a/src/test/generated/io/vertx/test/codegen/converter/KebabCaseDataObjectConverter.java b/src/test/generated/io/vertx/test/codegen/converter/KebabCaseDataObjectConverter.java new file mode 100644 index 000000000..0090ab558 --- /dev/null +++ b/src/test/generated/io/vertx/test/codegen/converter/KebabCaseDataObjectConverter.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.KebabCaseDataObject}. + * NOTE: This class has been automatically generated from the {@link io.vertx.test.codegen.converter.KebabCaseDataObject} original class using Vert.x codegen. + */ +public class KebabCaseDataObjectConverter { + + public static void fromJson(Iterable> json, KebabCaseDataObject obj) { + for (java.util.Map.Entry member : json) { + switch (member.getKey()) { + case "list-name": + if (member.getValue() instanceof JsonArray) { + java.util.ArrayList list = new java.util.ArrayList<>(); + ((Iterable)member.getValue()).forEach( item -> { + if (item instanceof Object) + list.add(item); + }); + obj.setListName(list); + } + break; + case "map-name": + 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.setMapName(map); + } + break; + case "string-name": + if (member.getValue() instanceof String) { + obj.setStringName((String)member.getValue()); + } + break; + } + } + } + + public static void toJson(KebabCaseDataObject obj, JsonObject json) { + toJson(obj, json.getMap()); + } + + public static void toJson(KebabCaseDataObject obj, java.util.Map json) { + if (obj.getListName() != null) { + JsonArray array = new JsonArray(); + obj.getListName().forEach(item -> array.add(item)); + json.put("list-name", array); + } + if (obj.getMapName() != null) { + JsonObject map = new JsonObject(); + obj.getMapName().forEach((key, value) -> map.put(key, value)); + json.put("map-name", map); + } + if (obj.getStringName() != null) { + json.put("string-name", obj.getStringName()); + } + } +} diff --git a/src/test/generated/io/vertx/test/codegen/converter/LowerCamelCaseDataObjectConverter.java b/src/test/generated/io/vertx/test/codegen/converter/LowerCamelCaseDataObjectConverter.java new file mode 100644 index 000000000..8f2286470 --- /dev/null +++ b/src/test/generated/io/vertx/test/codegen/converter/LowerCamelCaseDataObjectConverter.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.LowerCamelCaseDataObject}. + * NOTE: This class has been automatically generated from the {@link io.vertx.test.codegen.converter.LowerCamelCaseDataObject} original class using Vert.x codegen. + */ +public class LowerCamelCaseDataObjectConverter { + + public static void fromJson(Iterable> json, LowerCamelCaseDataObject obj) { + for (java.util.Map.Entry member : json) { + switch (member.getKey()) { + case "listName": + if (member.getValue() instanceof JsonArray) { + java.util.ArrayList list = new java.util.ArrayList<>(); + ((Iterable)member.getValue()).forEach( item -> { + if (item instanceof Object) + list.add(item); + }); + obj.setListName(list); + } + break; + case "mapName": + 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.setMapName(map); + } + break; + case "stringName": + if (member.getValue() instanceof String) { + obj.setStringName((String)member.getValue()); + } + break; + } + } + } + + public static void toJson(LowerCamelCaseDataObject obj, JsonObject json) { + toJson(obj, json.getMap()); + } + + public static void toJson(LowerCamelCaseDataObject obj, java.util.Map json) { + if (obj.getListName() != null) { + JsonArray array = new JsonArray(); + obj.getListName().forEach(item -> array.add(item)); + json.put("listName", array); + } + if (obj.getMapName() != null) { + JsonObject map = new JsonObject(); + obj.getMapName().forEach((key, value) -> map.put(key, value)); + json.put("mapName", map); + } + if (obj.getStringName() != null) { + json.put("stringName", obj.getStringName()); + } + } +} diff --git a/src/test/generated/io/vertx/test/codegen/converter/SnakeCaseDataObjectConverter.java b/src/test/generated/io/vertx/test/codegen/converter/SnakeCaseDataObjectConverter.java new file mode 100644 index 000000000..cf8fa0aa9 --- /dev/null +++ b/src/test/generated/io/vertx/test/codegen/converter/SnakeCaseDataObjectConverter.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.SnakeCaseDataObject}. + * NOTE: This class has been automatically generated from the {@link io.vertx.test.codegen.converter.SnakeCaseDataObject} original class using Vert.x codegen. + */ +public class SnakeCaseDataObjectConverter { + + public static void fromJson(Iterable> json, SnakeCaseDataObject obj) { + for (java.util.Map.Entry member : json) { + switch (member.getKey()) { + case "list_name": + if (member.getValue() instanceof JsonArray) { + java.util.ArrayList list = new java.util.ArrayList<>(); + ((Iterable)member.getValue()).forEach( item -> { + if (item instanceof Object) + list.add(item); + }); + obj.setListName(list); + } + break; + case "map_name": + 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.setMapName(map); + } + break; + case "string_name": + if (member.getValue() instanceof String) { + obj.setStringName((String)member.getValue()); + } + break; + } + } + } + + public static void toJson(SnakeCaseDataObject obj, JsonObject json) { + toJson(obj, json.getMap()); + } + + public static void toJson(SnakeCaseDataObject obj, java.util.Map json) { + if (obj.getListName() != null) { + JsonArray array = new JsonArray(); + obj.getListName().forEach(item -> array.add(item)); + json.put("list_name", array); + } + if (obj.getMapName() != null) { + JsonObject map = new JsonObject(); + obj.getMapName().forEach((key, value) -> map.put(key, value)); + json.put("map_name", map); + } + if (obj.getStringName() != null) { + json.put("string_name", obj.getStringName()); + } + } +} diff --git a/src/test/generated/io/vertx/test/codegen/converter/UpperCamelCaseDataObjectConverter.java b/src/test/generated/io/vertx/test/codegen/converter/UpperCamelCaseDataObjectConverter.java new file mode 100644 index 000000000..8e695e197 --- /dev/null +++ b/src/test/generated/io/vertx/test/codegen/converter/UpperCamelCaseDataObjectConverter.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.UpperCamelCaseDataObject}. + * NOTE: This class has been automatically generated from the {@link io.vertx.test.codegen.converter.UpperCamelCaseDataObject} original class using Vert.x codegen. + */ +public class UpperCamelCaseDataObjectConverter { + + public static void fromJson(Iterable> json, UpperCamelCaseDataObject obj) { + for (java.util.Map.Entry member : json) { + switch (member.getKey()) { + case "ListName": + if (member.getValue() instanceof JsonArray) { + java.util.ArrayList list = new java.util.ArrayList<>(); + ((Iterable)member.getValue()).forEach( item -> { + if (item instanceof Object) + list.add(item); + }); + obj.setListName(list); + } + break; + case "MapName": + 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.setMapName(map); + } + break; + case "StringName": + if (member.getValue() instanceof String) { + obj.setStringName((String)member.getValue()); + } + break; + } + } + } + + public static void toJson(UpperCamelCaseDataObject obj, JsonObject json) { + toJson(obj, json.getMap()); + } + + public static void toJson(UpperCamelCaseDataObject obj, java.util.Map json) { + if (obj.getListName() != null) { + JsonArray array = new JsonArray(); + obj.getListName().forEach(item -> array.add(item)); + json.put("ListName", array); + } + if (obj.getMapName() != null) { + JsonObject map = new JsonObject(); + obj.getMapName().forEach((key, value) -> map.put(key, value)); + json.put("MapName", map); + } + if (obj.getStringName() != null) { + json.put("StringName", obj.getStringName()); + } + } +} diff --git a/src/test/java/io/vertx/test/codegen/CaseTest.java b/src/test/java/io/vertx/test/codegen/CaseTest.java index 86a2b6283..3bfaa8fad 100644 --- a/src/test/java/io/vertx/test/codegen/CaseTest.java +++ b/src/test/java/io/vertx/test/codegen/CaseTest.java @@ -13,7 +13,7 @@ public class CaseTest { @Test - public void testFormatCamelCase() { + public void testFormatUpperCamelCase() { formatCamelCase("", ""); formatCamelCase("Foo", "foo"); formatCamelCase("Foo", "Foo"); @@ -25,7 +25,7 @@ public void testFormatCamelCase() { } @Test - public void testParseCamelCase() { + public void testParseUpperCamelCase() { parseCamelCase(""); parseCamelCase("Foo", "Foo"); parseCamelCase("FooBar", "Foo", "Bar"); @@ -50,6 +50,19 @@ public void testFormatLowerCamelCase() { formatLowerCamelCase("fooBarJuu", "Foo", "Bar", "Juu"); } + @Test + public void testParseLowerCamelCase() { + parseLowerCamelCase(""); + parseLowerCamelCase("foo", "foo"); + parseLowerCamelCase("fooBar", "foo", "Bar"); + parseLowerCamelCase("fooBarJuu", "foo", "Bar", "Juu"); + parseLowerCamelCase("URL", "URL"); + parseLowerCamelCase("URLDecoder", "URL", "Decoder"); + parseLowerCamelCase("testSomething", "test", "Something"); + parseLowerCamelCase("testURL", "test", "URL"); + parseLowerCamelCase("test123", "test123"); + } + @Test public void testFormatQualifiedCase() { formatQualifiedCase("", ""); @@ -127,14 +140,14 @@ public void testParseSnakeCase() { @Test public void testConversion() { - assertEquals("foo-bar-juu", Case.CAMEL.to(Case.KEBAB, "FooBarJuu")); - assertEquals("foo_bar_juu", Case.CAMEL.to(Case.SNAKE, "FooBarJuu")); - assertEquals("FooBarJuu", Case.SNAKE.to(Case.CAMEL, "foo_bar_juu")); - assertEquals("FooBarJuu", Case.KEBAB.to(Case.CAMEL, "foo-bar-juu")); + assertEquals("foo-bar-juu", Case.UPPER_CAMEL.to(Case.KEBAB, "FooBarJuu")); + assertEquals("foo_bar_juu", Case.UPPER_CAMEL.to(Case.SNAKE, "FooBarJuu")); + assertEquals("FooBarJuu", Case.SNAKE.to(Case.UPPER_CAMEL, "foo_bar_juu")); + assertEquals("FooBarJuu", Case.KEBAB.to(Case.UPPER_CAMEL, "foo-bar-juu")); } private void formatCamelCase(String expected, String... atoms) { - assertCase(Case.CAMEL, expected, atoms); + assertCase(Case.UPPER_CAMEL, expected, atoms); } private void formatLowerCamelCase(String expected, String... atoms) { @@ -158,7 +171,11 @@ private void parseSnakeCase(String s, String... expected) { } private void parseCamelCase(String s, String... expected) { - parseCase(Case.CAMEL, s, expected); + parseCase(Case.UPPER_CAMEL, s, expected); + } + + private void parseLowerCamelCase(String s, String... expected) { + parseCase(Case.LOWER_CAMEL, s, expected); } private void parseQualifiedCase(String s, String... expected) { diff --git a/src/test/java/io/vertx/test/codegen/DataObjectTest.java b/src/test/java/io/vertx/test/codegen/DataObjectTest.java index 4146eeb93..deea0b8e4 100644 --- a/src/test/java/io/vertx/test/codegen/DataObjectTest.java +++ b/src/test/java/io/vertx/test/codegen/DataObjectTest.java @@ -1,19 +1,13 @@ package io.vertx.test.codegen; -import io.vertx.codegen.DataObjectModel; -import io.vertx.codegen.GenException; -import io.vertx.codegen.PropertyInfo; -import io.vertx.codegen.PropertyKind; +import io.vertx.codegen.*; import io.vertx.codegen.doc.Doc; -import io.vertx.codegen.type.AnnotationValueInfo; -import io.vertx.codegen.type.ClassTypeInfo; -import io.vertx.codegen.type.TypeInfo; -import io.vertx.codegen.type.TypeReflectionFactory; +import io.vertx.codegen.type.*; import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonObject; import io.vertx.test.codegen.annotations.EmptyAnnotation; -import io.vertx.test.codegen.testdataobject.Foo; import io.vertx.test.codegen.annotations.TestEnum; +import io.vertx.test.codegen.converter.UpperCamelCaseDataObject; import io.vertx.test.codegen.testapi.InterfaceDataObject; import io.vertx.test.codegen.testdataobject.*; import io.vertx.test.codegen.testdataobject.imported.Imported; @@ -180,6 +174,7 @@ private void testPropertyCollectionGettersSetters(Class dataObjectClass, Prop public void testPropertyMapGettersAdders() throws Exception { } + @Test public void testPropertyMapGettersSetters() throws Exception { DataObjectModel model = new GeneratorHelper().generateDataObject(PropertyMapGettersSetters.class); @@ -834,6 +829,13 @@ public void testDataObjectWithAnnotations() throws Exception { assertNotNull(fieldWithMethodAnnotationModel.getAnnotation(SomeMethodAnnotation.class.getName()).getName()); } + @Test + public void testConverterCaseDataObject() throws Exception { + DataObjectModel model = new GeneratorHelper().generateDataObject(UpperCamelCaseDataObject.class); + assertTrue(model.getGenerateConverter()); + assertEquals(Case.UPPER_CAMEL, model.getNameCase()); + } + private void assertInvalidDataObject(Class dataObjectClass) throws Exception { try { new GeneratorHelper().generateDataObject(dataObjectClass); diff --git a/src/test/java/io/vertx/test/codegen/converter/DataObjectTest.java b/src/test/java/io/vertx/test/codegen/converter/DataObjectTest.java index 191363e52..f2bcbacaa 100644 --- a/src/test/java/io/vertx/test/codegen/converter/DataObjectTest.java +++ b/src/test/java/io/vertx/test/codegen/converter/DataObjectTest.java @@ -16,11 +16,12 @@ import io.vertx.core.json.JsonObject; import org.junit.Test; -import static org.junit.Assert.*; - import java.util.*; import java.util.concurrent.TimeUnit; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + /** * @author Julien Viet */ @@ -734,4 +735,218 @@ public void testPreferSetterToAdder() { assertEquals(1, obj.sets); assertEquals(0, obj.adds); } + + @Test + public void testUpperCamelCaseFromJson() { + List list = new ArrayList<>(); + list.add("foo"); + Map map = new HashMap<>(); + map.put("foo", "bar"); + String string = "foo"; + + JsonObject json = new JsonObject(); + json.put("ListName", new JsonArray(list)); + json.put("MapName", new JsonObject(map)); + json.put("StringName", string); + + UpperCamelCaseDataObject obj = new UpperCamelCaseDataObject(); + UpperCamelCaseDataObjectConverter.fromJson(json, obj); + + assertEquals(list, obj.getListName()); + assertEquals(map, obj.getMapName()); + assertEquals(string, obj.getStringName()); + } + + @Test + public void testUpperCamelCaseToJson() { + List list = new ArrayList<>(); + list.add("foo"); + Map map = new HashMap<>(); + map.put("foo", "bar"); + String string = "foo"; + + UpperCamelCaseDataObject obj = new UpperCamelCaseDataObject(); + obj.setListName(list); + obj.setMapName(map); + obj.setStringName(string); + + Map json = new HashMap<>(); + UpperCamelCaseDataObjectConverter.toJson(obj, json); + + assertEquals(new JsonArray(list), json.get("ListName")); + assertEquals(new JsonObject(map), json.get("MapName")); + assertEquals(string, json.get("StringName")); + assertEquals(3, json.size()); + } + + @Test + public void testLowerCamelCaseFromJson() { + List list = new ArrayList<>(); + list.add("foo"); + Map map = new HashMap<>(); + map.put("foo", "bar"); + String string = "foo"; + + JsonObject json = new JsonObject(); + json.put("listName", new JsonArray(list)); + json.put("mapName", new JsonObject(map)); + json.put("stringName", string); + + LowerCamelCaseDataObject obj = new LowerCamelCaseDataObject(json); + LowerCamelCaseDataObjectConverter.fromJson(json, obj); + + assertEquals(list, obj.getListName()); + assertEquals(map, obj.getMapName()); + assertEquals(string, obj.getStringName()); + } + + @Test + public void testLowerCamelCaseToJson() { + List list = new ArrayList<>(); + list.add("foo"); + Map map = new HashMap<>(); + map.put("foo", "bar"); + String string = "foo"; + + LowerCamelCaseDataObject obj = new LowerCamelCaseDataObject(); + obj.setListName(list); + obj.setMapName(map); + obj.setStringName(string); + + Map json = new HashMap<>(); + LowerCamelCaseDataObjectConverter.toJson(obj, json); + + assertEquals(new JsonArray(list), json.get("listName")); + assertEquals(new JsonObject(map), json.get("mapName")); + assertEquals(string, json.get("stringName")); + assertEquals(3, json.size()); + } + + @Test + public void testKebabCaseFromJson() { + List list = new ArrayList<>(); + list.add("foo"); + Map map = new HashMap<>(); + map.put("foo", "bar"); + String string = "foo"; + + JsonObject json = new JsonObject(); + json.put("list-name", new JsonArray(list)); + json.put("map-name", new JsonObject(map)); + json.put("string-name", string); + + KebabCaseDataObject obj = new KebabCaseDataObject(json); + KebabCaseDataObjectConverter.fromJson(json, obj); + + assertEquals(list, obj.getListName()); + assertEquals(map, obj.getMapName()); + assertEquals(string, obj.getStringName()); + } + + @Test + public void testKebabCaseToJson() { + List list = new ArrayList<>(); + list.add("foo"); + Map map = new HashMap<>(); + map.put("foo", "bar"); + String string = "foo"; + + KebabCaseDataObject obj = new KebabCaseDataObject(); + obj.setListName(list); + obj.setMapName(map); + obj.setStringName(string); + + Map json = new HashMap<>(); + KebabCaseDataObjectConverter.toJson(obj, json); + + assertEquals(new JsonArray(list), json.get("list-name")); + assertEquals(new JsonObject(map), json.get("map-name")); + assertEquals(string, json.get("string-name")); + assertEquals(3, json.size()); + } + + @Test + public void testSnakeCaseFromJson() { + List list = new ArrayList<>(); + list.add("foo"); + Map map = new HashMap<>(); + map.put("foo", "bar"); + String string = "foo"; + + JsonObject json = new JsonObject(); + json.put("list_name", new JsonArray(list)); + json.put("map_name", new JsonObject(map)); + json.put("string_name", string); + + SnakeCaseDataObject obj = new SnakeCaseDataObject(json); + SnakeCaseDataObjectConverter.fromJson(json, obj); + + assertEquals(list, obj.getListName()); + assertEquals(map, obj.getMapName()); + assertEquals(string, obj.getStringName()); + } + + @Test + public void testSnakeCaseToJson() { + List list = new ArrayList<>(); + list.add("foo"); + Map map = new HashMap<>(); + map.put("foo", "bar"); + String string = "foo"; + + SnakeCaseDataObject obj = new SnakeCaseDataObject(); + obj.setListName(list); + obj.setMapName(map); + obj.setStringName(string); + + Map json = new HashMap<>(); + SnakeCaseDataObjectConverter.toJson(obj, json); + + assertEquals(new JsonArray(list), json.get("list_name")); + assertEquals(new JsonObject(map), json.get("map_name")); + assertEquals(string, json.get("string_name")); + assertEquals(3, json.size()); + } + + @Test + public void testIndividualPropertyValueFromJson() { + List list = new ArrayList<>(); + list.add("foo"); + Map map = new HashMap<>(); + map.put("bar", "bar"); + String string = "baz"; + + JsonObject json = new JsonObject(); + json.put("listOfFoo", new JsonArray(list)); + json.put("map-of-bar", new JsonObject(map)); + json.put("string_of_baz", string); + + IndividualPropertyValueDataObject obj = new IndividualPropertyValueDataObject(json); + IndividualPropertyValueDataObjectConverter.fromJson(json, obj); + + assertEquals(list, obj.getFooList()); + assertEquals(map, obj.getBarMap()); + assertEquals(string, obj.getBazString()); + } + + @Test + public void testIndividualPropertyToJson() { + List list = new ArrayList<>(); + list.add("foo"); + Map map = new HashMap<>(); + map.put("bar", "bar"); + String string = "baz"; + + IndividualPropertyValueDataObject obj = new IndividualPropertyValueDataObject(); + obj.setFooList(list); + obj.setBarMap(map); + obj.setBazString(string); + + Map json = new HashMap<>(); + IndividualPropertyValueDataObjectConverter.toJson(obj, json); + + assertEquals(new JsonArray(list), json.get("listOfFoo")); + assertEquals(new JsonObject(map), json.get("map-of-bar")); + assertEquals(string, json.get("string_of_baz")); + } } diff --git a/src/test/java/io/vertx/test/codegen/converter/IndividualPropertyValueDataObject.java b/src/test/java/io/vertx/test/codegen/converter/IndividualPropertyValueDataObject.java new file mode 100644 index 000000000..ca1867f37 --- /dev/null +++ b/src/test/java/io/vertx/test/codegen/converter/IndividualPropertyValueDataObject.java @@ -0,0 +1,55 @@ +package io.vertx.test.codegen.converter; + +import io.vertx.codegen.annotations.DataObject; +import io.vertx.codegen.annotations.DataObjectProperty; +import io.vertx.core.json.JsonObject; + +import java.util.List; +import java.util.Map; + +/** + * @author Richard Gomez + */ +@DataObject(generateConverter = true) +public class IndividualPropertyValueDataObject { + + @DataObjectProperty("listOfFoo") + private List fooList; + @DataObjectProperty("map-of-bar") + private Map barMap; + @DataObjectProperty("string_of_baz") + private String bazString; + + public IndividualPropertyValueDataObject() { + } + + public IndividualPropertyValueDataObject(IndividualPropertyValueDataObject copy) { + } + + public IndividualPropertyValueDataObject(JsonObject json) { + } + + public List getFooList() { + return fooList; + } + + public void setFooList(List fooList) { + this.fooList = fooList; + } + + public Map getBarMap() { + return barMap; + } + + public void setBarMap(Map barMap) { + this.barMap = barMap; + } + + public String getBazString() { + return bazString; + } + + public void setBazString(String bazString) { + this.bazString = bazString; + } +} diff --git a/src/test/java/io/vertx/test/codegen/converter/IndividualPropertyValueEmptyDataObject.java b/src/test/java/io/vertx/test/codegen/converter/IndividualPropertyValueEmptyDataObject.java new file mode 100644 index 000000000..aef7da47a --- /dev/null +++ b/src/test/java/io/vertx/test/codegen/converter/IndividualPropertyValueEmptyDataObject.java @@ -0,0 +1,53 @@ +package io.vertx.test.codegen.converter; + +import io.vertx.codegen.annotations.DataObject; +import io.vertx.codegen.annotations.DataObjectProperty; +import io.vertx.core.json.JsonObject; + +import java.util.List; +import java.util.Map; + +/** + * @author Richard Gomez + */ +@DataObject() +public class IndividualPropertyValueEmptyDataObject { + + @DataObjectProperty("") + private List fooList; + private Map barMap; + private String bazString; + + public IndividualPropertyValueEmptyDataObject() { + } + + public IndividualPropertyValueEmptyDataObject(IndividualPropertyValueEmptyDataObject copy) { + } + + public IndividualPropertyValueEmptyDataObject(JsonObject json) { + } + + public List getFooList() { + return fooList; + } + + public void setFooList(List fooList) { + this.fooList = fooList; + } + + public Map getBarMap() { + return barMap; + } + + public void setBarMap(Map barMap) { + this.barMap = barMap; + } + + public String getBazString() { + return bazString; + } + + public void setBazString(String bazString) { + this.bazString = bazString; + } +} diff --git a/src/test/java/io/vertx/test/codegen/converter/KebabCaseDataObject.java b/src/test/java/io/vertx/test/codegen/converter/KebabCaseDataObject.java new file mode 100644 index 000000000..2e04c64aa --- /dev/null +++ b/src/test/java/io/vertx/test/codegen/converter/KebabCaseDataObject.java @@ -0,0 +1,53 @@ +package io.vertx.test.codegen.converter; + +import io.vertx.codegen.Case; +import io.vertx.codegen.annotations.DataObject; +import io.vertx.core.json.JsonObject; + +import java.util.List; +import java.util.Map; + +/** + * @author Richard Gomez + */ +@DataObject(generateConverter = true, nameCase = Case.KEBAB) +public class KebabCaseDataObject { + + private List listName; + private Map mapName; + private String stringName; + + public KebabCaseDataObject() { + } + + public KebabCaseDataObject(KebabCaseDataObject copy) { + } + + public KebabCaseDataObject(JsonObject json) { + } + + public List getListName() { + return listName; + } + + public void setListName(List listName) { + this.listName = listName; + } + + public Map getMapName() { + return mapName; + } + + public void setMapName(Map mapName) { + this.mapName = mapName; + } + + public String getStringName() { + return stringName; + } + + public void setStringName(String stringName) { + this.stringName = stringName; + } + +} diff --git a/src/test/java/io/vertx/test/codegen/converter/LowerCamelCaseDataObject.java b/src/test/java/io/vertx/test/codegen/converter/LowerCamelCaseDataObject.java new file mode 100644 index 000000000..66f9442b3 --- /dev/null +++ b/src/test/java/io/vertx/test/codegen/converter/LowerCamelCaseDataObject.java @@ -0,0 +1,52 @@ +package io.vertx.test.codegen.converter; + +import io.vertx.codegen.Case; +import io.vertx.codegen.annotations.DataObject; +import io.vertx.core.json.JsonObject; + +import java.util.List; +import java.util.Map; + +/** + * @author Richard Gomez + */ +@DataObject(generateConverter = true, nameCase = Case.LOWER_CAMEL) +public class LowerCamelCaseDataObject { + + private List listName; + private Map mapName; + private String stringName; + + public LowerCamelCaseDataObject() { + } + + public LowerCamelCaseDataObject(LowerCamelCaseDataObject copy) { + } + + public LowerCamelCaseDataObject(JsonObject json) { + } + + public List getListName() { + return listName; + } + + public void setListName(List listName) { + this.listName = listName; + } + + public Map getMapName() { + return mapName; + } + + public void setMapName(Map mapName) { + this.mapName = mapName; + } + + public String getStringName() { + return stringName; + } + + public void setStringName(String stringName) { + this.stringName = stringName; + } +} diff --git a/src/test/java/io/vertx/test/codegen/converter/SnakeCaseDataObject.java b/src/test/java/io/vertx/test/codegen/converter/SnakeCaseDataObject.java new file mode 100644 index 000000000..60a13a2e0 --- /dev/null +++ b/src/test/java/io/vertx/test/codegen/converter/SnakeCaseDataObject.java @@ -0,0 +1,53 @@ +package io.vertx.test.codegen.converter; + +import io.vertx.codegen.Case; +import io.vertx.codegen.annotations.DataObject; +import io.vertx.core.json.JsonObject; + +import java.util.List; +import java.util.Map; + +/** + * @author Richard Gomez + */ +@DataObject(generateConverter = true, nameCase = Case.SNAKE) +public class SnakeCaseDataObject { + + private List listName; + private Map mapName; + private String stringName; + + public SnakeCaseDataObject() { + } + + public SnakeCaseDataObject(SnakeCaseDataObject copy) { + } + + public SnakeCaseDataObject(JsonObject json) { + } + + public List getListName() { + return listName; + } + + public void setListName(List listName) { + this.listName = listName; + } + + public Map getMapName() { + return mapName; + } + + public void setMapName(Map mapName) { + this.mapName = mapName; + } + + public String getStringName() { + return stringName; + } + + public void setStringName(String stringName) { + this.stringName = stringName; + } + +} diff --git a/src/test/java/io/vertx/test/codegen/converter/UpperCamelCaseDataObject.java b/src/test/java/io/vertx/test/codegen/converter/UpperCamelCaseDataObject.java new file mode 100644 index 000000000..233dfc8e9 --- /dev/null +++ b/src/test/java/io/vertx/test/codegen/converter/UpperCamelCaseDataObject.java @@ -0,0 +1,53 @@ +package io.vertx.test.codegen.converter; + +import io.vertx.codegen.Case; +import io.vertx.codegen.annotations.DataObject; +import io.vertx.core.json.JsonObject; + +import java.util.List; +import java.util.Map; + +/** + * @author Richard Gomez + */ +@DataObject(generateConverter = true, nameCase = Case.UPPER_CAMEL) +public class UpperCamelCaseDataObject { + + private List listName; + private Map mapName; + private String stringName; + + public UpperCamelCaseDataObject() { + } + + public UpperCamelCaseDataObject(UpperCamelCaseDataObject copy) { + } + + public UpperCamelCaseDataObject(JsonObject json) { + } + + public List getListName() { + return listName; + } + + public void setListName(List listName) { + this.listName = listName; + } + + public Map getMapName() { + return mapName; + } + + public void setMapName(Map mapName) { + this.mapName = mapName; + } + + public String getStringName() { + return stringName; + } + + public void setStringName(String stringName) { + this.stringName = stringName; + } + +}