From 6088c0bd9995ede41149a4689cee37065e31eb37 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Mon, 17 May 2021 12:27:22 +0200 Subject: [PATCH 1/5] operator can be null in certain cases --- packages/stream_chat/lib/src/models/filter.dart | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/stream_chat/lib/src/models/filter.dart b/packages/stream_chat/lib/src/models/filter.dart index 2356c57b..82ca1177 100644 --- a/packages/stream_chat/lib/src/models/filter.dart +++ b/packages/stream_chat/lib/src/models/filter.dart @@ -87,8 +87,8 @@ extension FilterOperatorX on FilterOperator { /// See Query Channels Documentation class Filter extends Equatable { const Filter.__({ - required this.operator, required this.value, + this.operator, this.key, }); @@ -159,13 +159,13 @@ class Filter extends Equatable { /// Creates a custom [Filter] if there isn't one already available. const factory Filter.custom({ - required String operator, required Object value, + String? operator, String? key, }) = Filter.__; /// 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. /// Specifies the name of the field the filter should match. @@ -188,7 +188,7 @@ class Filter extends Equatable { final groupOperators = _groupOperators.map((it) => it.rawValue); assert( - groupOperators.contains(operator) || key != null, + (operator != null && groupOperators.contains(operator)) || key != null, 'Filter must contain the `key` when the operator is not a ' 'group operator.', ); @@ -196,11 +196,13 @@ class Filter extends Equatable { if (groupOperators.contains(operator)) { // Filters with group operators are encoded in the following form: // { $: [ , ] } - json[operator] = value; - } else { + json[operator!] = value; + } else if (operator != null) { // Normal filters are encoded in the following form: // { key: { $: } } json[key!] = {operator: value}; + } else { + json[key!] = value; } return json; From c7eeb35ef98cb033fa069c1a42239cc08bd10c65 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Mon, 17 May 2021 12:35:13 +0200 Subject: [PATCH 2/5] add test --- .../stream_chat/test/src/models/filter_test.dart | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/stream_chat/test/src/models/filter_test.dart b/packages/stream_chat/test/src/models/filter_test.dart index c73c0d0b..e09aff53 100644 --- a/packages/stream_chat/test/src/models/filter_test.dart +++ b/packages/stream_chat/test/src/models/filter_test.dart @@ -180,6 +180,19 @@ void main() { '{"$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)}}', + ); + print('asdasda'); + print('{"$key":${json.encode(values)}}'); + }); }); test('groupedFilter', () { From 16953b3a0a096f2a0bc49624cce689a5f9fd6e12 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Mon, 17 May 2021 12:35:57 +0200 Subject: [PATCH 3/5] remove print --- packages/stream_chat/test/src/models/filter_test.dart | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/stream_chat/test/src/models/filter_test.dart b/packages/stream_chat/test/src/models/filter_test.dart index e09aff53..99c20233 100644 --- a/packages/stream_chat/test/src/models/filter_test.dart +++ b/packages/stream_chat/test/src/models/filter_test.dart @@ -190,8 +190,6 @@ void main() { encoded, '{"$key":${json.encode(values)}}', ); - print('asdasda'); - print('{"$key":${json.encode(values)}}'); }); }); From 26e1c21e70d9a7f2ad80db4c4e9efe9e51ea76e0 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Mon, 17 May 2021 12:53:37 +0200 Subject: [PATCH 4/5] add filter.raw --- .../stream_chat/lib/src/models/filter.dart | 21 +++++++++++-------- .../test/src/models/filter_test.dart | 21 +++++++++++++++++++ 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/packages/stream_chat/lib/src/models/filter.dart b/packages/stream_chat/lib/src/models/filter.dart index 82ca1177..a2f4ce4b 100644 --- a/packages/stream_chat/lib/src/models/filter.dart +++ b/packages/stream_chat/lib/src/models/filter.dart @@ -1,5 +1,7 @@ // ignore_for_file: non_constant_identifier_names, constant_identifier_names +import 'dart:convert'; + import 'package:equatable/equatable.dart'; const _groupOperators = [ @@ -164,6 +166,11 @@ class Filter extends Equatable { String? key, }) = Filter.__; + /// Creates a custom [Filter] from a raw value + const factory Filter.raw({ + required Map value, + }) = Filter.__; + /// An operator used for the filter. The operator string must start with `$` final String? operator; @@ -183,16 +190,10 @@ class Filter extends Equatable { List get props => [operator, key, value]; /// Serializes to json object - Map toJson() { - final json = {}; + Map toJson() { + final json = {}; final groupOperators = _groupOperators.map((it) => it.rawValue); - assert( - (operator != null && groupOperators.contains(operator)) || key != null, - 'Filter must contain the `key` when the operator is not a ' - 'group operator.', - ); - if (groupOperators.contains(operator)) { // Filters with group operators are encoded in the following form: // { $: [ , ] } @@ -201,8 +202,10 @@ class Filter extends Equatable { // Normal filters are encoded in the following form: // { key: { $: } } json[key!] = {operator: value}; - } else { + } else if (key != null) { json[key!] = value; + } else { + return value as Map; } return json; diff --git a/packages/stream_chat/test/src/models/filter_test.dart b/packages/stream_chat/test/src/models/filter_test.dart index 99c20233..ac026c6c 100644 --- a/packages/stream_chat/test/src/models/filter_test.dart +++ b/packages/stream_chat/test/src/models/filter_test.dart @@ -130,6 +130,14 @@ void main() { expect(filter.operator, operator); }); + test('raw', () { + const value = { + 'test': ['a', 'b'], + }; + const filter = Filter.raw(value: value); + expect(filter.value, value); + }); + group('groupedOperator', () { final filter1 = Filter.equal('testKey', 'testValue'); final filter2 = Filter.in_('testKey', const ['testValue']); @@ -191,6 +199,19 @@ void main() { '{"$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', () { From 5d4b2d33faa3628a2de0fdf91a9423edb450f0af Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Mon, 17 May 2021 13:28:40 +0200 Subject: [PATCH 5/5] add doc comment --- packages/stream_chat/lib/src/models/filter.dart | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/stream_chat/lib/src/models/filter.dart b/packages/stream_chat/lib/src/models/filter.dart index a2f4ce4b..eb3932ff 100644 --- a/packages/stream_chat/lib/src/models/filter.dart +++ b/packages/stream_chat/lib/src/models/filter.dart @@ -1,7 +1,5 @@ // ignore_for_file: non_constant_identifier_names, constant_identifier_names -import 'dart:convert'; - import 'package:equatable/equatable.dart'; const _groupOperators = [ @@ -166,7 +164,15 @@ class Filter extends Equatable { String? key, }) = Filter.__; - /// Creates a custom [Filter] from a raw value + /// 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 value, }) = Filter.__;