Skip to content

Commit e912874

Browse files
mp911dechristophstrobl
authored andcommitted
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
1 parent 7d7021e commit e912874

File tree

3 files changed

+119
-11
lines changed

3 files changed

+119
-11
lines changed

src/main/java/org/springframework/data/keyvalue/annotation/KeySpace.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626

2727
/**
2828
* Marker interface for methods with {@link Persistent} annotations indicating the presence of a dedicated keyspace the
29-
* entity should reside in. If present the value will be picked up for resolving the keyspace.
29+
* entity should reside in. If present the value will be picked up for resolving the keyspace. The {@link #value()}
30+
* attribute supports SpEL expressions to dynamically resolve the keyspace based on a per-operation basis.
3031
*
3132
* <pre>
3233
* <code>

src/main/java/org/springframework/data/keyvalue/core/mapping/BasicKeyValuePersistentEntity.java

+36-10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717

1818
import org.springframework.data.mapping.model.BasicPersistentEntity;
1919
import org.springframework.data.util.TypeInformation;
20+
import org.springframework.expression.Expression;
21+
import org.springframework.expression.ParserContext;
22+
import org.springframework.expression.common.LiteralExpression;
23+
import org.springframework.expression.spel.standard.SpelExpressionParser;
2024
import org.springframework.lang.Nullable;
2125
import org.springframework.util.StringUtils;
2226

@@ -31,32 +35,52 @@
3135
public class BasicKeyValuePersistentEntity<T, P extends KeyValuePersistentProperty<P>>
3236
extends BasicPersistentEntity<T, P> implements KeyValuePersistentEntity<T, P> {
3337

38+
private static final SpelExpressionParser PARSER = new SpelExpressionParser();
39+
3440
private static final KeySpaceResolver DEFAULT_FALLBACK_RESOLVER = ClassNameKeySpaceResolver.INSTANCE;
3541

42+
private final @Nullable Expression keyspaceExpression;
3643
private final @Nullable String keyspace;
3744

3845
/**
3946
* @param information must not be {@literal null}.
40-
* @param keySpaceResolver can be {@literal null}.
47+
* @param fallbackKeySpaceResolver can be {@literal null}.
4148
*/
4249
public BasicKeyValuePersistentEntity(TypeInformation<T> information,
4350
@Nullable KeySpaceResolver fallbackKeySpaceResolver) {
4451

4552
super(information);
4653

47-
this.keyspace = detectKeySpace(information.getType(), fallbackKeySpaceResolver);
48-
}
49-
50-
@Nullable
51-
private static String detectKeySpace(Class<?> type, @Nullable KeySpaceResolver fallback) {
52-
54+
Class<T> type = information.getType();
5355
String keySpace = AnnotationBasedKeySpaceResolver.INSTANCE.resolveKeySpace(type);
5456

5557
if (StringUtils.hasText(keySpace)) {
56-
return keySpace;
58+
this.keyspace = keySpace;
59+
this.keyspaceExpression = detectExpression(keySpace);
60+
} else {
61+
this.keyspace = resolveKeyspace(fallbackKeySpaceResolver, type);
62+
this.keyspaceExpression = null;
5763
}
64+
}
5865

59-
return (fallback == null ? DEFAULT_FALLBACK_RESOLVER : fallback).resolveKeySpace(type);
66+
/**
67+
* Returns a SpEL {@link Expression} if the given {@link String} is actually an expression that does not evaluate to a
68+
* {@link LiteralExpression} (indicating that no subsequent evaluation is necessary).
69+
*
70+
* @param potentialExpression can be {@literal null}
71+
* @return
72+
*/
73+
@Nullable
74+
private static Expression detectExpression(String potentialExpression) {
75+
76+
Expression expression = PARSER.parseExpression(potentialExpression, ParserContext.TEMPLATE_EXPRESSION);
77+
return expression instanceof LiteralExpression ? null : expression;
78+
}
79+
80+
@Nullable
81+
private static String resolveKeyspace(@Nullable KeySpaceResolver fallbackKeySpaceResolver, Class<?> type) {
82+
return (fallbackKeySpaceResolver == null ? DEFAULT_FALLBACK_RESOLVER : fallbackKeySpaceResolver)
83+
.resolveKeySpace(type);
6084
}
6185

6286
/*
@@ -65,6 +89,8 @@ private static String detectKeySpace(Class<?> type, @Nullable KeySpaceResolver f
6589
*/
6690
@Override
6791
public String getKeySpace() {
68-
return this.keyspace;
92+
return keyspaceExpression == null //
93+
? keyspace //
94+
: keyspaceExpression.getValue(getEvaluationContext(null), String.class);
6995
}
7096
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.keyvalue.core.mapping;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import java.util.Collections;
21+
import java.util.LinkedHashMap;
22+
import java.util.Map;
23+
24+
import org.junit.Test;
25+
26+
import org.springframework.data.keyvalue.annotation.KeySpace;
27+
import org.springframework.data.keyvalue.core.mapping.context.KeyValueMappingContext;
28+
import org.springframework.data.mapping.context.MappingContext;
29+
import org.springframework.data.spel.ExtensionAwareEvaluationContextProvider;
30+
import org.springframework.data.spel.spi.EvaluationContextExtension;
31+
32+
/**
33+
* Unit tests for {@link BasicKeyValuePersistentEntity}.
34+
*
35+
* @author Mark Paluch
36+
*/
37+
public class BasicKeyValuePersistentEntityUnitTests {
38+
39+
MappingContext<? extends KeyValuePersistentEntity<?, ?>, ? extends KeyValuePersistentProperty<?>> mappingContext = new KeyValueMappingContext<>();
40+
41+
@Test // DATAKV-268
42+
public void shouldDeriveKeyspaceFromClassName() {
43+
44+
KeyValuePersistentEntity<?, ?> persistentEntity = mappingContext.getPersistentEntity(KeyspaceEntity.class);
45+
46+
assertThat(persistentEntity.getKeySpace()).isEqualTo(KeyspaceEntity.class.getName());
47+
}
48+
49+
@Test // DATAKV-268
50+
public void shouldEvaluateKeyspaceExpression() {
51+
52+
KeyValuePersistentEntity<?, ?> persistentEntity = mappingContext.getPersistentEntity(ExpressionEntity.class);
53+
54+
persistentEntity.setEvaluationContextProvider(
55+
new ExtensionAwareEvaluationContextProvider(Collections.singletonList(new SampleExtension())));
56+
57+
assertThat(persistentEntity.getKeySpace()).isEqualTo("some");
58+
}
59+
60+
@KeySpace("#{myProperty}")
61+
static class ExpressionEntity {}
62+
63+
@KeySpace
64+
static class KeyspaceEntity {}
65+
66+
static class SampleExtension implements EvaluationContextExtension {
67+
68+
@Override
69+
public String getExtensionId() {
70+
return "sampleExtension";
71+
}
72+
73+
@Override
74+
public Map<String, Object> getProperties() {
75+
76+
Map<String, Object> properties = new LinkedHashMap<>();
77+
properties.put("myProperty", "some");
78+
return properties;
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)