Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes for v1.2.0 #318

Merged
merged 5 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/meilisearch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export 'src/searchable.dart';
export 'src/multi_search_result.dart';
export 'src/query_parameters/multi_search_query.dart';
export 'src/query_parameters/index_search_query.dart';
export 'src/filter_builder/_exports.dart' hide MeiliPoint;
export 'src/filter_builder/_exports.dart';
brunoocasali marked this conversation as resolved.
Show resolved Hide resolved
export 'src/query_parameters/_exports.dart';
export 'src/matching_strategy_enum.dart';
export 'src/key.dart';
Expand Down
5 changes: 5 additions & 0 deletions lib/src/annotations.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RequiredMeiliServerVersion {
const RequiredMeiliServerVersion(this.version);

final String version;
}
21 changes: 14 additions & 7 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:dio/dio.dart';
import 'annotations.dart';
import 'query_parameters/_exports.dart';
import 'result.dart';
import 'swap_index.dart';
Expand Down Expand Up @@ -99,8 +100,10 @@ class MeiliSearchClient {

/// Return list of all existing indexes.
Future<Result<MeiliSearchIndex>> getIndexes({IndexesQuery? params}) async {
final response = await http.getMethod<Map<String, Object?>>('/indexes',
queryParameters: params?.toQuery());
final response = await http.getMethod<Map<String, Object?>>(
'/indexes',
queryParameters: params?.toQuery(),
);

return Result<MeiliSearchIndex>.fromMapWithType(
response.data!,
Expand Down Expand Up @@ -256,17 +259,20 @@ class MeiliSearchClient {

/// Get a list of tasks from the client.
Future<TasksResults> getTasks({TasksQuery? params}) async {
final response = await http.getMethod<Map<String, Object?>>('/tasks',
queryParameters: params?.toQuery());
final response = await http.getMethod<Map<String, Object?>>(
'/tasks',
queryParameters: params?.toQuery(),
);

return TasksResults.fromMap(response.data!);
}

/// Cancel tasks based on the input query params
Future<Task> cancelTasks({CancelTasksQuery? params}) async {
final response = await http.postMethod<Map<String, Object?>>(
'/tasks/cancel',
queryParameters: params?.toQuery());
'/tasks/cancel',
queryParameters: params?.toQuery(),
);

return Task.fromMap(response.data!);
}
Expand All @@ -288,10 +294,11 @@ class MeiliSearchClient {
}

/// does a Multi-index search
@RequiredMeiliServerVersion('1.1.0')
Future<MultiSearchResult> multiSearch(MultiSearchQuery query) async {
final response = await http.postMethod<Map<String, Object?>>(
'/multi-search',
data: query.toMap(),
data: query.toSparseMap(),
);

return MultiSearchResult.fromMap(response.data!);
Expand Down
77 changes: 60 additions & 17 deletions lib/src/facade.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'filter_builder/filter_builder_base.dart';
import 'filter_builder/operators.dart';
import 'filter_builder/values.dart';
import 'annotations.dart';
import 'filter_builder/_exports.dart';

/// Convenience class to access all Meilisearch filter expressions

Expand Down Expand Up @@ -74,6 +73,7 @@ class Meili {
distanceInMeters,
);

@RequiredMeiliServerVersion('1.1.0')
static MeiliGeoBoundingBoxOperatorExpression geoBoundingBox(
MeiliPoint p1,
MeiliPoint p2,
Expand All @@ -87,13 +87,30 @@ class Meili {
MeiliAttributeExpression attribute) =>
MeiliNotExistsOperatorExpression(attribute);

/// TODO(ahmednfwela): waiting for Meili V1.2.0
// static MeiliNullOperatorExpression isNull(
// MeiliAttributeExpression attribute) =>
// MeiliNullOperatorExpression(attribute);
// static MeiliNotNullOperatorExpression isNotNull(
// MeiliAttributeExpression attribute) =>
// MeiliNotNullOperatorExpression(attribute);
@RequiredMeiliServerVersion('1.2.0')
static MeiliIsNullOperatorExpression isNull(
MeiliAttributeExpression attribute,
) =>
MeiliIsNullOperatorExpression(attribute);

@RequiredMeiliServerVersion('1.2.0')
static MeiliIsNotNullOperatorExpression isNotNull(
MeiliAttributeExpression attribute,
) =>
MeiliIsNotNullOperatorExpression(attribute);

@RequiredMeiliServerVersion('1.2.0')
static MeiliIsEmptyOperatorExpression isEmpty(
MeiliAttributeExpression attribute,
) =>
MeiliIsEmptyOperatorExpression(attribute);

@RequiredMeiliServerVersion('1.2.0')
static MeiliIsNotEmptyOperatorExpression isNotEmpty(
MeiliAttributeExpression attribute,
) =>
MeiliIsNotEmptyOperatorExpression(attribute);

static MeiliNotOperatorExpression not(MeiliOperatorExpressionBase operator) =>
MeiliNotOperatorExpression(operator);
static MeiliInOperatorExpression $in(MeiliAttributeExpression attribute,
Expand All @@ -103,6 +120,10 @@ class Meili {
values: values,
);

static List<MeiliValueExpressionBase> values(Iterable<Object> v) {
return v.map(value).toList();
}

static MeiliValueExpressionBase value(Object v) {
if (v is String) {
return MeiliStringValueExpression(v);
Expand All @@ -121,10 +142,13 @@ class Meili {
}

static MeiliOrOperatorExpression or(
List<MeiliOperatorExpressionBase> operands) =>
List<MeiliOperatorExpressionBase> operands,
) =>
MeiliOrOperatorExpression.fromList(operands);

static MeiliAndOperatorExpression and(
List<MeiliOperatorExpressionBase> operands) =>
List<MeiliOperatorExpressionBase> operands,
) =>
MeiliAndOperatorExpression.fromList(operands);

static MeiliEmptyExpression empty() => MeiliEmptyExpression();
Expand All @@ -148,27 +172,46 @@ extension MeiliFiltersOperatorsExt on MeiliOperatorExpressionBase {
extension MeiliAttributesExt on MeiliAttributeExpression {
MeiliEqualsOperatorExpression eq(MeiliValueExpressionBase value) =>
Meili.eq(this, value);

MeiliNotEqualsOperatorExpression notEq(MeiliValueExpressionBase value) =>
Meili.notEq(this, value);

MeiliGreaterThanOperatorExpression gt(MeiliValueExpressionBase value) =>
Meili.gt(this, value);

MeiliGreaterThanEqualsOperatorExpression gte(
MeiliValueExpressionBase value) =>
MeiliValueExpressionBase value,
) =>
Meili.gte(this, value);

MeiliLessThanOperatorExpression lt(MeiliValueExpressionBase value) =>
Meili.lt(this, value);

MeiliLessThanEqualsOperatorExpression lte(MeiliValueExpressionBase value) =>
Meili.lte(this, value);

MeiliToOperatorExpression to(
MeiliValueExpressionBase min, MeiliValueExpressionBase max) =>
MeiliValueExpressionBase min,
MeiliValueExpressionBase max,
) =>
Meili.to(this, min, max);

MeiliExistsOperatorExpression exists() => Meili.exists(this);

MeiliNotExistsOperatorExpression notExists() => Meili.notExists(this);

/// TODO(ahmednfwela): waiting for Meili V1.2.0
// MeiliNullOperatorExpression isNull() => Meili.isNull(this);
// MeiliNotNullOperatorExpression isNotNull() => Meili.isNotNull(this);
@RequiredMeiliServerVersion('1.2.0')
MeiliIsNullOperatorExpression isNull() => Meili.isNull(this);

@RequiredMeiliServerVersion('1.2.0')
MeiliIsNotNullOperatorExpression isNotNull() => Meili.isNotNull(this);

@RequiredMeiliServerVersion('1.2.0')
MeiliIsEmptyOperatorExpression isEmpty() => Meili.isEmpty(this);

@RequiredMeiliServerVersion('1.2.0')
MeiliIsNotEmptyOperatorExpression isNotEmpty() => Meili.isNotEmpty(this);

MeiliInOperatorExpression $in(List<MeiliValueExpressionBase> values) =>
Meili.$in(this, values);
}
Expand Down
1 change: 1 addition & 0 deletions lib/src/filter_builder/_exports.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export 'filter_builder_base.dart';
export 'operators.dart';
export 'values.dart';
export 'attribute.dart';
60 changes: 60 additions & 0 deletions lib/src/filter_builder/attribute.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import 'dart:convert';

import 'package:collection/collection.dart';

import 'filter_builder_base.dart';
import 'operators.dart';

const _eq = DeepCollectionEquality();

/// Represents an attribute path in a filter expression
class MeiliAttributeExpression extends MeiliExpressionBase {
final List<String> parts;

MeiliAttributeExpression(String path)
: parts = _normalizeParts(path.split('.'));
MeiliAttributeExpression.fromParts(List<String> parts)
: parts = _normalizeParts(parts);

static List<String> _normalizeParts(List<String> parts) {
return parts
.map((e) => e.trim())
.where((element) => element.isNotEmpty)
.toList();
}

@override
String transform() {
return jsonEncode(parts.join('.'));
}

@override
bool operator ==(Object other) {
if (identical(this, other)) return true;

return other is MeiliAttributeExpression && _eq.equals(other.parts, parts);
}

@override
int get hashCode => _eq.hash(parts);

MeiliLessThanOperatorExpression operator <(MeiliValueExpressionBase value) {
return MeiliLessThanOperatorExpression(property: this, value: value);
}

MeiliGreaterThanOperatorExpression operator >(
MeiliValueExpressionBase value) {
return MeiliGreaterThanOperatorExpression(property: this, value: value);
}

MeiliGreaterThanEqualsOperatorExpression operator >=(
MeiliValueExpressionBase value) {
return MeiliGreaterThanEqualsOperatorExpression(
property: this, value: value);
}

MeiliLessThanEqualsOperatorExpression operator <=(
MeiliValueExpressionBase value) {
return MeiliLessThanEqualsOperatorExpression(property: this, value: value);
}
}
65 changes: 13 additions & 52 deletions lib/src/filter_builder/filter_builder_base.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import 'dart:convert';

import 'operators.dart';

/// Represents an arbitrary filter expression
abstract class MeiliExpressionBase {
const MeiliExpressionBase();

String transform();

@override
String toString() => transform();

/// Fallback equality and hashing, override these when possible to optimize performance
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;

return other is MeiliExpressionBase && other.transform() == transform();
}

@override
int get hashCode => transform().hashCode;
}

/// Represents a filter operator with either at least one operand
Expand All @@ -31,53 +42,3 @@ abstract class MeiliOperatorExpressionBase extends MeiliExpressionBase {
abstract class MeiliValueExpressionBase extends MeiliExpressionBase {
const MeiliValueExpressionBase();
}

/// Represents an empty filter
///
/// works as a starting point for filter builders
class MeiliEmptyExpression extends MeiliOperatorExpressionBase {
const MeiliEmptyExpression();
@override
String transform() => "";
}

/// Represents an attribute path in a filter expression
class MeiliAttributeExpression extends MeiliExpressionBase {
final List<String> parts;

MeiliAttributeExpression(String path)
: parts = _normalizeParts(path.split('.'));
MeiliAttributeExpression.fromParts(List<String> parts)
: parts = _normalizeParts(parts);
static List<String> _normalizeParts(List<String> parts) {
return parts
.map((e) => e.trim())
.where((element) => element.isNotEmpty)
.toList();
}

@override
String transform() {
return jsonEncode(parts.join('.'));
}

MeiliLessThanOperatorExpression operator <(MeiliValueExpressionBase value) {
return MeiliLessThanOperatorExpression(property: this, value: value);
}

MeiliGreaterThanOperatorExpression operator >(
MeiliValueExpressionBase value) {
return MeiliGreaterThanOperatorExpression(property: this, value: value);
}

MeiliGreaterThanEqualsOperatorExpression operator >=(
MeiliValueExpressionBase value) {
return MeiliGreaterThanEqualsOperatorExpression(
property: this, value: value);
}

MeiliLessThanEqualsOperatorExpression operator <=(
MeiliValueExpressionBase value) {
return MeiliLessThanEqualsOperatorExpression(property: this, value: value);
}
}
Loading