Skip to content
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
@@ -0,0 +1,49 @@
package io.vertx.test.codegen.converter;

import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.impl.JsonUtil;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.Base64;

/**
* Converter and mapper for {@link io.vertx.test.codegen.converter.DataObjectPropertyFormattedDataObject}.
* NOTE: This class has been automatically generated from the {@link io.vertx.test.codegen.converter.DataObjectPropertyFormattedDataObject} original class using Vert.x codegen.
*/
public class DataObjectPropertyFormattedDataObjectConverter {


private static final Base64.Decoder BASE64_DECODER = JsonUtil.BASE64_DECODER;
private static final Base64.Encoder BASE64_ENCODER = JsonUtil.BASE64_ENCODER;

public static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, DataObjectPropertyFormattedDataObject obj) {
for (java.util.Map.Entry<String, Object> member : json) {
switch (member.getKey()) {
case "id":
if (member.getValue() instanceof String) {
obj.setId((String)member.getValue());
}
break;
case ":ref":
if (member.getValue() instanceof String) {
obj.setRef((String)member.getValue());
}
break;
}
}
}

public static void toJson(DataObjectPropertyFormattedDataObject obj, JsonObject json) {
toJson(obj, json.getMap());
}

public static void toJson(DataObjectPropertyFormattedDataObject obj, java.util.Map<String, Object> json) {
if (obj.getId() != null) {
json.put("id", obj.getId());
}
if (obj.getRef() != null) {
json.put(":ref", obj.getRef());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.core.json.JsonObject;

@DataObject(generateConverter = true)
public class DataObjectPropertyFormattedDataObject {

private String id;
private String ref;

public DataObjectPropertyFormattedDataObject() {
}

public DataObjectPropertyFormattedDataObject(JsonObject json) {
}

public String getId() {
return id;
}

public DataObjectPropertyFormattedDataObject setId(String id) {
this.id = id;
return this;
}

@DataObject.Property(name = ":ref")
public String getRef() {
return ref;
}

public DataObjectPropertyFormattedDataObject setRef(String ref) {
this.ref = ref;
return this;
}
}
12 changes: 12 additions & 0 deletions src/main/java/io/vertx/codegen/annotations/DataObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,16 @@
* @return if generated converters are enabled, buffers should default to the configured type.
*/
String base64Type() default "";

/**
* Methods marked with this annotation allow to specify their json field name individually.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface Property {
/**
* @return the field name to use for the property.
*/
String name();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ private void genToJson(String visibility, boolean inheritConverter, DataObjectMo
}

private void genPropToJson(String before, String after, PropertyInfo prop, PrintWriter writer) {
String jsonPropertyName = LowerCamelCase.INSTANCE.to(formatter, prop.getName());
String jsonPropertyName = getJsonPropertyName(prop);

String indent = " ";
if (prop.isList() || prop.isSet()) {
writer.print(indent + "if (obj." + prop.getGetterMethod() + "() != null) {\n");
Expand Down Expand Up @@ -320,7 +321,8 @@ private void genFromJson(String visibility, boolean inheritConverter, DataObject
}

private void genPropFromJson(String cast, String before, String after, PropertyInfo prop, PrintWriter writer) {
String jsonPropertyName = LowerCamelCase.INSTANCE.to(formatter, prop.getName());
String jsonPropertyName = getJsonPropertyName(prop);

String indent = " ";
writer.print(indent + "case \"" + jsonPropertyName + "\":\n");
if (prop.isList() || prop.isSet()) {
Expand Down Expand Up @@ -366,6 +368,14 @@ private void genPropFromJson(String cast, String before, String after, PropertyI
writer.print(indent + " break;\n");
}

private String getJsonPropertyName(PropertyInfo prop) {
return prop.getAnnotations().stream()
.filter(ann -> ann.getName().equals(DataObject.Property.class.getCanonicalName()))
.findFirst()
.map(ann -> (String) ann.getMember("name"))
.orElseGet(() -> LowerCamelCase.INSTANCE.to(formatter, prop.getName()));
}

private Case getCase(DataObjectModel model) {
AnnotationValueInfo abc = model
.getAnnotations()
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/io/vertx/test/codegen/converter/DataObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,21 @@ public void testSnakeFormatted() {
Assert.assertEquals(expected, test);
}

@Test
public void testDataObjectPropertyFormatted() {
DataObjectPropertyFormattedDataObject obj = new DataObjectPropertyFormattedDataObject();
JsonObject expected = new JsonObject()
.put("id", "val1")
.put(":ref", "self");
DataObjectPropertyFormattedDataObjectConverter.fromJson(expected
, obj);
Assert.assertEquals("val1", obj.getId());
Assert.assertEquals("self", obj.getRef());
JsonObject test = new JsonObject();
DataObjectPropertyFormattedDataObjectConverter.toJson(obj, test);
Assert.assertEquals(expected, test);
}

@Test
public void testBase64Basic() {
TestDataObjectBase64Basic obj = new TestDataObjectBase64Basic();
Expand Down