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

Codegen formatter pluggabilty #371

Open
wants to merge 1 commit 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
Original file line number Diff line number Diff line change
Expand Up @@ -377,19 +377,6 @@ private Case getCase(DataObjectModel model) {
.stream().filter(ann -> ann.getName().equals(DataObject.class.getName()))
.findFirst().get();
ClassTypeInfo cti = (ClassTypeInfo) abc.getMember("jsonPropertyNameFormatter");
switch (cti.getName()) {
case "io.vertx.codegen.format.CamelCase":
return CamelCase.INSTANCE;
case "io.vertx.codegen.format.SnakeCase":
return SnakeCase.INSTANCE;
case "io.vertx.codegen.format.LowerCamelCase":
return LowerCamelCase.INSTANCE;
case "io.vertx.codegen.format.KebabCase":
return KebabCase.INSTANCE;
case "io.vertx.codegen.format.QualifiedCase":
return QualifiedCase.INSTANCE;
default:
throw new UnsupportedOperationException("Todo");
}
return Case.loadCase(cti.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,22 @@ public void testSnakeFormatted() {
Assert.assertEquals(expected, test);
}

@Test
public void testExternallyFormatted() {
ExternalCaseFormattedDataObject obj = new ExternalCaseFormattedDataObject();
JsonObject expected = new JsonObject()
.put("foo", "val1")
.put("foofooBar", "val2")
.put("foofooBarfooJuu", "val3");
ExternalCaseFormattedDataObjectConverter.fromJson(expected, obj);
Assert.assertEquals("val1", obj.getFoo());
Assert.assertEquals("val2", obj.getFooBar());
Assert.assertEquals("val3", obj.getFooBarJuu());
JsonObject test = new JsonObject();
ExternalCaseFormattedDataObjectConverter.toJson(obj, test);
Assert.assertEquals(expected, test);
}

@Test
public void testBase64Basic() {
TestDataObjectBase64Basic obj = new TestDataObjectBase64Basic();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2011-2017 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/

package io.vertx.test.codegen.converter;

import io.vertx.codegen.annotations.DataObject;
import io.vertx.codegen.format.SnakeCase;
import io.vertx.core.json.JsonObject;
import io.vertx.test.codegen.format.FooCase;

/**
* @author <a href="mailto:[email protected]">Julien Viet</a>
*/
@DataObject(generateConverter = true, jsonPropertyNameFormatter = FooCase.class)
public class ExternalCaseFormattedDataObject {

private String foo;
private String fooBar;
private String fooBarJuu;

public ExternalCaseFormattedDataObject() {
}

public ExternalCaseFormattedDataObject(JsonObject json) {
}

public String getFoo() {
return foo;
}

public ExternalCaseFormattedDataObject setFoo(String foo) {
this.foo = foo;
return this;
}

public String getFooBar() {
return fooBar;
}

public ExternalCaseFormattedDataObject setFooBar(String fooBar) {
this.fooBar = fooBar;
return this;
}

public String getFooBarJuu() {
return fooBarJuu;
}

public ExternalCaseFormattedDataObject setFooBarJuu(String fooBarJuu) {
this.fooBarJuu = fooBarJuu;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,51 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
* @author <a href="mailto:[email protected]">Julien Viet</a>
*/
public abstract class Case {

private static Case ERROR = new io.vertx.codegen.format.Case() {};

private static final ConcurrentMap<String, Case> CACHED_CASES = new ConcurrentHashMap<>();

/**
* Resolve a case given its Java FQN.
*
* @param name the case FQN
* @return the loaded case, {@code null} on errors
*/
public static Case loadCase(String name) {
Case found = CACHED_CASES.computeIfAbsent(name, n -> {
// Maybe use java util service loading mechanism
switch (name) {
case "io.vertx.codegen.format.CamelCase":
return CamelCase.INSTANCE;
case "io.vertx.codegen.format.SnakeCase":
return SnakeCase.INSTANCE;
case "io.vertx.codegen.format.LowerCamelCase":
return LowerCamelCase.INSTANCE;
case "io.vertx.codegen.format.KebabCase":
return KebabCase.INSTANCE;
case "io.vertx.codegen.format.QualifiedCase":
return QualifiedCase.INSTANCE;
default:
try {
Class<?> clazz = Case.class.getClassLoader().loadClass(name);
Case i = (Case) clazz.getConstructor().newInstance();
return i;
} catch (Exception e) {
return ERROR;
}
}
});
return found == ERROR ? null : found;
}

public String name() {
throw new UnsupportedOperationException();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.vertx.test.codegen.format;

import io.vertx.codegen.format.Case;

import java.util.ArrayList;
import java.util.List;

/**
* Chain everything with foo
*/
public class FooCase extends Case {

@Override
public String format(Iterable<String> atoms) {
List<String> names = new ArrayList<>();
for (String s : atoms) {
names.add(s);
}
return String.join("foo", names);
}
}