cartridge-java icon indicating copy to clipboard operation
cartridge-java copied to clipboard

No converter for enum

Open ArtDu opened this issue 3 years ago • 2 comments

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())
        );

ArtDu avatar Oct 23 '21 11:10 ArtDu

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.

akudiyar avatar Oct 27 '21 11:10 akudiyar

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();
        }
    }

ArtDu avatar Oct 27 '21 11:10 ArtDu