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
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ dependencies {
compile group: 'com.uber.m3', name: 'tally-core', version: '0.2.3'
compile group: 'com.google.guava', name: 'guava', version: '27.0.1-jre'
compile group: 'com.cronutils', name: 'cron-utils', version: '8.0.0'
compile group: 'com.google.protobuf', name: 'protobuf-java', version: '3.6.1'
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'com.googlecode.junit-toolbox', name: 'junit-toolbox', version: '2.4'
testCompile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
Expand All @@ -66,7 +67,7 @@ compileJava {
dependsOn 'googleJavaFormat'
options.encoding = 'UTF-8'
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" << "-XepExcludedPaths:" +
".*/generated-sources/.*" << "-Werror"
".*/generated-sources/.*"
}

// Generation version.properties for value to be included into the request header
Expand All @@ -91,7 +92,7 @@ classes {
compileTestJava {
options.encoding = 'UTF-8'
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" << "-XepExcludedPaths:" +
".*/generated-sources/.*" << "-Werror"
".*/generated-sources/.*"
}

if (JavaVersion.current().isJava8Compatible()) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/uber/cadence/converter/JsonDataConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.google.protobuf.GeneratedMessageV3;
import java.io.IOException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.function.Function;
Expand Down Expand Up @@ -88,6 +90,11 @@ public byte[] toData(Object... values) throws DataConverterException {
try {
if (values.length == 1) {
Object value = values[0];
if (value instanceof GeneratedMessageV3) {
GeneratedMessageV3 protoObj = (GeneratedMessageV3) value;
return protoObj.toByteArray();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

String json = gson.toJson(value);
return json.getBytes(StandardCharsets.UTF_8);
}
Expand All @@ -101,12 +108,18 @@ public byte[] toData(Object... values) throws DataConverterException {
}

@Override
@SuppressWarnings("unchecked")
public <T> T fromData(byte[] content, Class<T> valueClass, Type valueType)
throws DataConverterException {
if (content == null) {
return null;
}
try {
if (GeneratedMessageV3.class.isAssignableFrom(valueClass)) {
Method m = valueClass.getMethod("parseFrom", byte[].class);
return (T) m.invoke(null, content);
}

return gson.fromJson(new String(content, StandardCharsets.UTF_8), valueType);
} catch (Exception e) {
throw new DataConverterException(content, new Type[] {valueType}, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

package com.uber.cadence.converter;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import com.uber.cadence.EventType;
import com.uber.cadence.History;
Expand Down Expand Up @@ -163,6 +164,23 @@ public void testThriftFieldsInPOJOArray() {
assertEquals(new String(converted, StandardCharsets.UTF_8), testData, fromConverted[1]);
}

@Test
public void testProto() {
TestProtos.Person person =
TestProtos.Person.newBuilder()
.setName("abc")
.setId(1)
.setEmail("[email protected]")
.addPhones(TestProtos.Person.PhoneNumber.newBuilder().setNumber("123456").build())
.build();
TestProtos.AddressBook book = TestProtos.AddressBook.newBuilder().addPeople(person).build();
byte[] converted = converter.toData(book);

TestProtos.AddressBook fromConverted =
converter.fromData(converted, TestProtos.AddressBook.class, TestProtos.AddressBook.class);
assertEquals(book, fromConverted);
}

public static void foo(List<UUID> arg) {}

@Test
Expand Down
Loading