From 9942ab9c52578f920c685655da4a3c00047473c6 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 22 Jan 2024 09:22:58 +0000 Subject: [PATCH 1/4] dart/flutter implementation for updated query - returns json - adds OR and Contains query --- templates/dart/lib/query.dart.twig | 105 +++++++++++++++++------------ 1 file changed, 63 insertions(+), 42 deletions(-) diff --git a/templates/dart/lib/query.dart.twig b/templates/dart/lib/query.dart.twig index a5763d79d..46a5836b8 100644 --- a/templates/dart/lib/query.dart.twig +++ b/templates/dart/lib/query.dart.twig @@ -1,99 +1,120 @@ part of {{ language.params.packageName }}; +import 'dart:convert'; + /// Helper class to generate query strings. class Query { - Query._(); + final String method; + final String? attribute; + final dynamic values; + + Query._(this.method, [this.attribute = null, this.values = null]); + + Map toJson() { + final map = { + 'method': method, + }; + + if(values != null) { + map['values'] = values is List ? values : [values]; + } + + if(attribute != null) { + map['attribute'] = attribute; + } + + return map; + } + + @override + String toString() => jsonEncode(toJson()); /// Filter resources where [attribute] is equal to [value]. /// /// [value] can be a single value or a list. If a list is used /// the query will return resources where [attribute] is equal /// to any of the values in the list. - static String equal(String attribute, dynamic value) => - _addQuery(attribute, 'equal', value); + static Query equal(String attribute, dynamic value) => + Query._('equal', attribute, value); /// Filter resources where [attribute] is not equal to [value]. - /// - /// [value] can be a single value or a list. If a list is used - /// the query will return resources where [attribute] is equal - /// to any of the values in the list. - static String notEqual(String attribute, dynamic value) => - _addQuery(attribute, 'notEqual', value); + static Query notEqual(String attribute, dynamic value) => + Query._('notEqual', attribute, [value]); /// Filter resources where [attribute] is less than [value]. - static String lessThan(String attribute, dynamic value) => - _addQuery(attribute, 'lessThan', value); + static Query lessThan(String attribute, dynamic value) => + Query._('lessThan', attribute, value); /// Filter resources where [attribute] is less than or equal to [value]. - static String lessThanEqual(String attribute, dynamic value) => - _addQuery(attribute, 'lessThanEqual', value); + static Query lessThanEqual(String attribute, dynamic value) => + Query._('lessThanEqual', attribute, value); /// Filter resources where [attribute] is greater than [value]. - static String greaterThan(String attribute, dynamic value) => - _addQuery(attribute, 'greaterThan', value); + static Query greaterThan(String attribute, dynamic value) => + Query._('greaterThan', attribute, value); /// Filter resources where [attribute] is greater than or equal to [value]. - static String greaterThanEqual(String attribute, dynamic value) => - _addQuery(attribute, 'greaterThanEqual', value); + static Query greaterThanEqual(String attribute, dynamic value) => + Query._('greaterThanEqual', attribute, value); /// Filter resources where by searching [attribute] for [value]. - static String search(String attribute, String value) => - _addQuery(attribute, 'search', value); + static Query search(String attribute, String value) => + Query._('search', attribute, value); /// Filter resources where [attribute] is null. - static String isNull(String attribute) => 'isNull("$attribute")'; + static Query isNull(String attribute) => Query._('isNull', attribute); /// Filter resources where [attribute] is not null. - static String isNotNull(String attribute) => 'isNotNull("$attribute")'; + static Query isNotNull(String attribute) => Query._('isNotNull', attribute); /// Filter resources where [attribute] is between [start] and [end] (inclusive). - static String between(String attribute, dynamic start, dynamic end) => - 'between("$attribute", ${_parseValues(start)}, ${_parseValues(end)})'; + static Query between(String attribute, dynamic start, dynamic end) => + Query._('between', attribute, [start, end]); /// Filter resources where [attribute] starts with [value]. - static String startsWith(String attribute, String value) => - _addQuery(attribute, 'startsWith', value); + static Query startsWith(String attribute, String value) => + Query._('startsWith', attribute, value); /// Filter resources where [attribute] ends with [value]. - static String endsWith(String attribute, String value) => - _addQuery(attribute, 'endsWith', value); + static Query endsWith(String attribute, String value) => + Query._('endsWith', attribute, value); + + /// Filter resouorces where [attribute] contains [value] + /// [value] can be a single value or a list. + static Query contains(String attribute, dynamic value) => + Query._('contains', attribute, value); + + static Query or(List queries) => Query._('or', null, queries); /// Specify which attributes should be returned by the API call. - static String select(List attributes) => - 'select([${attributes.map((attr) => "\"$attr\"").join(",")}])'; + static Query select(List attributes) => + Query._('select', null, attributes); /// Sort results by [attribute] ascending. - static String orderAsc(String attribute) => 'orderAsc("$attribute")'; + static Query orderAsc(String attribute) => Query._('orderAsc', attribute); /// Sort results by [attribute] descending. - static String orderDesc(String attribute) => 'orderDesc("$attribute")'; + static Query orderDesc(String attribute) => Query._('orderDesc', attribute); /// Return results before [id]. /// /// Refer to the [Cursor Based Pagination]({{sdk.url}}/docs/pagination#cursor-pagination) /// docs for more information. - static String cursorBefore(String id) => 'cursorBefore("$id")'; + static Query cursorBefore(String id) => Query._('cursorBefore', null, id); /// Return results after [id]. /// /// Refer to the [Cursor Based Pagination]({{sdk.url}}/docs/pagination#cursor-pagination) /// docs for more information. - static String cursorAfter(String id) => 'cursorAfter("$id")'; + static Query cursorAfter(String id) => Query._('cursorAfter', null, id); /// Return only [limit] results. - static String limit(int limit) => 'limit($limit)'; + static Query limit(int limit) => Query._('limit', null, limit); /// Return results from [offset]. /// /// Refer to the [Offset Pagination]({{sdk.url}}/docs/pagination#offset-pagination) /// docs for more information. - static String offset(int offset) => 'offset($offset)'; - - static String _addQuery(String attribute, String method, dynamic value) => (value - is List) - ? '$method("$attribute", [${value.map((item) => _parseValues(item)).join(",")}])' - : '$method("$attribute", [${_parseValues(value)}])'; + static Query offset(int offset) => Query._('offset', null, offset); - static String _parseValues(dynamic value) => - (value is String) ? '"$value"' : '$value'; } \ No newline at end of file From ad6c2206b23db7e824574674b4f26a322d602e9b Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 22 Jan 2024 09:58:19 +0000 Subject: [PATCH 2/4] update query test --- templates/dart/test/query_test.dart.twig | 165 +++++++++++++---------- 1 file changed, 92 insertions(+), 73 deletions(-) diff --git a/templates/dart/test/query_test.dart.twig b/templates/dart/test/query_test.dart.twig index 9ed2f12e4..cbe838079 100644 --- a/templates/dart/test/query_test.dart.twig +++ b/templates/dart/test/query_test.dart.twig @@ -23,42 +23,42 @@ void main() { BasicFilterQueryTest( description: 'with a string', value: 's', - expectedValues: '["s"]', + expectedValues: ["s"], ), BasicFilterQueryTest( description: 'with an integer', value: 1, - expectedValues: '[1]', + expectedValues: [1], ), BasicFilterQueryTest( description: 'with a double', value: 1.2, - expectedValues: '[1.2]', + expectedValues: [1.2], ), BasicFilterQueryTest( description: 'with a whole number double', value: 1.0, - expectedValues: '[1.0]', + expectedValues: [1.0], ), BasicFilterQueryTest( description: 'with a bool', value: false, - expectedValues: '[false]', + expectedValues: [false], ), BasicFilterQueryTest( description: 'with a list', value: ['a', 'b', 'c'], - expectedValues: '["a","b","c"]', + expectedValues: ["a","b","c"], ), ]; group('equal()', () { for (var t in tests) { test(t.description, () { - expect( - Query.equal('attr', t.value), - 'equal("attr", ${t.expectedValues})', - ); + final query = Query.equal('attr', t.value).toJson(); + expect(query['attribute'], 'attr'); + expect(query['values'], t.expectedValues); + expect(query['method'], 'equal'); }); } }); @@ -66,10 +66,10 @@ void main() { group('notEqual()', () { for (var t in tests) { test(t.description, () { - expect( - Query.notEqual('attr', t.value), - 'notEqual("attr", ${t.expectedValues})', - ); + final query = Query.notEqual('attr', t.value).toJson(); + expect(query['attribute'], 'attr'); + expect(query['values'], t.expectedValues); + expect(query['method'], 'notEqual'); }); } }); @@ -77,10 +77,10 @@ void main() { group('lessThan()', () { for (var t in tests) { test(t.description, () { - expect( - Query.lessThan('attr', t.value), - 'lessThan("attr", ${t.expectedValues})', - ); + final query = Query.lessThan('attr', t.value).toJson(); + expect(query['attribute'], 'attr'); + expect(query['values'], t.expectedValues); + expect(query['method'], 'lessThan'); }); } }); @@ -88,10 +88,10 @@ void main() { group('lessThanEqual()', () { for (var t in tests) { test(t.description, () { - expect( - Query.lessThanEqual('attr', t.value), - 'lessThanEqual("attr", ${t.expectedValues})', - ); + final query = Query.lessThanEqual('attr', t.value).toJson(); + expect(query['attribute'], 'attr'); + expect(query['values'], t.expectedValues); + expect(query['method'], 'lessThanEqual'); }); } }); @@ -99,10 +99,10 @@ void main() { group('greaterThan()', () { for (var t in tests) { test(t.description, () { - expect( - Query.greaterThan('attr', t.value), - 'greaterThan("attr", ${t.expectedValues})', - ); + final query = Query.greaterThan('attr', t.value).toJson(); + expect(query['attribute'], 'attr'); + expect(query['values'], t.expectedValues); + expect(query['method'], 'greaterThan'); }); } }); @@ -110,87 +110,106 @@ void main() { group('greaterThanEqual()', () { for (var t in tests) { test(t.description, () { - expect( - Query.greaterThanEqual('attr', t.value), - 'greaterThanEqual("attr", ${t.expectedValues})', - ); + final query = Query.greaterThanEqual('attr', t.value).toJson(); + expect(query['attribute'], 'attr'); + expect(query['values'], t.expectedValues); + expect(query['method'], 'greaterThanEqual'); }); } }); }); - group('search()', () { - test('returns search', () { - expect(Query.search('attr', 'keyword1 keyword2'), 'search("attr", ["keyword1 keyword2"])'); - }); + test('returns search', () { + final query = Query.search('attr', 'keyword1 keyword2').toJson(); + expect(query['attribute'], 'attr'); + expect(query['values'], ['keyword1 keyword2']); + expect(query['method'], 'search'); }); - group('isNull()', () { - test('returns isNull', () { - expect(Query.isNull('attr'), 'isNull("attr")'); - }); + test('returns isNull', () { + final query = Query.isNull('attr').toJson(); + expect(query['attribute'], 'attr'); + expect(query['values'], null); + expect(query['method'], 'isNull'); }); - group('isNotNull()', () { - test('returns isNotNull', () { - expect(Query.isNotNull('attr'), 'isNotNull("attr")'); - }); + test('returns isNotNull', () { + final query = Query.isNotNull('attr', 'keyword1 keyword2').toJson(); + expect(query['attribute'], 'attr'); + expect(query['values'], null); + expect(query['method'], 'isNotNull'); }); group('between()', () { test('with integers', () { - expect(Query.between('attr', 1, 2), 'between("attr", [1,2])'); + final query = Query.between('attr', 1, 2).toJson(); + expect(query['attribute'], 'attr'); + expect(query['values'], [1, 2]); + expect(query['method'], 'between'); }); test('with doubles', () { - expect(Query.between('attr', 1.0, 2.0), 'between("attr", [1.0,2.0])'); + final query = Query.between('attr', 1.0, 2.0).toJson(); + expect(query['attribute'], 'attr'); + expect(query['values'], [1.0, 2.0]); + expect(query['method'], 'between'); }); test('with strings', () { - expect(Query.between('attr', "a", "z"), 'between("attr", ["a","z"])'); + final query = Query.between('attr', 'a', 'z').toJson(); + expect(query['attribute'], 'attr'); + expect(query['values'], ['a', 'z']); + expect(query['method'], 'between'); }); }); - group('select()', () { - test('returns select', () { - expect(Query.select(['attr1', 'attr2']), 'select(["attr1","attr2"])'); - }); + test('returns select', () { + final query = Query.select(['attr1', 'attr2']).toJson(); + expect(query['attribute'], null); + expect(query['values'], ['attr1', 'attr2']); + expect(query['method'], 'select'); }); - group('orderAsc()', () { - test('returns orderAsc', () { - expect(Query.orderAsc('attr'), 'orderAsc("attr")'); - }); + test('returns orderAsc', () { + final query = Query.orderAsc('attr').toJson(); + expect(query['attribute'], 'attr'); + expect(query['values'], null); + expect(query['method'], 'orderAsc'); }); - group('orderDesc()', () { - test('returns orderDesc', () { - expect(Query.orderDesc('attr'), 'orderDesc("attr")'); - }); + test('returns orderDesc', () { + final query = Query.orderDesc('attr').toJson(); + expect(query['attribute'], 'attr'); + expect(query['values'], null); + expect(query['method'], 'orderDesc'); }); - group('cursorBefore()', () { - test('returns cursorBefore', () { - expect(Query.cursorBefore(ID.custom('custom')), 'cursorBefore("custom")'); - }); + test('returns cursorBefore', () { + final query = Query.cursorBefore('custom').toJson(); + expect(query['attribute'], null); + expect(query['values'], 'custom'); + expect(query['method'], 'cursorBefore'); }); - group('cursorAfter()', () { - test('returns cursorAfter', () { - expect(Query.cursorAfter(ID.custom('custom')), 'cursorAfter("custom")'); - }); + test('returns cursorAfter', () { + final query = Query.cursorAfter('custom').toJson(); + expect(query['attribute'], null); + expect(query['values'], 'custom'); + expect(query['method'], 'cursorAfter'); }); - group('limit()', () { - test('returns limit', () { - expect(Query.limit(1), 'limit(1)'); - }); + test('returns limit', () { + final query = Query.limit(1).toJson(); + expect(query['attribute'], null); + expect(query['values'], 1); + expect(query['method'], 'limit'); }); - group('offset()', () { - test('returns offset', () { - expect(Query.offset(1), 'offset(1)'); - }); + test('returns offset', () { + final query = Query.offset(1).toJson(); + expect(query['attribute'], null); + expect(query['values'], 1); + expect(query['method'], 'offset'); }); } From 833dff525de5047b8c9c94bf61a2c07ab7cf058d Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 22 Jan 2024 10:03:13 +0000 Subject: [PATCH 3/4] rearrange attribute --- templates/dart/lib/query.dart.twig | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/templates/dart/lib/query.dart.twig b/templates/dart/lib/query.dart.twig index 46a5836b8..78e3ef229 100644 --- a/templates/dart/lib/query.dart.twig +++ b/templates/dart/lib/query.dart.twig @@ -14,14 +14,14 @@ class Query { final map = { 'method': method, }; - - if(values != null) { - map['values'] = values is List ? values : [values]; - } if(attribute != null) { map['attribute'] = attribute; } + + if(values != null) { + map['values'] = values is List ? values : [values]; + } return map; } From 1c6b856aa9f3b961d276d9bba4d026688f814016 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 22 Jan 2024 10:05:37 +0000 Subject: [PATCH 4/4] fix tests --- tests/Base.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Base.php b/tests/Base.php index 79c687d3a..d27436e8f 100644 --- a/tests/Base.php +++ b/tests/Base.php @@ -81,12 +81,12 @@ abstract class Base extends TestCase '{ "method": "startsWith", "attribute": "name", "values": ["Ann"]}', '{ "method": "endsWith", "attribute": "name", "values": ["nne"]}', '{ "method": "select", "values": [["name", "age"]]}', - '{ "method": "orderAsc", "attribute": "title", "values": []}', - '{ "method": "orderDesc", "attribute": "title", "values": []}', + '{ "method": "orderAsc", "attribute": "title"}', + '{ "method": "orderDesc", "attribute": "title"}', '{ "method": "cursorAfter", "values": ["my_movie_id"]}', '{ "method": "cursorBefore", "values": ["my_movie_id"]}', - '{ "method": "limit", "attribute": "", "values": [50]}', - '{ "method": "offset", "attribute": "", "values": [20]}', + '{ "method": "limit", "values": [50]}', + '{ "method": "offset", "values": [20]}', ]; protected const PERMISSION_HELPER_RESPONSES = [