Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No converter for enum #143

Open
ArtDu opened this issue Oct 23, 2021 · 2 comments
Open

No converter for enum #143

ArtDu opened this issue Oct 23, 2021 · 2 comments

Comments

@ArtDu
Copy link
Contributor

ArtDu commented Oct 23, 2021

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())
        );
@akudiyar
Copy link
Collaborator

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.

@ArtDu
Copy link
Contributor Author

ArtDu commented Oct 27, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants