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

Add a new jsonproperty formatter for SCREAMING_SNAKE_CASE #368

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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.vertx.codegen.format.KebabCase;
import io.vertx.codegen.format.LowerCamelCase;
import io.vertx.codegen.format.QualifiedCase;
import io.vertx.codegen.format.ScreamingSnakeCase;
import io.vertx.codegen.format.SnakeCase;
import io.vertx.codegen.type.AnnotationValueInfo;
import io.vertx.codegen.type.ClassKind;
Expand Down Expand Up @@ -382,6 +383,8 @@ private Case getCase(DataObjectModel model) {
return CamelCase.INSTANCE;
case "io.vertx.codegen.format.SnakeCase":
return SnakeCase.INSTANCE;
case "io.vertx.codegen.format.ScreamingSnakeCase":
return ScreamingSnakeCase.INSTANCE;
case "io.vertx.codegen.format.LowerCamelCase":
return LowerCamelCase.INSTANCE;
case "io.vertx.codegen.format.KebabCase":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@
*/
public class DataObjectTest {

private static JsonObject toJson(Map<String, Object> map) {
JsonObject json = new JsonObject();
map.forEach(json::put);
return json;
}

@Test
public void testJsonToDataObject() throws Exception {

Expand Down Expand Up @@ -331,6 +325,16 @@ public void testJsonToDataObject() throws Exception {
// assertEquals(Collections.singletonList(aggregatedDataObject), obj.getAddedAggregatedDataObjects());
}

private String toBase64(Buffer buffer) {
return JsonUtil.BASE64_ENCODER.encodeToString(buffer.getBytes());
}

private static JsonObject toJson(Map<String, Object> map) {
JsonObject json = new JsonObject();
map.forEach(json::put);
return json;
}

@Test
public void testEmptyJsonToDataObject() {

Expand Down Expand Up @@ -819,10 +823,6 @@ public void testNotInherit() {
assertEquals(expectedJson, json);
}

private String toBase64(Buffer buffer) {
return JsonUtil.BASE64_ENCODER.encodeToString(buffer.getBytes());
}

@Test
public void testPreferSetterToAdder() {
SetterAdderDataObject obj = new SetterAdderDataObject();
Expand All @@ -849,6 +849,23 @@ public void testSnakeFormatted() {
Assert.assertEquals(expected, test);
}

@Test
public void testScreamingSnakeFormatted() {
ScreamingSnakeFormattedDataObject obj = new ScreamingSnakeFormattedDataObject();
JsonObject expected = new JsonObject()
.put("FOO", "val1")
.put("FOO_BAR", "val2")
.put("FOO_BAR_JUU", "val3");
ScreamingSnakeFormattedDataObjectConverter.fromJson(expected
, obj);
Assert.assertEquals("val1", obj.getFoo());
Assert.assertEquals("val2", obj.getFooBar());
Assert.assertEquals("val3", obj.getFooBarJuu());
JsonObject test = new JsonObject();
ScreamingSnakeFormattedDataObjectConverter.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,55 @@
/*
* 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.ScreamingSnakeCase;
import io.vertx.core.json.JsonObject;

@DataObject(generateConverter = true, jsonPropertyNameFormatter = ScreamingSnakeCase.class)
public class ScreamingSnakeFormattedDataObject {

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

public ScreamingSnakeFormattedDataObject() {}

public ScreamingSnakeFormattedDataObject(JsonObject json) {}

public String getFoo() {
return foo;
}

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

public String getFooBar() {
return fooBar;
}

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

public String getFooBarJuu() {
return fooBarJuu;
}

public ScreamingSnakeFormattedDataObject setFooBarJuu(String fooBarJuu) {
this.fooBarJuu = fooBarJuu;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.vertx.codegen.format;

import java.util.List;
import java.util.regex.Pattern;

/** Screaming Snake case, for instance {@literal FOO_BAR}. */
public class ScreamingSnakeCase extends Case {

/** A {@code SnakeCase} instance. */
public static final Case INSTANCE = new ScreamingSnakeCase();

private final Pattern validator =
Pattern.compile("(?:[\\p{Upper}\\d]|(?:(?<=[\\p{Upper}\\d])_(?=[\\p{Upper}\\d])))*");

@Override
public String name() {
return "SCREAMING_SNAKE";
}

@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.toUpperCase());
}
}
return sb.toString();
}

@Override
public List<String> parse(String name) {
if (!validator.matcher(name).matches()) {
throw new IllegalArgumentException("Invalid screaming snake case:" + name);
}
return split(name, "_");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.vertx.codegen.format.KebabCase;
import io.vertx.codegen.format.LowerCamelCase;
import io.vertx.codegen.format.QualifiedCase;
import io.vertx.codegen.format.ScreamingSnakeCase;
import io.vertx.codegen.format.SnakeCase;
import org.junit.Test;

Expand Down Expand Up @@ -130,6 +131,21 @@ public void testParseSnakeCase() {
}
}

@Test
public void testParseScreamingSnakeCase() {
parseScreamingSnakeCase("");
parseScreamingSnakeCase("FOO", "FOO");
parseScreamingSnakeCase("FOO_BAR", "FOO", "BAR");
parseScreamingSnakeCase("FOO_BAR_JUU", "FOO", "BAR", "JUU");
for (String test : Arrays.asList("_", "_FOO", "FOO_", "FOO__BAR")) {
try {
SnakeCase.INSTANCE.parse(test);
fail("Was expecting " + test + " to be rejected");
} catch (Exception ignore) {
}
}
}

@Test
public void testConversion() {
assertEquals("foo-bar-juu", CamelCase.INSTANCE.to(KebabCase.INSTANCE, "FooBarJuu"));
Expand Down Expand Up @@ -162,6 +178,10 @@ private void parseSnakeCase(String s, String... expected) {
parseCase(SnakeCase.INSTANCE, s, expected);
}

private void parseScreamingSnakeCase(String s, String... expected) {
parseCase(ScreamingSnakeCase.INSTANCE, s, expected);
}

private void parseCamelCase(String s, String... expected) {
parseCase(CamelCase.INSTANCE, s, expected);
}
Expand Down