Merge pull request #433 from GetStream/hotfix/filters
fix: filter.operator can be null
This commit is contained in:
@@ -87,8 +87,8 @@ extension FilterOperatorX on FilterOperator {
|
|||||||
/// See <a href="https://getstream.io/chat/docs/query_channels/?language=dart" target="_top">Query Channels Documentation</a>
|
/// See <a href="https://getstream.io/chat/docs/query_channels/?language=dart" target="_top">Query Channels Documentation</a>
|
||||||
class Filter extends Equatable {
|
class Filter extends Equatable {
|
||||||
const Filter.__({
|
const Filter.__({
|
||||||
required this.operator,
|
|
||||||
required this.value,
|
required this.value,
|
||||||
|
this.operator,
|
||||||
this.key,
|
this.key,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -159,13 +159,26 @@ class Filter extends Equatable {
|
|||||||
|
|
||||||
/// Creates a custom [Filter] if there isn't one already available.
|
/// Creates a custom [Filter] if there isn't one already available.
|
||||||
const factory Filter.custom({
|
const factory Filter.custom({
|
||||||
required String operator,
|
|
||||||
required Object value,
|
required Object value,
|
||||||
|
String? operator,
|
||||||
String? key,
|
String? key,
|
||||||
}) = Filter.__;
|
}) = Filter.__;
|
||||||
|
|
||||||
|
/// Creates a custom [Filter] from a raw map value
|
||||||
|
///
|
||||||
|
/// ```dart
|
||||||
|
/// final filter = Filter.raw(
|
||||||
|
/// {
|
||||||
|
/// 'members': [user1.id, user2.id],
|
||||||
|
/// }
|
||||||
|
/// )
|
||||||
|
/// ```
|
||||||
|
const factory Filter.raw({
|
||||||
|
required Map<String, Object?> value,
|
||||||
|
}) = Filter.__;
|
||||||
|
|
||||||
/// An operator used for the filter. The operator string must start with `$`
|
/// An operator used for the filter. The operator string must start with `$`
|
||||||
final String operator;
|
final String? operator;
|
||||||
|
|
||||||
/// The "left-hand" side of the filter.
|
/// The "left-hand" side of the filter.
|
||||||
/// Specifies the name of the field the filter should match.
|
/// Specifies the name of the field the filter should match.
|
||||||
@@ -183,24 +196,22 @@ class Filter extends Equatable {
|
|||||||
List<Object?> get props => [operator, key, value];
|
List<Object?> get props => [operator, key, value];
|
||||||
|
|
||||||
/// Serializes to json object
|
/// Serializes to json object
|
||||||
Map<String, Object> toJson() {
|
Map<String, Object?> toJson() {
|
||||||
final json = <String, Object>{};
|
final json = <String, Object?>{};
|
||||||
final groupOperators = _groupOperators.map((it) => it.rawValue);
|
final groupOperators = _groupOperators.map((it) => it.rawValue);
|
||||||
|
|
||||||
assert(
|
|
||||||
groupOperators.contains(operator) || key != null,
|
|
||||||
'Filter must contain the `key` when the operator is not a '
|
|
||||||
'group operator.',
|
|
||||||
);
|
|
||||||
|
|
||||||
if (groupOperators.contains(operator)) {
|
if (groupOperators.contains(operator)) {
|
||||||
// Filters with group operators are encoded in the following form:
|
// Filters with group operators are encoded in the following form:
|
||||||
// { $<operator>: [ <filter 1>, <filter 2> ] }
|
// { $<operator>: [ <filter 1>, <filter 2> ] }
|
||||||
json[operator] = value;
|
json[operator!] = value;
|
||||||
} else {
|
} else if (operator != null) {
|
||||||
// Normal filters are encoded in the following form:
|
// Normal filters are encoded in the following form:
|
||||||
// { key: { $<operator>: <value> } }
|
// { key: { $<operator>: <value> } }
|
||||||
json[key!] = {operator: value};
|
json[key!] = {operator: value};
|
||||||
|
} else if (key != null) {
|
||||||
|
json[key!] = value;
|
||||||
|
} else {
|
||||||
|
return value as Map<String, Object?>;
|
||||||
}
|
}
|
||||||
|
|
||||||
return json;
|
return json;
|
||||||
|
|||||||
@@ -130,6 +130,14 @@ void main() {
|
|||||||
expect(filter.operator, operator);
|
expect(filter.operator, operator);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('raw', () {
|
||||||
|
const value = {
|
||||||
|
'test': ['a', 'b'],
|
||||||
|
};
|
||||||
|
const filter = Filter.raw(value: value);
|
||||||
|
expect(filter.value, value);
|
||||||
|
});
|
||||||
|
|
||||||
group('groupedOperator', () {
|
group('groupedOperator', () {
|
||||||
final filter1 = Filter.equal('testKey', 'testValue');
|
final filter1 = Filter.equal('testKey', 'testValue');
|
||||||
final filter2 = Filter.in_('testKey', const ['testValue']);
|
final filter2 = Filter.in_('testKey', const ['testValue']);
|
||||||
@@ -180,6 +188,30 @@ void main() {
|
|||||||
'{"$key":{"${FilterOperator.in_.rawValue}":${json.encode(values)}}}',
|
'{"$key":{"${FilterOperator.in_.rawValue}":${json.encode(values)}}}',
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('custom with no operator', () {
|
||||||
|
const key = 'testKey';
|
||||||
|
const values = ['testValue'];
|
||||||
|
final filter = Filter.custom(key: key, value: values);
|
||||||
|
final encoded = json.encode(filter);
|
||||||
|
expect(
|
||||||
|
encoded,
|
||||||
|
'{"$key":${json.encode(values)}}',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('raw', () {
|
||||||
|
const value = {
|
||||||
|
'test': ['a', 'b'],
|
||||||
|
};
|
||||||
|
const filter = Filter.raw(value: value);
|
||||||
|
|
||||||
|
final encoded = json.encode(filter);
|
||||||
|
expect(
|
||||||
|
encoded,
|
||||||
|
json.encode(value),
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('groupedFilter', () {
|
test('groupedFilter', () {
|
||||||
|
|||||||
Reference in New Issue
Block a user