Skip to content

Commit

Permalink
Add StringValueToByteArray converter
Browse files Browse the repository at this point in the history
Closes #230
  • Loading branch information
ArtDu committed Jun 7, 2022
1 parent 76ed92d commit 42999fe
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [Unreleased]

- Add StringValueToByteArray converter ([#230](https://github.com/tarantool/cartridge-java/issues/230))

## [0.8.0] - 2022-05-20

### Features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.tarantool.driver.mappers.converters.value.DefaultIntegerValueToDoubleConverter;
import io.tarantool.driver.mappers.converters.value.DefaultIntegerValueToFloatConverter;
import io.tarantool.driver.mappers.converters.value.DefaultIntegerValueToLongConverter;
import io.tarantool.driver.mappers.converters.value.DefaultStringValueToByteArrayConverter;
import io.tarantool.driver.mappers.converters.value.DefaultStringValueToCharacterConverter;
import io.tarantool.driver.mappers.converters.value.DefaultStringValueToStringConverter;
import org.msgpack.value.BinaryValue;
Expand Down Expand Up @@ -56,6 +57,7 @@ public final class DefaultMessagePackMapperFactory {
private DefaultMessagePackMapperFactory() {
defaultSimpleTypesMapper = new DefaultMessagePackMapper.Builder()
// converters for primitive values
.withValueConverter(ValueType.STRING, byte[].class, new DefaultStringValueToByteArrayConverter())
.withValueConverter(ValueType.STRING, Character.class, new DefaultStringValueToCharacterConverter())
.withValueConverter(ValueType.STRING, String.class, new DefaultStringValueToStringConverter())
.withValueConverter(ValueType.INTEGER, Float.class, new DefaultIntegerValueToFloatConverter())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.tarantool.driver.mappers.converters.value;

import io.tarantool.driver.mappers.converters.ValueConverter;
import org.msgpack.value.StringValue;

/**
* Default {@link StringValue} to {@code byte[]} converter
*
* @author Artyom Dubinin
*/
public class DefaultStringValueToByteArrayConverter implements ValueConverter<StringValue, byte[]> {

private static final long serialVersionUID = 20220601L;

@Override
public byte[] fromValue(StringValue value) {
return value.asByteArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,23 @@ public void test_crudOperations_shouldWorkWithVarbinary() throws Exception {
List<Byte> byteListFromTarantool = Arrays.asList(ArrayUtils.toObject(bytesFromTarantool));
Assertions.assertEquals(byteList, byteListFromTarantool);
}

@Test
public void test_crudOperations_shouldWorkWithBytesAsString() throws Exception {
//given
byte[] bytes = "hello".getBytes(StandardCharsets.UTF_8);
List<Byte> byteList = Arrays.asList(ArrayUtils.toObject(bytes));
client.space("space_with_string")
.insert(tupleFactory.create(1, bytes)).get();

//when
TarantoolTuple fields = client
.space("space_with_string")
.select(Conditions.equals("id", 1)).get().get(0);

//then
byte[] bytesFromTarantool = fields.getByteArray("string_field");
List<Byte> byteListFromTarantool = Arrays.asList(ArrayUtils.toObject(bytesFromTarantool));
Assertions.assertEquals(byteList, byteListFromTarantool);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.tarantool.driver.mappers;

import io.tarantool.driver.mappers.converters.value.DefaultStringValueToByteArrayConverter;
import org.junit.jupiter.api.Test;
import org.msgpack.value.ValueFactory;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* @author Artyom Dubinin
*/
public class DefaultStringValueToByteArrayConverterTest {

@Test
public void test_shouldConvertCorrectly() {
//given
DefaultStringValueToByteArrayConverter converter = new DefaultStringValueToByteArrayConverter();

//when
byte[] actual = converter.fromValue(ValueFactory.newString("Hello"));

//then
assertEquals("Hello", new String(actual));
}
}
15 changes: 15 additions & 0 deletions src/test/resources/cartridge/app/roles/api_storage.lua
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,21 @@ local function init_space()
space_with_varbinary:create_index('bucket_id', { parts = { 'bucket_id' }, unique = false, if_not_exists = true, })
end

local space_with_string = box.schema.space.create(
'space_with_string',
{
format = {
{ 'id', 'unsigned' },
{ 'string_field', 'string',},
{ 'bucket_id', 'unsigned' },
},
if_not_exists = true,
}
)

space_with_string:create_index('id', { parts = { 'id' }, if_not_exists = true, })
space_with_string:create_index('bucket_id', { parts = { 'bucket_id' }, unique = false, if_not_exists = true, })

-- cursor test spaces
local cursor_test_space = box.schema.space.create('cursor_test_space', { if_not_exists = true })
cursor_test_space:format({
Expand Down

0 comments on commit 42999fe

Please sign in to comment.