Byte buffer (#56)

* Fix byte buffer bug

* Bump geck version
This commit is contained in:
Pavel Popov 2017-08-18 11:38:06 +03:00 committed by GitHub
parent 3aef1bd355
commit e9da1dbcd2
10 changed files with 67 additions and 10 deletions

View File

@ -7,7 +7,7 @@
<parent>
<groupId>com.rbkmoney.geck</groupId>
<artifactId>parent</artifactId>
<version>0.6.1</version>
<version>0.6.2</version>
</parent>
<artifactId>common</artifactId>

View File

@ -7,7 +7,7 @@
<parent>
<groupId>com.rbkmoney.geck</groupId>
<artifactId>parent</artifactId>
<version>0.6.1</version>
<version>0.6.2</version>
</parent>
<artifactId>filter</artifactId>

View File

@ -7,7 +7,7 @@
<parent>
<groupId>com.rbkmoney.geck</groupId>
<artifactId>parent</artifactId>
<version>0.6.1</version>
<version>0.6.2</version>
</parent>
<artifactId>migrator</artifactId>

View File

@ -12,7 +12,7 @@
<groupId>com.rbkmoney.geck</groupId>
<artifactId>parent</artifactId>
<version>0.6.1</version>
<version>0.6.2</version>
<packaging>pom</packaging>
<modules>

View File

@ -7,7 +7,7 @@
<parent>
<groupId>com.rbkmoney.geck</groupId>
<artifactId>parent</artifactId>
<version>0.6.1</version>
<version>0.6.2</version>
</parent>
<artifactId>serializer</artifactId>

View File

@ -13,6 +13,7 @@ import org.apache.thrift.TUnion;
import org.apache.thrift.meta_data.*;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.*;
import static com.rbkmoney.geck.serializer.kit.EventFlags.*;
@ -310,7 +311,7 @@ public class TBaseHandler<R extends TBase> implements StructHandler<R> {
@Override
public void value(byte[] value) throws IOException {
value(value, ThriftType.BINARY);
value(ByteBuffer.wrap(value), ThriftType.BINARY);
}
@Override

View File

@ -11,6 +11,7 @@ import org.apache.thrift.TUnion;
import org.apache.thrift.meta_data.*;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@ -21,6 +22,16 @@ import java.util.Set;
*/
public class TBaseProcessor implements StructProcessor<TBase> {
private final boolean checkRequiredFields;
public TBaseProcessor() {
this(true);
}
public TBaseProcessor(boolean checkRequiredFields) {
this.checkRequiredFields = checkRequiredFields;
}
@Override
public <R> R process(TBase value, StructHandler<R> handler) throws IOException {
if (value == null) {
@ -68,7 +79,7 @@ public class TBaseProcessor implements StructProcessor<TBase> {
}
protected void processUnsetField(TFieldIdEnum tFieldIdEnum, FieldMetaData fieldMetaData, StructHandler handler) throws IOException {
if (fieldMetaData.requirementType == TFieldRequirementType.REQUIRED) {
if (checkRequiredFields && fieldMetaData.requirementType == TFieldRequirementType.REQUIRED) {
throw new IllegalStateException(String.format("field '%s' is required and must not be null", tFieldIdEnum.getFieldName()));
}
}
@ -105,7 +116,13 @@ public class TBaseProcessor implements StructProcessor<TBase> {
handler.value(object.toString());
break;
case BINARY:
handler.value((byte[]) object);
if (object instanceof byte[]) {
handler.value((byte[]) object);
} else if (object instanceof ByteBuffer) {
handler.value(((ByteBuffer) object).array());
} else {
throw new IllegalStateException(String.format("Unknown binary type, type='%s'", object.getClass().getName()));
}
break;
case LIST:
List list = TypeUtil.convertType(List.class, object);

View File

@ -3,6 +3,7 @@ package com.rbkmoney.geck.serializer.kit.mock;
import com.rbkmoney.geck.serializer.StructHandler;
import com.rbkmoney.geck.serializer.kit.tbase.TBaseHandler;
import com.rbkmoney.geck.serializer.kit.tbase.ThriftType;
import com.rbkmoney.geck.serializer.test.BinaryTest;
import com.rbkmoney.geck.serializer.test.TestObject;
import junit.framework.TestCase;
import org.apache.thrift.TBase;
@ -10,11 +11,14 @@ import org.apache.thrift.TFieldIdEnum;
import org.apache.thrift.TFieldRequirementType;
import org.apache.thrift.TUnion;
import org.apache.thrift.meta_data.FieldMetaData;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertTrue;
@ -28,10 +32,25 @@ public class MockTBaseProcessorTest {
public void requiredFieldsOnlyTest() throws IOException {
TestObject testObject = new TestObject();
testObject = new MockTBaseProcessor(MockMode.REQUIRED_ONLY).process(testObject, new TBaseHandler<>(TestObject.class));
TestCase.assertTrue(checkFields(testObject, true));
assertTrue(checkFields(testObject, true));
assertFalse(checkFields(testObject, false));
}
@Test
public void binaryTest() throws IOException {
BinaryTest binaryTest = new BinaryTest();
binaryTest = new MockTBaseProcessor(MockMode.REQUIRED_ONLY)
.process(binaryTest, new TBaseHandler<>(BinaryTest.class));
assertNotNull(binaryTest.getData());
assertNotNull(binaryTest.getDataInList());
binaryTest.getDataInList().stream().forEach(Assert::assertNotNull);
assertNotNull(binaryTest.getDataInSet());
binaryTest.getDataInSet().stream().forEach(Assert::assertNotNull);
assertNotNull(binaryTest.getDataInMap());
binaryTest.getDataInMap().keySet().stream().forEach(Assert::assertNotNull);
binaryTest.getDataInMap().values().stream().forEach(Assert::assertNotNull);
}
@Test
public void fieldHandlerTest() throws IOException {
String testValue = "KEK";

View File

@ -1,12 +1,16 @@
package com.rbkmoney.geck.serializer.kit.tbase;
import com.rbkmoney.geck.serializer.kit.mock.RandomValueGenerator;
import com.rbkmoney.geck.serializer.test.*;
import com.rbkmoney.geck.serializer.handler.HandlerStub;
import com.rbkmoney.geck.serializer.kit.mock.MockTBaseProcessor;
import org.apache.thrift.TBase;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static com.rbkmoney.geck.serializer.GeckTestUtil.getTestObject;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@ -58,4 +62,17 @@ public class TBaseProcessorTest {
}
@Test
public void binaryUnknownTypeDataTest() throws IOException {
BinaryTest binaryTest = new BinaryTest();
List byteArrayInList = new ArrayList<>();
byteArrayInList.add(5);
binaryTest.setFieldValue(BinaryTest._Fields.DATA_IN_LIST, byteArrayInList);
assertThatThrownBy(() -> new TBaseProcessor(false)
.process(binaryTest, new TBaseHandler(BinaryTest.class, TBaseHandler.Mode.PREFER_NAME, false)))
.hasMessage("Unknown binary type, type='java.lang.Integer'");
}
}

View File

@ -38,6 +38,9 @@ struct TUnionTest {
struct BinaryTest {
1: required binary data
2: required list<binary> dataInList
3: required set<binary> dataInSet
4: required map<binary, binary> dataInMap
}
struct MapListTest {
@ -85,4 +88,4 @@ struct Ids3 {
3: required i16 another_mini_id
2: required i32 another_id
1: required i64 another_big_id
}
}