16
16
package org .springframework .data .mongodb .core .convert ;
17
17
18
18
import org .bson .conversions .Bson ;
19
-
20
19
import org .springframework .data .convert .ValueConversionContext ;
21
20
import org .springframework .data .mapping .model .PropertyValueProvider ;
22
21
import org .springframework .data .mapping .model .SpELContext ;
23
22
import org .springframework .data .mongodb .core .mapping .MongoPersistentProperty ;
24
23
import org .springframework .data .util .TypeInformation ;
24
+ import org .springframework .lang .CheckReturnValue ;
25
25
import org .springframework .lang .Nullable ;
26
26
27
27
/**
@@ -38,7 +38,7 @@ public class MongoConversionContext implements ValueConversionContext<MongoPersi
38
38
39
39
@ Nullable private final MongoPersistentProperty persistentProperty ;
40
40
@ Nullable private final SpELContext spELContext ;
41
- @ Nullable private final String fieldNameAndQueryOperator ;
41
+ @ Nullable private final OperatorContext operatorContext ;
42
42
43
43
public MongoConversionContext (PropertyValueProvider <MongoPersistentProperty > accessor ,
44
44
@ Nullable MongoPersistentProperty persistentProperty , MongoConverter mongoConverter ) {
@@ -53,19 +53,19 @@ public MongoConversionContext(PropertyValueProvider<MongoPersistentProperty> acc
53
53
54
54
public MongoConversionContext (PropertyValueProvider <MongoPersistentProperty > accessor ,
55
55
@ Nullable MongoPersistentProperty persistentProperty , MongoConverter mongoConverter ,
56
- @ Nullable String fieldNameAndQueryOperator ) {
57
- this (accessor , persistentProperty , mongoConverter , null , fieldNameAndQueryOperator );
56
+ @ Nullable OperatorContext operatorContext ) {
57
+ this (accessor , persistentProperty , mongoConverter , null , operatorContext );
58
58
}
59
59
60
60
public MongoConversionContext (PropertyValueProvider <MongoPersistentProperty > accessor ,
61
61
@ Nullable MongoPersistentProperty persistentProperty , MongoConverter mongoConverter ,
62
- @ Nullable SpELContext spELContext , @ Nullable String fieldNameAndQueryOperator ) {
62
+ @ Nullable SpELContext spELContext , @ Nullable OperatorContext operatorContext ) {
63
63
64
64
this .accessor = accessor ;
65
65
this .persistentProperty = persistentProperty ;
66
66
this .mongoConverter = mongoConverter ;
67
67
this .spELContext = spELContext ;
68
- this .fieldNameAndQueryOperator = fieldNameAndQueryOperator ;
68
+ this .operatorContext = operatorContext ;
69
69
}
70
70
71
71
@ Override
@@ -78,6 +78,17 @@ public MongoPersistentProperty getProperty() {
78
78
return persistentProperty ;
79
79
}
80
80
81
+ /**
82
+ *
83
+ * @param operatorContext
84
+ * @return new instance of {@link MongoConversionContext}.
85
+ * @since 4.5
86
+ */
87
+ @ CheckReturnValue
88
+ public MongoConversionContext forOperator (@ Nullable OperatorContext operatorContext ) {
89
+ return new MongoConversionContext (accessor , persistentProperty , mongoConverter , spELContext , operatorContext );
90
+ }
91
+
81
92
@ Nullable
82
93
public Object getValue (String propertyPath ) {
83
94
return accessor .getPropertyValue (getProperty ().getOwner ().getRequiredPersistentProperty (propertyPath ));
@@ -101,7 +112,78 @@ public SpELContext getSpELContext() {
101
112
}
102
113
103
114
@ Nullable
104
- public String getFieldNameAndQueryOperator () {
105
- return fieldNameAndQueryOperator ;
115
+ public OperatorContext getOperatorContext () {
116
+ return operatorContext ;
117
+ }
118
+
119
+ /**
120
+ * The {@link OperatorContext} provides access to the actual conversion intent like a write operation or a query
121
+ * operator such as {@literal $gte}.
122
+ *
123
+ * @since 4.5
124
+ */
125
+ public interface OperatorContext {
126
+
127
+ /**
128
+ * The operator the conversion is used in.
129
+ * @return {@literal write} for simple write operations during save, or a query operator.
130
+ */
131
+ String getOperator ();
132
+
133
+ /**
134
+ * The context path the operator is used in.
135
+ * @return never {@literal null}.
136
+ */
137
+ String getPath ();
138
+
139
+ boolean isWriteOperation ();
140
+ }
141
+
142
+ public static class WriteOperatorContext implements OperatorContext {
143
+
144
+ private final String path ;
145
+
146
+ public WriteOperatorContext (String path ) {
147
+ this .path = path ;
148
+ }
149
+
150
+ @ Override
151
+ public String getOperator () {
152
+ return "write" ;
153
+ }
154
+
155
+ @ Override
156
+ public String getPath () {
157
+ return path ;
158
+ }
159
+
160
+ @ Override
161
+ public boolean isWriteOperation () {
162
+ return true ;
163
+ }
164
+ }
165
+
166
+ public static class QueryOperatorContext implements OperatorContext {
167
+
168
+ private final String operator ;
169
+ private final String path ;
170
+
171
+ public QueryOperatorContext (@ Nullable String operator , String path ) {
172
+ this .operator = operator != null ? operator : "$eq" ;
173
+ this .path = path ;
174
+ }
175
+
176
+ public String getOperator () {
177
+ return operator ;
178
+ }
179
+
180
+ public String getPath () {
181
+ return path ;
182
+ }
183
+
184
+ @ Override
185
+ public boolean isWriteOperation () {
186
+ return false ;
187
+ }
106
188
}
107
189
}
0 commit comments