Skip to content

Add API for property specific converters [DATACMNS-1036] #1484

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

Closed
spring-projects-issues opened this issue Apr 7, 2017 · 2 comments
Closed
Assignees
Labels
in: mapping Mapping and conversion infrastructure theme: 3.0 type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link

Oliver Drotbohm opened DATACMNS-1036 and commented

The current conversion subsystem can only deal with type based converters. We should add API to allow users to register converters for a specific PersistentProperty


Issue Links:

  • DATACASS-668 Provide a way to custom convert some properties of an entity
    ("is depended on by")

  • DATAJDBC-379 Disable auto-apply custom converts
    ("is depended on by")

  • DATAJDBC-450 Storing non root entities in serialized form

  • DATAMONGO-889 Missing field and property annotations do not allow conditional conversion

5 votes, 5 watchers

@spring-projects-issues
Copy link
Author

Oliver Drotbohm commented

  • ConversionContext
    • EntityConverter
    • MappingContext
    • Owner
  • PersistentPropertyConverter<T, S, P extends PersistentProperty<P>>
    • S write(T value, P property, ConversionContext context);
    • T read(S value, P property, ConversionContext context);
  • Open questions:
    • How to register those PPConverters? As a user? As another framework (Spring Security)?
interface PPC<T, S, P extends PersistentProperty<P>> { …}

interface PPCPlugin extends Plugin<P> { … }

class Conversions implements PPCPlugin {

	PropertyPath path;

	<K, L extends S> PPCSupport assignTo(PPC converter, Class<K> type, Function<K, L> consumer) {
		Recorder recorder = MIR.forProxyOfType(type).record(consumer);
		this.path = PropertyPath.of(type, recorder.getPropertyPath());
	}
	

	supports(PP property) {

		if (path.getSegment() != null) {
			path.getOwner == property.getOwner
			path.getSegment == property.getName;
		} else {
			path.getOwner = property.getType
		}
	}
}


PluginRegistry<PPC<…>, PersistentProperty<?>> registry;

PPC converter = registry.getPluginFor(property, () -> new DefaultPPC());


class MyEncodingConverter extends PPCSupport<String, String, …> {

	read(…);

	write(…);
}

class Config {

  @Bean
  Conversions foo() {

    return ConversionsBuilder.forConverter(new MyEncodingConverter())
      .assignByProperty(Employee.class, Employee::getPassword, e -> e.getEmail().getValue())
      .assingByType(Password.class));
    }
  }
}

@nbali
Copy link

nbali commented Apr 19, 2021

If I might have a suggestion, the converter would be superior even to the JPA version if the context (the POJO being converted, the field being converted, etc) would be also available and not just the values.

JPA (only values):
https://docs.oracle.com/javaee/7/api/javax/persistence/AttributeConverter.html
vs
Objectify (with context):
https://github.com/objectify/objectify/blob/master/src/main/java/com/googlecode/objectify/impl/translate/TranslatorFactory.java
https://github.com/objectify/objectify/blob/master/src/main/java/com/googlecode/objectify/impl/translate/Translator.java

For example the second can be used to check for annotations on the field (or values of other fields), and the conversion can be dynamic based on that.

@RequiredArgsConstructor
class EncryptingTranslatorFactory implements TranslatorFactory<String, String> {

    @NonNull
    private final EncryptionService encryptionService;

    @Override
    public Translator<String, String> create(TypeKey<String> tk, CreateContext ctx, Path path) {
        if (tk.isAnnotationPresent(Sensitive.class)) {
            if (tk.getTypeAsClass() != String.class) {
                throw new IllegalStateException("@Sensitive is only supported for String at the moment!");
            }
            return <...>;
        } else {
            return null;
        }
    }

}

@christophstrobl christophstrobl linked a pull request Mar 1, 2022 that will close this issue
@mp911de mp911de added this to the 2.7 M4 (2021.2.0) milestone Mar 18, 2022
@mp911de mp911de added type: enhancement A general enhancement and removed type: task A general task labels Mar 18, 2022
mp911de pushed a commit that referenced this issue Mar 18, 2022
mp911de pushed a commit that referenced this issue Mar 18, 2022
Introduce a builder API to register PropertyValueConverters using simple (Bi)Functions. Additional methods on ValueConversionContext to enable advanced use cases in converter implementation. Tweak generics of VCC to be able to expose store-specific PersistentProperty implementations via the context.

See #1484
Original pull request: #2566.
mp911de pushed a commit that referenced this issue Mar 18, 2022
mp911de added a commit that referenced this issue Mar 18, 2022
Increase visibility of converter builders.

Refine generics naming towards DV/SV instead of A/B to easier identify store-native values and domains-specific values in the API. Refactor PropertyValueConversions.hasValueConverter into a non-default method.

Tweak documentation.

See #1484
Original pull request: #2566.
mp911de pushed a commit that referenced this issue Mar 18, 2022
Introduce a builder API to register PropertyValueConverters using simple (Bi)Functions. Additional methods on ValueConversionContext to enable advanced use cases in converter implementation. Tweak generics of VCC to be able to expose store-specific PersistentProperty implementations via the context.

See #1484
Original pull request: #2566.
mp911de pushed a commit that referenced this issue Mar 18, 2022
mp911de added a commit that referenced this issue Mar 18, 2022
Increase visibility of converter builders.

Refine generics naming towards DV/SV instead of A/B to easier identify store-native values and domains-specific values in the API. Refactor PropertyValueConversions.hasValueConverter into a non-default method.

Tweak documentation.

See #1484
Original pull request: #2566.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: mapping Mapping and conversion infrastructure theme: 3.0 type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants