25
25
import org .springframework .util .StringUtils ;
26
26
27
27
/**
28
- * A {@link MongoExpression} using the {@link ParameterBindingDocumentCodec} for parsing the {@literal json} expression.
29
- * The expression will be wrapped within <code>{ }</code> if necessary. Placeholders like {@code ?0} are resolved when
30
- * first obtaining the target {@link Document} via {@link #toDocument()}.
28
+ * A {@link MongoExpression} using the {@link ParameterBindingDocumentCodec} for parsing a raw ({@literal json})
29
+ * expression. The expression will be wrapped within <code>{ ... }</code> if necessary. The actual parsing and parameter
30
+ * binding of placeholders like {@code ?0} is delayed upon first call on the the target {@link Document} via
31
+ * {@link #toDocument()}.
31
32
* <p />
32
33
*
33
34
* <pre class="code">
38
39
* { '$toUpper' : '?0' }, "$name" -> { '$toUpper' : '$name' }
39
40
* </pre>
40
41
*
41
- * Some types (like {@link java.util.UUID}) cannot be used directly but require a special {@link org.bson.codecs.Codec}.
42
- * Make sure to provide a {@link CodecRegistry} containing the required {@link org.bson.codecs.Codec codecs} via
43
- * {@link #withCodecRegistry(CodecRegistry)}.
42
+ * Some types might require a special {@link org.bson.codecs.Codec}. If so, make sure to provide a {@link CodecRegistry}
43
+ * containing the required {@link org.bson.codecs.Codec codec} via {@link #withCodecRegistry(CodecRegistry)}.
44
44
*
45
45
* @author Christoph Strobl
46
46
* @since 3.2
47
47
*/
48
48
public class BindableMongoExpression implements MongoExpression {
49
49
50
- private final String json ;
50
+ private final String expressionString ;
51
51
52
52
@ Nullable //
53
53
private final CodecRegistryProvider codecRegistryProvider ;
@@ -60,24 +60,24 @@ public class BindableMongoExpression implements MongoExpression {
60
60
/**
61
61
* Create a new instance of {@link BindableMongoExpression}.
62
62
*
63
- * @param json must not be {@literal null}.
63
+ * @param expression must not be {@literal null}.
64
64
* @param args can be {@literal null}.
65
65
*/
66
- public BindableMongoExpression (String json , @ Nullable Object [] args ) {
67
- this (json , null , args );
66
+ public BindableMongoExpression (String expression , @ Nullable Object [] args ) {
67
+ this (expression , null , args );
68
68
}
69
69
70
70
/**
71
71
* Create a new instance of {@link BindableMongoExpression}.
72
72
*
73
- * @param json must not be {@literal null}.
73
+ * @param expression must not be {@literal null}.
74
74
* @param codecRegistryProvider can be {@literal null}.
75
75
* @param args can be {@literal null}.
76
76
*/
77
- public BindableMongoExpression (String json , @ Nullable CodecRegistryProvider codecRegistryProvider ,
77
+ public BindableMongoExpression (String expression , @ Nullable CodecRegistryProvider codecRegistryProvider ,
78
78
@ Nullable Object [] args ) {
79
79
80
- this .json = wrapJsonIfNecessary ( json ) ;
80
+ this .expressionString = expression ;
81
81
this .codecRegistryProvider = codecRegistryProvider ;
82
82
this .args = args ;
83
83
this .target = Lazy .of (this ::parse );
@@ -90,7 +90,7 @@ public BindableMongoExpression(String json, @Nullable CodecRegistryProvider code
90
90
* @return new instance of {@link BindableMongoExpression}.
91
91
*/
92
92
public BindableMongoExpression withCodecRegistry (CodecRegistry codecRegistry ) {
93
- return new BindableMongoExpression (json , () -> codecRegistry , args );
93
+ return new BindableMongoExpression (expressionString , () -> codecRegistry , args );
94
94
}
95
95
96
96
/**
@@ -100,19 +100,27 @@ public BindableMongoExpression withCodecRegistry(CodecRegistry codecRegistry) {
100
100
* @return new instance of {@link BindableMongoExpression}.
101
101
*/
102
102
public BindableMongoExpression bind (Object ... args ) {
103
- return new BindableMongoExpression (json , codecRegistryProvider , args );
103
+ return new BindableMongoExpression (expressionString , codecRegistryProvider , args );
104
104
}
105
105
106
106
/*
107
107
* (non-Javadoc)
108
- *
109
108
* @see org.springframework.data.mongodb.MongoExpression#toDocument()
110
109
*/
111
110
@ Override
112
111
public Document toDocument () {
113
112
return target .get ();
114
113
}
115
114
115
+ /*
116
+ * (non-Javadoc)
117
+ * @see java.lang.Object#toString()
118
+ */
119
+ @ Override
120
+ public String toString () {
121
+ return "BindableMongoExpression{" + "expressionString='" + expressionString + '\'' + ", args=" + args + '}' ;
122
+ }
123
+
116
124
private String wrapJsonIfNecessary (String json ) {
117
125
118
126
if (StringUtils .hasText (json ) && (json .startsWith ("{" ) && json .endsWith ("}" ))) {
@@ -124,18 +132,20 @@ private String wrapJsonIfNecessary(String json) {
124
132
125
133
private Document parse () {
126
134
135
+ String expression = wrapJsonIfNecessary (expressionString );
136
+
127
137
if (ObjectUtils .isEmpty (args )) {
128
138
129
139
if (codecRegistryProvider == null ) {
130
- return Document .parse (json );
140
+ return Document .parse (expression );
131
141
}
132
142
133
- return Document .parse (json , codecRegistryProvider .getCodecFor (Document .class )
143
+ return Document .parse (expression , codecRegistryProvider .getCodecFor (Document .class )
134
144
.orElseGet (() -> new DocumentCodec (codecRegistryProvider .getCodecRegistry ())));
135
145
}
136
146
137
147
ParameterBindingDocumentCodec codec = codecRegistryProvider == null ? new ParameterBindingDocumentCodec ()
138
148
: new ParameterBindingDocumentCodec (codecRegistryProvider .getCodecRegistry ());
139
- return codec .decode (json , args );
149
+ return codec .decode (expression , args );
140
150
}
141
151
}
0 commit comments