cartridge-java
cartridge-java copied to clipboard
No converter for enum
An example was made on cartridge-springdata, but this is relevant for cartridge-java
@Tuple("test_space")
public enum TestEnum {
a,
b;
}
@Tuple("test_space")
public interface EnumRepository extends TarantoolRepository<TestEnum, Integer> {
@Query(function = "f")
void f(TestEnum e);
}
function f(entity)
require("log").info(entity)
end
Error:
io.tarantool.driver.mappers.MessagePackObjectMapperException: ObjectConverter for type class org.springframework.data.tarantool.entities.TestEnum is not found
Probably the simplest solution is to create a converter that will convert enum to StringValue using enum.name()
:
MessagePackMapper defaultMapper =
DefaultMessagePackMapperFactory.getInstance().defaultComplexTypesMapper();
defaultMapper.registerObjectConverter(
Enum.class, StringValue.class, object ->
ValueFactory.newString(object.name())
);
Another possible solution that may be used as W/A or implemented permanently in SpringData - a custom Enum-to-String conversion. AFAIK, such conversion already exists there but probably is not working now.
AFAIK, such conversion already exists there but probably is not working now.
For reading everything is ok, but for writing an error appears. If we write a custom converter, then everything is ok
@WritingConverter
public enum EnumToStringConverter implements Converter<Enum, String> {
INSTANCE;
@Override
public String convert(Enum source) {
return source.name();
}
}