DATAKV-268 - Add SpEL support for @KeySpace.

We now support SpEL expressions in @KeySpace that gets evaluated on a per-operation basis.

@KeySpace("#{myProperty}")
class Person {}

Original Pull Request: #46
This commit is contained in:
Mark Paluch
2019-06-19 10:34:05 +02:00
committed by Christoph Strobl
parent 7d7021e951
commit e9128743b6
3 changed files with 119 additions and 11 deletions

View File

@@ -26,7 +26,8 @@ import org.springframework.data.annotation.Persistent;
/**
* Marker interface for methods with {@link Persistent} annotations indicating the presence of a dedicated keyspace the
* entity should reside in. If present the value will be picked up for resolving the keyspace.
* entity should reside in. If present the value will be picked up for resolving the keyspace. The {@link #value()}
* attribute supports SpEL expressions to dynamically resolve the keyspace based on a per-operation basis.
*
* <pre>
* <code>

View File

@@ -17,6 +17,10 @@ package org.springframework.data.keyvalue.core.mapping;
import org.springframework.data.mapping.model.BasicPersistentEntity;
import org.springframework.data.util.TypeInformation;
import org.springframework.expression.Expression;
import org.springframework.expression.ParserContext;
import org.springframework.expression.common.LiteralExpression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
@@ -31,32 +35,52 @@ import org.springframework.util.StringUtils;
public class BasicKeyValuePersistentEntity<T, P extends KeyValuePersistentProperty<P>>
extends BasicPersistentEntity<T, P> implements KeyValuePersistentEntity<T, P> {
private static final SpelExpressionParser PARSER = new SpelExpressionParser();
private static final KeySpaceResolver DEFAULT_FALLBACK_RESOLVER = ClassNameKeySpaceResolver.INSTANCE;
private final @Nullable Expression keyspaceExpression;
private final @Nullable String keyspace;
/**
* @param information must not be {@literal null}.
* @param keySpaceResolver can be {@literal null}.
* @param fallbackKeySpaceResolver can be {@literal null}.
*/
public BasicKeyValuePersistentEntity(TypeInformation<T> information,
@Nullable KeySpaceResolver fallbackKeySpaceResolver) {
super(information);
this.keyspace = detectKeySpace(information.getType(), fallbackKeySpaceResolver);
}
@Nullable
private static String detectKeySpace(Class<?> type, @Nullable KeySpaceResolver fallback) {
Class<T> type = information.getType();
String keySpace = AnnotationBasedKeySpaceResolver.INSTANCE.resolveKeySpace(type);
if (StringUtils.hasText(keySpace)) {
return keySpace;
this.keyspace = keySpace;
this.keyspaceExpression = detectExpression(keySpace);
} else {
this.keyspace = resolveKeyspace(fallbackKeySpaceResolver, type);
this.keyspaceExpression = null;
}
}
return (fallback == null ? DEFAULT_FALLBACK_RESOLVER : fallback).resolveKeySpace(type);
/**
* Returns a SpEL {@link Expression} if the given {@link String} is actually an expression that does not evaluate to a
* {@link LiteralExpression} (indicating that no subsequent evaluation is necessary).
*
* @param potentialExpression can be {@literal null}
* @return
*/
@Nullable
private static Expression detectExpression(String potentialExpression) {
Expression expression = PARSER.parseExpression(potentialExpression, ParserContext.TEMPLATE_EXPRESSION);
return expression instanceof LiteralExpression ? null : expression;
}
@Nullable
private static String resolveKeyspace(@Nullable KeySpaceResolver fallbackKeySpaceResolver, Class<?> type) {
return (fallbackKeySpaceResolver == null ? DEFAULT_FALLBACK_RESOLVER : fallbackKeySpaceResolver)
.resolveKeySpace(type);
}
/*
@@ -65,6 +89,8 @@ public class BasicKeyValuePersistentEntity<T, P extends KeyValuePersistentProper
*/
@Override
public String getKeySpace() {
return this.keyspace;
return keyspaceExpression == null //
? keyspace //
: keyspaceExpression.getValue(getEvaluationContext(null), String.class);
}
}

View File

@@ -0,0 +1,81 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.keyvalue.core.mapping;
import static org.assertj.core.api.Assertions.*;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.Test;
import org.springframework.data.keyvalue.annotation.KeySpace;
import org.springframework.data.keyvalue.core.mapping.context.KeyValueMappingContext;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.spel.ExtensionAwareEvaluationContextProvider;
import org.springframework.data.spel.spi.EvaluationContextExtension;
/**
* Unit tests for {@link BasicKeyValuePersistentEntity}.
*
* @author Mark Paluch
*/
public class BasicKeyValuePersistentEntityUnitTests {
MappingContext<? extends KeyValuePersistentEntity<?, ?>, ? extends KeyValuePersistentProperty<?>> mappingContext = new KeyValueMappingContext<>();
@Test // DATAKV-268
public void shouldDeriveKeyspaceFromClassName() {
KeyValuePersistentEntity<?, ?> persistentEntity = mappingContext.getPersistentEntity(KeyspaceEntity.class);
assertThat(persistentEntity.getKeySpace()).isEqualTo(KeyspaceEntity.class.getName());
}
@Test // DATAKV-268
public void shouldEvaluateKeyspaceExpression() {
KeyValuePersistentEntity<?, ?> persistentEntity = mappingContext.getPersistentEntity(ExpressionEntity.class);
persistentEntity.setEvaluationContextProvider(
new ExtensionAwareEvaluationContextProvider(Collections.singletonList(new SampleExtension())));
assertThat(persistentEntity.getKeySpace()).isEqualTo("some");
}
@KeySpace("#{myProperty}")
static class ExpressionEntity {}
@KeySpace
static class KeyspaceEntity {}
static class SampleExtension implements EvaluationContextExtension {
@Override
public String getExtensionId() {
return "sampleExtension";
}
@Override
public Map<String, Object> getProperties() {
Map<String, Object> properties = new LinkedHashMap<>();
properties.put("myProperty", "some");
return properties;
}
}
}