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 1, 2022
1 parent 92ba4f7 commit 8fc9904
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
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 @@ -66,4 +66,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);
}
}
14 changes: 14 additions & 0 deletions src/test/resources/cartridge/app/roles/api_storage.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ local function init_space()
space_with_varbinary:create_index('id', { parts = { 'id' }, if_not_exists = true, })
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, })

local instances_info = box.schema.space.create(
'instances_info',
Expand Down

0 comments on commit 8fc9904

Please sign in to comment.