Skip to content

Commit 94c397b

Browse files
author
Giorgio
committed
Fix when setting expression after a raw map
1 parent 546badc commit 94c397b

22 files changed

+161
-98
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"dart.sdkPath": "/home/giorgio/dart/dart-sdk.2.9.2"
3+
}

CHANGELOG.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1+
# Changelog
2+
3+
## 0.4.2
4+
5+
* Fixed problem in SelectorBuilder:
6+
if you set a raw map, then you couldn't add any new query expression or you loose the inital raw map "query" section.
7+
* Lint clean-up
8+
19
## 0.4.1
10+
211
* class `Set` of aggregation stages moved to class `SetStage` to resolve conflict with `dart:core` library
3-
12+
413
## 0.4.0 - September 30th, 2019 (@alexd1971, @vadimtsushko)
14+
515
* Aggregation pipeline builder [PR#14](https://github.com/mongo-dart/mongo_dart_query/pull/14)
616

717
## 0.3.1 - June 30th, 2018 (@thosakwe, @aemino)
18+
819
* Merged [PR #11](https://github.com/mongo-dart/mongo_dart_query/pull/11) from @aemino,
9-
which adds Dart 2 fixes for this package, namely coercin of generic types avoiding implicit casts.
20+
which adds Dart 2 fixes for this package, namely coercin of generic types avoiding implicit casts

analysis_options.yaml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
# Defines a default set of lint rules enforced for
2+
# projects at Google. For details and rationale,
3+
# see https://github.com/dart-lang/pedantic#enabled-lints.
4+
include: package:pedantic/analysis_options.yaml
5+
6+
# For lint rules and documentation, see http://dart-lang.github.io/linter/lints.
7+
# Uncomment to specify additional rules.
8+
# linter:
9+
# rules:
10+
# - camel_case_types
11+
12+
113
analyzer:
2-
strong-mode:
3-
implicit-casts: false
14+
### exclude:
15+
### - path/to/excluded/files/**

lib/src/modifier_builder.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ ModifierBuilder get modify => ModifierBuilder();
55
class ModifierBuilder {
66
Map<String, dynamic> map = {};
77

8-
toString() => "ModifierBuilder($map)";
8+
@override
9+
String toString() => 'ModifierBuilder($map)';
910

1011
void _updateOperation(String operator, String fieldName, value) {
11-
Map<String, dynamic> opMap = map[operator] as Map<String, dynamic>;
12+
var opMap = map[operator] as Map<String, dynamic>;
1213
if (opMap == null) {
1314
opMap = <String, dynamic>{};
1415
map[operator] = opMap;

lib/src/mongo_aggregation/aggregation_base.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class AEList extends Iterable implements AggregationExpr {
4242
@protected
4343
AEList.internal(this._iterable);
4444

45+
@override
4546
_AEIterator get iterator => _AEIterator(_iterable);
4647
@override
4748
List build() => _iterable
@@ -67,6 +68,7 @@ class _AEIterator<T> implements Iterator<T> {
6768
return true;
6869
}
6970

71+
@override
7072
T get current => _current;
7173
}
7274

@@ -84,11 +86,13 @@ class AEObject extends Iterable<MapEntry<String, dynamic>>
8486
@protected
8587
AEObject.internal(Map<String, dynamic> map)
8688
: _iterable = map.entries.where(_valueIsNotNull).map((entry) {
87-
if (entry.value is List)
89+
if (entry.value is List) {
8890
return MapEntry(entry.key, AEList(entry.value as List));
89-
if (entry.value is Map<String, dynamic>)
91+
}
92+
if (entry.value is Map<String, dynamic>) {
9093
return MapEntry(
9194
entry.key, AEObject(entry.value as Map<String, dynamic>));
95+
}
9296
return entry;
9397
});
9498
@override

lib/src/mongo_aggregation/aggregation_pipeline_builder.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class AggregationPipelineBuilder implements Builder {
1717
///
1818
/// Returns aggregation pipeline in format suitable for mongodb aggregate
1919
/// query
20+
@override
2021
List<Map<String, dynamic>> build() =>
2122
stages.map((stage) => stage.build()).toList();
2223
}

lib/src/mongo_aggregation/pipeline_builder.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class AggregationPipelineBuilder implements Builder {
1717
///
1818
/// Returns aggregation pipeline in format suitable for mongodb aggregate
1919
/// query
20+
@override
2021
List<Map<String, dynamic>> build() =>
2122
stages.map((stage) => stage.build()).toList();
2223
}

lib/src/selector_builder.dart

Lines changed: 49 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
part of mongo_dart_query;
22

33
SelectorBuilder get where => SelectorBuilder();
4+
const keyQuery = r'$query';
45

56
class SelectorBuilder {
6-
static final RegExp objectIdRegexp = RegExp(".ObjectId...([0-9a-f]{24})....");
7+
static final RegExp objectIdRegexp = RegExp('.ObjectId...([0-9a-f]{24})....');
78
Map<String, dynamic> map = {};
8-
bool _isQuerySet = false;
99
Map<String, dynamic> get _query {
10-
if (!_isQuerySet) {
11-
map['\$query'] = <String, dynamic>{};
12-
_isQuerySet = true;
10+
if (!map.containsKey(keyQuery)) {
11+
map[keyQuery] = <String, dynamic>{};
1312
}
14-
return map['\$query'] as Map<String, dynamic>;
13+
return map[keyQuery] as Map<String, dynamic>;
1514
}
1615

1716
int paramSkip = 0;
1817
int paramLimit = 0;
1918
Map<String, dynamic> paramFields;
2019

21-
String toString() => "SelectorBuilder($map)";
20+
@override
21+
String toString() => 'SelectorBuilder($map)';
2222

23-
_addExpression(String fieldName, value) {
24-
Map<String, dynamic> exprMap = {};
23+
void _addExpression(String fieldName, value) {
24+
var exprMap = <String, dynamic>{};
2525
exprMap[fieldName] = value;
2626
if (_query.isEmpty) {
2727
_query[fieldName] = value;
@@ -30,9 +30,9 @@ class SelectorBuilder {
3030
}
3131
}
3232

33-
_addExpressionMap(Map<String, dynamic> expr) {
33+
void _addExpressionMap(Map<String, dynamic> expr) {
3434
if (_query.containsKey('\$and')) {
35-
List expressions = _query['\$and'] as List;
35+
var expressions = _query['\$and'] as List;
3636
expressions.add(expr);
3737
} else {
3838
var expressions = [_query];
@@ -41,16 +41,12 @@ class SelectorBuilder {
4141
}
4242
}
4343

44-
void _ensureParamFields() {
45-
if (paramFields == null) {
46-
paramFields = <String, dynamic>{};
47-
}
48-
}
44+
void _ensureParamFields() => paramFields ??= <String, dynamic>{};
4945

5046
void _ensureOrderBy() {
5147
_query;
52-
if (!map.containsKey("orderby")) {
53-
map["orderby"] = <String, dynamic>{};
48+
if (!map.containsKey('orderby')) {
49+
map['orderby'] = <String, dynamic>{};
5450
}
5551
}
5652

@@ -65,58 +61,58 @@ class SelectorBuilder {
6561
}
6662

6763
SelectorBuilder ne(String fieldName, value) {
68-
_addExpression(fieldName, {"\$ne": value});
64+
_addExpression(fieldName, {'\$ne': value});
6965
return this;
7066
}
7167

7268
SelectorBuilder gt(String fieldName, value) {
73-
_addExpression(fieldName, {"\$gt": value});
69+
_addExpression(fieldName, {'\$gt': value});
7470
return this;
7571
}
7672

7773
SelectorBuilder lt(String fieldName, value) {
78-
_addExpression(fieldName, {"\$lt": value});
74+
_addExpression(fieldName, {'\$lt': value});
7975
return this;
8076
}
8177

8278
SelectorBuilder gte(String fieldName, value) {
83-
_addExpression(fieldName, {"\$gte": value});
79+
_addExpression(fieldName, {'\$gte': value});
8480
return this;
8581
}
8682

8783
SelectorBuilder lte(String fieldName, value) {
88-
_addExpression(fieldName, {"\$lte": value});
84+
_addExpression(fieldName, {'\$lte': value});
8985
return this;
9086
}
9187

9288
SelectorBuilder all(String fieldName, List values) {
93-
_addExpression(fieldName, {"\$all": values});
89+
_addExpression(fieldName, {'\$all': values});
9490
return this;
9591
}
9692

9793
SelectorBuilder nin(String fieldName, List values) {
98-
_addExpression(fieldName, {"\$nin": values});
94+
_addExpression(fieldName, {'\$nin': values});
9995
return this;
10096
}
10197

10298
SelectorBuilder oneFrom(String fieldName, List values) {
103-
_addExpression(fieldName, {"\$in": values});
99+
_addExpression(fieldName, {'\$in': values});
104100
return this;
105101
}
106102

107103
SelectorBuilder exists(String fieldName) {
108-
_addExpression(fieldName, {"\$exists": true});
104+
_addExpression(fieldName, {'\$exists': true});
109105
return this;
110106
}
111107

112108
SelectorBuilder notExists(String fieldName) {
113-
_addExpression(fieldName, {"\$exists": false});
109+
_addExpression(fieldName, {'\$exists': false});
114110
return this;
115111
}
116112

117113
SelectorBuilder mod(String fieldName, int value) {
118114
_addExpression(fieldName, {
119-
"\$mod": [value, 0]
115+
'\$mod': [value, 0]
120116
});
121117
return this;
122118
}
@@ -135,94 +131,94 @@ class SelectorBuilder {
135131

136132
SelectorBuilder inRange(String fieldName, min, max,
137133
{bool minInclude = true, bool maxInclude = false}) {
138-
Map<String, dynamic> rangeMap = {};
134+
var rangeMap = <String, dynamic>{};
139135
if (minInclude) {
140-
rangeMap["\$gte"] = min;
136+
rangeMap['\$gte'] = min;
141137
} else {
142-
rangeMap["\$gt"] = min;
138+
rangeMap['\$gt'] = min;
143139
}
144140
if (maxInclude) {
145-
rangeMap["\$lte"] = max;
141+
rangeMap['\$lte'] = max;
146142
} else {
147-
rangeMap["\$lt"] = max;
143+
rangeMap['\$lt'] = max;
148144
}
149145
_addExpression(fieldName, rangeMap);
150146
return this;
151147
}
152148

153149
SelectorBuilder sortBy(String fieldName, {bool descending = false}) {
154150
_ensureOrderBy();
155-
int order = 1;
151+
var order = 1;
156152
if (descending) {
157153
order = -1;
158154
}
159-
map["orderby"][fieldName] = order;
155+
map['orderby'][fieldName] = order;
160156
return this;
161157
}
162158

163159
SelectorBuilder sortByMetaTextScore(String fieldName) {
164160
_ensureOrderBy();
165-
map["orderby"][fieldName] = <String, dynamic>{'\$meta': "textScore"};
161+
map['orderby'][fieldName] = <String, dynamic>{'\$meta': 'textScore'};
166162
return this;
167163
}
168164

169165
SelectorBuilder hint(String fieldName, {bool descending = false}) {
170166
_query;
171-
if (!map.containsKey("\$hint")) {
172-
map["\$hint"] = <String, dynamic>{};
167+
if (!map.containsKey('\$hint')) {
168+
map['\$hint'] = <String, dynamic>{};
173169
}
174-
int order = 1;
170+
var order = 1;
175171
if (descending) {
176172
order = -1;
177173
}
178-
map["\$hint"][fieldName] = order;
174+
map['\$hint'][fieldName] = order;
179175
return this;
180176
}
181177

182178
SelectorBuilder hintIndex(String indexName) {
183179
_query;
184-
map["\$hint"] = indexName;
180+
map['\$hint'] = indexName;
185181
return this;
186182
}
187183

188184
SelectorBuilder comment(String commentStr) {
189185
_query;
190-
map["\$comment"] = commentStr;
186+
map['\$comment'] = commentStr;
191187
return this;
192188
}
193189

194190
SelectorBuilder explain() {
195191
_query;
196-
map["\$explain"] = true;
192+
map['\$explain'] = true;
197193
return this;
198194
}
199195

200196
SelectorBuilder snapshot() {
201197
_query;
202-
map["\$snapshot"] = true;
198+
map['\$snapshot'] = true;
203199
return this;
204200
}
205201

206202
SelectorBuilder showDiskLoc() {
207203
_query;
208-
map["\$showDiskLoc"] = true;
204+
map['\$showDiskLoc'] = true;
209205
return this;
210206
}
211207

212208
SelectorBuilder returnKey() {
213209
_query;
214-
map["\$sreturnKey"] = true;
210+
map['\$sreturnKey'] = true;
215211
return this;
216212
}
217213

218214
SelectorBuilder jsQuery(String javaScriptCode) {
219-
_query["\$where"] = BsonCode(javaScriptCode);
215+
_query['\$where'] = BsonCode(javaScriptCode);
220216
return this;
221217
}
222218

223219
SelectorBuilder metaTextScore(String fieldName) {
224220
_ensureParamFields();
225-
paramFields[fieldName] = {'\$meta': "textScore"};
221+
paramFields[fieldName] = {'\$meta': 'textScore'};
226222

227223
return this;
228224
}
@@ -260,17 +256,17 @@ class SelectorBuilder {
260256

261257
SelectorBuilder within(String fieldName, value) {
262258
_addExpression(fieldName, {
263-
"\$within": {"\$box": value}
259+
'\$within': {'\$box': value}
264260
});
265261
return this;
266262
}
267263

268264
SelectorBuilder near(String fieldName, var value, [double maxDistance]) {
269265
if (maxDistance == null) {
270-
_addExpression(fieldName, {"\$near": value});
266+
_addExpression(fieldName, {'\$near': value});
271267
} else {
272268
_addExpression(
273-
fieldName, {"\$near": value, "\$maxDistance": maxDistance});
269+
fieldName, {'\$near': value, '\$maxDistance': maxDistance});
274270
}
275271
return this;
276272
}
@@ -309,7 +305,7 @@ class SelectorBuilder {
309305
throw StateError('`And` opertion is not supported on empty query');
310306
}
311307
if (_query.containsKey('\$or')) {
312-
List expressions = _query['\$or'] as List;
308+
var expressions = _query['\$or'] as List;
313309
expressions.add(other._query);
314310
} else {
315311
var expressions = [_query];

0 commit comments

Comments
 (0)