Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable per-property naming with the @DataObjectProperty annotation #212

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 0 additions & 43 deletions src/main/java/io/vertx/codegen/CamelCase.java

This file was deleted.

199 changes: 170 additions & 29 deletions src/main/java/io/vertx/codegen/Case.java
Original file line number Diff line number Diff line change
@@ -1,62 +1,203 @@
package io.vertx.codegen;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.regex.Pattern;

/**
* @author <a href="mailto:[email protected]">Julien Viet</a>
* @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<String> 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<String> parse(String name) {
String[] atoms = name.split("(?<!(^|[A-Z]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])");

if (atoms.length == 1 && atoms[0].isEmpty()) {
return Collections.emptyList();
} else {
return Arrays.asList(atoms);
}
}
},

/**
* Java full qualified case, for instance {@literal foo.bar}
* Camel case starting with a lower case, for instance {@literal fooBar}.
*/
public static final Case QUALIFIED = new QualifiedCase();
LOWER_CAMEL() {
@Override
public String format(Iterable<String> atoms) {
StringBuilder sb = new StringBuilder();
Iterator<String> 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<String> parse(final String name) {
String[] atoms = name.split("(?<!(^|[A-Z]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])");

if (atoms.length == 1 && atoms[0].isEmpty()) {
return Collections.emptyList();
} else {
return Arrays.asList(atoms);
}
}
},

/**
* Kebab case, for instance {@literal foo-bar}.
*/
public static final Case KEBAB = new KebabCase();
KEBAB() {
private final Pattern validator = Pattern.compile("(?:\\p{Alnum}|(?:(?<=\\p{Alnum})-(?=\\p{Alnum})))*");

@Override
public String format(Iterable<String> 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<String> 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<String> atoms) {
StringBuilder sb = new StringBuilder();

public String format(Iterable<String> 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<String> 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<String> 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<String> 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<String> 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<String> parse(String name) {
throw new UnsupportedOperationException();
}
public abstract List<String> 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
Expand All @@ -75,11 +216,11 @@ protected static List<String> 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<String, Case> vars() {
HashMap<String, Case> 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;
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/io/vertx/codegen/DataObjectModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Object> getVars() {
Map<String, Object> vars = Model.super.getVars();
Expand All @@ -168,6 +176,7 @@ public Map<String, Object> 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());
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -307,7 +317,6 @@ private void processConstructor(ExecutableElement constrElt) {
}



@SuppressWarnings("unchecked")
private void processMethods(List<ExecutableElement> methodsElt) {

Expand Down
39 changes: 0 additions & 39 deletions src/main/java/io/vertx/codegen/KebabCase.java

This file was deleted.

Loading