8000 computed values class annotations · arangodb/spring-data@6e2664a · GitHub
[go: up one dir, main page]

Skip to content

Commit 6e2664a

Browse files
committed
computed values class annotations
1 parent 8fc2f09 commit 6e2664a

File tree

4 files changed

+146
-6
lines changed

4 files changed

+146
-6
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2017 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb.springframework.annotation;
22+
23+
import com.arangodb.model.ComputedValue;
24+
25+
import java.lang.annotation.*;
26+
27+
/**
28+
* Annotation to define computed values of a collection.
29+
*
30+
* @see <a href="https://docs.arangodb.com/stable/concepts/data-structure/documents/computed-values">Reference Doc</a>
31+
*/
32+
@Repeatable(ComputedValues.class)
33+
@Retention(RetentionPolicy.RUNTIME)
34+
@Target({ElementType.TYPE})
35+
public @interface ComputedValueEntry {
36+
37+
/**
38+
* @return The name of the target attribute. Can only be a top-level attribute, but you may return a nested object.
39+
* Cannot be _key, _id, _rev, _from, _to, or a shard key attribute.
40+
* @see ComputedValue#name(String)
41+
*/
42+
String name() default "";
43+
44+
/**
45+
* @return An AQL RETURN operation with an expression that computes the desired value. If empty, the computed value
46+
* data definition will not be set on collection creation.
47+
* @see ComputedValue#expression(String)
48+
*/
49+
String expression() default "";
50+
51+
/**
52+
* @return Whether the computed value shall take precedence over a user-provided or existing attribute.
53+
* The default is false.
54+
* @see ComputedValue#overwrite(Boolean)
55+
*/
56+
boolean overwrite() default false;
57+
58+
/**
59+
* @return An array of operations to define on which write operations the value shall be computed.
60+
* The default is ["insert", "update", "replace"].
61+
* @see ComputedValue#computeOn(ComputedValue.ComputeOn...)
62+
*/
63+
ComputedValue.ComputeOn[] computeOn() default {
64+
ComputedValue.ComputeOn.insert,
65+
ComputedValue.ComputeOn.update,
66+
ComputedValue.ComputeOn.replace
67+
};
68+
69+
/**
70+
* @return Whether the target attribute shall be set if the expression evaluates to null. You can set the option to
71+
* false to not set (or unset) the target attribute if the expression returns null. The default is true.
72+
* @see ComputedValue#keepNull(Boolean)
73+
*/
74+
boolean keepNull() default true;
75+
76+
/**
77+
* @return Whether to let the write operation fail if the expression produces a warning.
78+
* The default is false.
79+
* @see ComputedValue#failOnWarning(Boolean)
80+
*/
81+
boolean failOnWarning() default false;
82+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2017 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb.springframework.annotation;
22+
23+
import java.lang.annotation.ElementType;
24+
import java.lang.annotation.Retention;
25+
import java.lang.annotation.RetentionPolicy;
26+
import java.lang.annotation.Target;
27+
28+
@Retention(RetentionPolicy.RUNTIME)
29+
@Target({ ElementType.TYPE })
30+
public @interface ComputedValues {
31+
32+
ComputedValueEntry[] value();
33+
34+
}

src/main/java/com/arangodb/springframework/core/mapping/DefaultArangoPersistentEntity.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.arangodb.entity.CollectionType;
2424
import com.arangodb.entity.ReplicationFactor;
2525
import com.arangodb.model.CollectionCreateOptions;
26+
import com.arangodb.model.ComputedValue;
2627
import com.arangodb.springframework.annotation.*;
2728
import org.springframework.beans.BeansException;
2829
import org.springframework.context.ApplicationContext;
@@ -200,10 +201,33 @@ public CollectionCreateOptions getCollectionOptions() {
200201
options = new CollectionCreateOptions().type(CollectionType.DOCUMENT);
201202
}
202203

204+
getComputedValues().forEach(options::computedValues);
205+
return options;
206+
}
207+
208+
private List<ComputedValue> getComputedValues() {
209+
List<ComputedValue> res = new ArrayList<>();
210+
211+
// computed values from entity annotations
212+
Collection<ComputedValueEntry> computedValueEntries = getIndexes(ComputedValueEntry.class);
213+
Optional.ofNullable(findAnnotation(ComputedValues.class))
214+
.ifPresent(i -> computedValueEntries.addAll(Arrays.asList(i.value())));
215+
computedValueEntries.stream()
216+
.map(it -> new ComputedValue()
217+
.name(it.name())
218+
.expression(it.expression())
219+
.overwrite(it.overwrite())
220+
.computeOn(it.computeOn())
221+
.keepNull(it.keepNull())
222+
.failOnWarning(it.failOnWarning())
223+
)
224+
.forEach(res::add);
225+
226+
// computed values from fields annotations
203227
computedValueProperties.values().stream()
204228
.flatMap(it -> it.getComputedValue()
205229
.filter(cv -> !cv.expression().isEmpty())
206-
.map(cv -> new com.arangodb.model.ComputedValue()
230+
.map(cv -> new ComputedValue()
207231
.name(it.getFieldName())
208232
.expression(cv.expression())
209233
.overwrite(cv.overwrite())
@@ -213,9 +237,9 @@ public CollectionCreateOptions getCollectionOptions() {
213237
)
214238
.stream()
215239
)
216-
.forEach(options::computedValues);
240+
.forEach(res::add);
217241

218-
return options;
242+
return res;
219243
}
220244

221245
@Override

src/test/java/com/arangodb/springframework/core/mapping/GeneralMappingTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,12 +563,12 @@ public void computedValueMutableProp() {
563563
}
564564

565565
@Document
566+
@ComputedValueEntry(
567+
name = "value",
568+
expression = "RETURN \"foo\"")
566569
record ComputedValueImmutable(
567570
@Id
568571
String id,
569-
570-
@Field("compVal")
571-
@ComputedValueField("RETURN \"foo\"")
572572
String value
573573
) {
574574
}

0 commit comments

Comments
 (0)
0