Skip to content

Commit 8fc2f09

Browse files
committed
computed values doc
1 parent 7267482 commit 8fc2f09

File tree

5 files changed

+98
-63
lines changed

5 files changed

+98
-63
lines changed

src/main/java/com/arangodb/springframework/annotation/ComputedValue.java

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
import org.springframework.core.annotation.AliasFor;
25+
26+
import java.lang.annotation.ElementType;
27+
import java.lang.annotation.Retention;
28+
import java.lang.annotation.RetentionPolicy;
29+
import java.lang.annotation.Target;
30+
31+
/**
32+
* Annotation to mark a field representing an ArangoDB computed value.
33+
* If the property is mutable, then the field is automatically updated in place with the value coming from the server
34+
* for the following operations:
35+
* <ul>
36+
* <li>{@link com.arangodb.springframework.core.ArangoOperations#repsert(Object)}</li>
37+
* <li>{@link com.arangodb.springframework.core.ArangoOperations#repsertAll(Iterable, Class)}</li>
38+
* <li>{@link com.arangodb.springframework.repository.ArangoRepository#save(Object)}</li>
39+
* <li>{@link com.arangodb.springframework.repository.ArangoRepository#saveAll(Iterable)}</li>
40+
* </ul>
41+
*
42+
* @see <a href="https://docs.arangodb.com/stable/concepts/data-structure/documents/computed-values">Reference Doc</a>
43+
*/
44+
@Retention(RetentionPolicy.RUNTIME)
45+
@Target({ElementType.FIELD})
46+
public @interface ComputedValueField {
47+
48+
@AliasFor("expression")
49+
String value() default "";
50+
51+
/**
52+
* @return An AQL RETURN operation with an expression that computes the desired value. If empty, the computed value
53+
* data definition will not be set on collection creation.
54+
* @see ComputedValue#expression(String)
55+
*/
56+
@AliasFor("value")
57+
String expression() default "";
58+
59+
/**
60+
* @return Whether the computed value shall take precedence over a user-provided or existing attribute.
61+
* The default is false.
62+
* @see ComputedValue#overwrite(Boolean)
63+
*/
64+
boolean overwrite() default false;
65+
66+
/**
67+
* @return An array of operations to define on which write operations the value shall be computed.
68+
* The default is ["insert", "update", "replace"].
69+
* @see ComputedValue#computeOn(ComputedValue.ComputeOn...)
70+
*/
71+
ComputedValue.ComputeOn[] computeOn() default {
72+
ComputedValue.ComputeOn.insert,
73+
ComputedValue.ComputeOn.update,
74+
ComputedValue.ComputeOn.replace
75+
};
76+
77+
/**
78+
* @return Whether the target attribute shall be set if the expression evaluates to null. You can set the option to
79+
* false to not set (or unset) the target attribute if the expression returns null. The default is true.
80+
* @see ComputedValue#keepNull(Boolean)
81+
*/
82+
boolean keepNull() default true;
83+
84+
/**
85+
* @return Whether to let the write operation fail if the expression produces a warning.
86+
* The default is false.
87+
* @see ComputedValue#failOnWarning(Boolean)
88+
*/
89+
boolean failOnWarning() default false;
90+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public interface ArangoPersistentProperty extends PersistentProperty<ArangoPersi
4848

4949
Optional<To> getTo();
5050

51-
Optional<ComputedValue> getComputedValue();
51+
Optional<ComputedValueField> getComputedValue();
5252

5353
Optional<PersistentIndexed> getPersistentIndexed();
5454

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ private Optional<String> getAnnotatedFieldName() {
114114
}
115115

116116
@Override
117-
public Optional<ComputedValue> getComputedValue() {
118-
return Optional.ofNullable(findAnnotation(ComputedValue.class));
117+
public Optional<ComputedValueField> getComputedValue() {
118+
return Optional.ofNullable(findAnnotation(ComputedValueField.class));
119119
}
120120

121121
@Override

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

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -539,15 +539,9 @@ static class ComputedValueMutable {
539539
private String id;
540540

541541
@Field("compVal")
542-
@ComputedValue(
542+
@ComputedValueField(
543543
expression = "RETURN \"foo\"",
544-
computeOn = {
545-
com.arangodb.model.ComputedValue.ComputeOn.update,
546-
com.arangodb.model.ComputedValue.ComputeOn.replace,
547-
com.arangodb.model.ComputedValue.ComputeOn.insert
548-
},
549-
overwrite = true
550-
)
544+
overwrite = true)
551545
private String value;
552546
}
553547

@@ -574,26 +568,18 @@ record ComputedValueImmutable(
574568
String id,
575569

576570
@Field("compVal")
577-
@ComputedValue(
578-
expression = "RETURN \"foo\"",
579-
computeOn = {
580-
com.arangodb.model.ComputedValue.ComputeOn.update,
581-
com.arangodb.model.ComputedValue.ComputeOn.replace,
582-
com.arangodb.model.ComputedValue.ComputeOn.insert
583-
},
584-
overwrite = true
585-
)
571+
@ComputedValueField("RETURN \"foo\"")
586572
String value
587573
) {
588574
}
589575

590576
@Test
591577
public void computedValueImmutableProp() {
592-
ComputedValueImmutable entity = new ComputedValueImmutable(null, "bar");
578+
ComputedValueImmutable entity = new ComputedValueImmutable(null, null);
593579
ComputedValueImmutable saved = template.repsert(entity);
594580
assertThat(entity.id, is(nullValue()));
595581
assertThat(saved.id, is(notNullValue()));
596-
assertThat(entity.value, is("bar"));
582+
assertThat(entity.value, is(nullValue()));
597583
assertThat(saved.value, is("foo"));
598584
}
599585

0 commit comments

Comments
 (0)