Skip to content

No way to create a modifiable copy of DefaultMessagePackMapper. Now the copy affects original object #166

Closed
@ArtDu

Description

@ArtDu

The case is: we want to create a copy of the default mapper and add some custom converters there. But we don't want to affect all existing converter stack. For example - I want to create a method that converts all Integers to Strings for some specific space..

We can't reuse a custom MessagePackMapper right now.

@Override
public MessagePackMapper copy() {
return new DefaultMessagePackMapper(this);
}

/**
* Copying constructor
* @param mapper another mapper instance
*/
public DefaultMessagePackMapper(DefaultMessagePackMapper mapper) {
this();
this.valueConverters.putAll(mapper.valueConverters);
this.objectConverters.putAll(mapper.objectConverters);
this.valueConvertersByTarget.putAll(mapper.valueConvertersByTarget);
this.objectConvertersByTarget.putAll(mapper.objectConvertersByTarget);
}

private final Map<String, List<ValueConverter<? extends Value, ?>>> valueConverters;
private final Map<String, List<ObjectConverter<?, ? extends Value>>> objectConverters;
private final Map<String, ValueConverter<? extends Value, ?>> valueConvertersByTarget;
private final Map<String, ObjectConverter<?, ? extends Value>> objectConvertersByTarget;
private final ObjectConverter<Object, NilValue> nilConverter = new DefaultNilConverter();

tarantoolClient.getConfig().getMessagePackMapper().copy();
Deepcopy is not used here, so copying does not make sense. It only copies the surface layer:
if there was an entry in tarantoolClient.getConfig().getMessagePackMapper() in Map<String, List<ValueConverter<? extends Value, ?>>> valueConverters; with the key org.msgpack.value.ArrayValue, then the copied list List<ValueConverter<? extends Value, ?> will be the same for all copied configs, and when registering a new converter for the copied config, it will appear for the original one as well.

For details see these comments' threads:
tarantool/cartridge-springdata#80 (comment)
tarantool/cartridge-springdata#80 (comment)
tarantool/cartridge-springdata#80 (comment)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions