3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
5
import 'dart:async' ;
6
+ import 'dart:convert' ;
6
7
import 'dart:math' show max;
7
8
8
9
import 'package:_pub_shared/search/search_form.dart' ;
9
10
import 'package:_pub_shared/search/search_request_data.dart' ;
10
11
import 'package:_pub_shared/search/tags.dart' ;
11
12
import 'package:clock/clock.dart' ;
12
- import 'package:collection/collection.dart' ;
13
13
import 'package:json_annotation/json_annotation.dart' ;
14
14
import 'package:pub_dev/shared/utils.dart' ;
15
15
@@ -158,36 +158,9 @@ class ApiDocPage {
158
158
}
159
159
160
160
class ServiceSearchQuery {
161
- final String ? query;
162
- final ParsedQueryText parsedQuery;
163
- final TagsPredicate tagsPredicate;
161
+ final SearchRequestData _data;
164
162
165
- final String ? publisherId;
166
-
167
- final int ? minPoints;
168
-
169
- /// The value of the `sort` URL query parameter.
170
- final SearchOrder ? order;
171
- final int offset;
172
- final int limit;
173
-
174
- /// The scope/depth of text matching.
175
- final TextMatchExtent ? textMatchExtent;
176
-
177
- ServiceSearchQuery ._({
178
- this .query,
179
- TagsPredicate ? tagsPredicate,
180
- String ? publisherId,
181
- required this .minPoints,
182
- this .order,
183
- int ? offset,
184
- int ? limit,
185
- this .textMatchExtent,
186
- }) : offset = max (0 , offset ?? 0 ),
187
- limit = max (_minSearchLimit, limit ?? 10 ),
188
- parsedQuery = ParsedQueryText .parse (query),
189
- tagsPredicate = tagsPredicate ?? TagsPredicate (),
190
- publisherId = publisherId? .trimToNull ();
163
+ ServiceSearchQuery (this ._data);
191
164
192
165
factory ServiceSearchQuery .parse ({
193
166
String ? query,
@@ -200,96 +173,47 @@ class ServiceSearchQuery {
200
173
TextMatchExtent ? textMatchExtent,
201
174
}) {
202
175
final q = query? .trimToNull ();
203
- return ServiceSearchQuery ._(
176
+ final tags = tagsPredicate? .toQueryParameters ();
177
+ return ServiceSearchQuery (SearchRequestData (
204
178
query: q,
205
- tagsPredicate : tagsPredicate ,
179
+ tags : tags ,
206
180
publisherId: publisherId,
207
181
minPoints: minPoints,
208
182
order: order,
209
183
offset: offset,
210
184
limit: limit,
211
185
textMatchExtent: textMatchExtent,
212
- );
186
+ )) ;
213
187
}
214
188
215
- factory ServiceSearchQuery .fromServiceUrl (Uri uri) {
216
- final q = uri.queryParameters['q' ];
217
- final tagsPredicate =
218
- TagsPredicate .parseQueryValues (uri.queryParametersAll['tags' ]);
219
- final publisherId = uri.queryParameters['publisherId' ];
220
- final String ? orderValue = uri.queryParameters['order' ];
221
- final SearchOrder ? order = parseSearchOrder (orderValue);
222
-
223
- final minPoints =
224
- int .tryParse (uri.queryParameters['minPoints' ] ?? '0' ) ?? 0 ;
225
- final offset = int .tryParse (uri.queryParameters['offset' ] ?? '0' ) ?? 0 ;
226
- final limit = int .tryParse (uri.queryParameters['limit' ] ?? '0' ) ?? 0 ;
227
- final textMatchExtentValue =
228
- uri.queryParameters['textMatchExtent' ]? .trim () ?? '' ;
229
- final textMatchExtent = TextMatchExtent .values
230
- .firstWhereOrNull ((e) => e.name == textMatchExtentValue);
231
-
232
- return ServiceSearchQuery .parse (
233
- query: q,
234
- tagsPredicate: tagsPredicate,
189
+ ServiceSearchQuery change ({
190
+ TextMatchExtent ? textMatchExtent,
191
+ }) {
192
+ return ServiceSearchQuery (SearchRequestData (
193
+ query: query,
194
+ tags: _data.tags,
235
195
publisherId: publisherId,
236
196
order: order,
237
197
minPoints: minPoints,
238
198
offset: offset,
239
199
limit: limit,
240
- textMatchExtent: textMatchExtent,
241
- );
200
+ textMatchExtent: textMatchExtent ?? this .textMatchExtent ,
201
+ )) ;
242
202
}
243
203
244
- factory ServiceSearchQuery .fromSearchRequestData (SearchRequestData data) {
245
- final tagsPredicate = TagsPredicate .parseQueryValues (data.tags);
246
- return ServiceSearchQuery .parse (
247
- query: data.query,
248
- tagsPredicate: tagsPredicate,
249
- publisherId: data.publisherId,
250
- order: data.order,
251
- minPoints: data.minPoints,
252
- offset: data.offset ?? 0 ,
253
- limit: data.limit,
254
- textMatchExtent: data.textMatchExtent,
255
- );
256
- }
204
+ late final parsedQuery = ParsedQueryText .parse (_data.query);
205
+ late final tagsPredicate = TagsPredicate .parseQueryValues (_data.tags);
257
206
258
- ServiceSearchQuery change ({
259
- String ? query,
260
- TagsPredicate ? tagsPredicate,
261
- String ? publisherId,
262
- SearchOrder ? order,
263
- int ? offset,
264
- int ? limit,
265
- TextMatchExtent ? textMatchExtent,
266
- }) {
267
- return ServiceSearchQuery ._(
268
- query: query ?? this .query,
269
- tagsPredicate: tagsPredicate ?? this .tagsPredicate,
270
- publisherId: publisherId ?? this .publisherId,
271
- order: order ?? this .order,
272
- minPoints: minPoints,
273
- offset: offset ?? this .offset,
274
- limit: limit ?? this .limit,
275
- textMatchExtent: textMatchExtent ?? this .textMatchExtent,
276
- );
277
- }
207
+ String ? get query => _data.query;
208
+ String ? get publisherId => _data.publisherId? .trimToNull ();
209
+ int ? get minPoints => _data.minPoints;
210
+ SearchOrder ? get order => _data.order;
211
+ int get offset => max (0 , _data.offset ?? 0 );
212
+ int get limit => max (_minSearchLimit, _data.limit ?? 10 );
213
+ TextMatchExtent ? get textMatchExtent => _data.textMatchExtent;
278
214
279
215
Map <String , dynamic > toUriQueryParameters () {
280
- final map = < String , dynamic > {
281
- 'q' : query,
282
- 'tags' : tagsPredicate.toQueryParameters (),
283
- 'publisherId' : publisherId,
284
- 'offset' : offset.toString (),
285
- if (minPoints != null && minPoints! > 0 )
286
- 'minPoints' : minPoints.toString (),
287
- 'limit' : limit.toString (),
288
- 'order' : order? .name,
289
- if (textMatchExtent != null ) 'textMatchExtent' : textMatchExtent! .name,
290
- };
291
- map.removeWhere ((k, v) => v == null );
292
- return map;
216
+ return _data.toUriQueryParameters ();
293
217
}
294
218
295
219
/// The effective sort order to use:
@@ -327,17 +251,10 @@ class ServiceSearchQuery {
327
251
}
328
252
329
253
SearchRequestData toSearchRequestData () {
330
- return SearchRequestData (
331
- query: query,
332
- tags: tagsPredicate.toQueryParameters (),
333
- publisherId: publisherId,
334
- minPoints: minPoints,
335
- order: order,
336
- offset: offset,
337
- limit: limit,
338
- textMatchExtent: textMatchExtent,
339
- );
254
+ return _data;
340
255
}
256
+
257
+ String toDebugString () => json.encode (_data.toJson ());
341
258
}
342
259
343
260
class QueryValidity {
0 commit comments