From 0f9b867b50cbbca5f2c0c20db57a527b254eea58 Mon Sep 17 00:00:00 2001 From: Dundo7 Date: Fri, 10 Jul 2020 11:34:25 +0200 Subject: [PATCH] Fixed some maintenance issues --- CHANGELOG.md | 7 +++- lib/src/parser.dart | 84 ++++++++++++++++++++++++++++++++------------- pubspec.yaml | 2 +- test/japx_test.dart | 63 ++++++++++++++++++++++++++-------- test/test_data.dart | 48 +++++++++++++------------- 5 files changed, 139 insertions(+), 65 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e45e58d..2ab95a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ ## 1.0.0 -* Initial release with created Japx package. \ No newline at end of file +* Initial release with created Japx package. + +## 1.0.1 + +* Updated package documentation. +* Resolved maintenance issues and suggestions. \ No newline at end of file diff --git a/lib/src/parser.dart b/lib/src/parser.dart index a9bd8a0..0c14b42 100644 --- a/lib/src/parser.dart +++ b/lib/src/parser.dart @@ -19,7 +19,8 @@ class _TypeIdPair { int get hashCode => type.hashCode ^ id.hashCode; @override - bool operator ==(other) => other is _TypeIdPair && type == other.type && id == other.id; + bool operator ==(other) => + other is _TypeIdPair && type == other.type && id == other.id; @override String toString() => '[$type: $id]'; @@ -41,10 +42,14 @@ class Japx { /// - parameter additionalParams: Additional Map to add with `data` to JSON:API object. /// /// - returns: JSON:API object. - static Map encode(Object json, {Map additionalParams}) { + static Map encode(Object json, + {Map additionalParams}) { final params = additionalParams ?? {}; if (json is List) { - final list = json.map((e) => e as Map).map((e) => _encodeAttributesAndRelationships(e)).toList(); + final list = json + .map((e) => e as Map) + .map((e) => _encodeAttributesAndRelationships(e)) + .toList(); params[_data] = list; } if (json is Map) { @@ -59,12 +64,17 @@ class Japx { /// - parameter includeList: The include list for deserializing JSON:API relationships. /// /// - returns: JSON object. - static Map decode(Map jsonApi, {String includeList}) { - return (includeList != null) ? _japxDecodeList(jsonApi, includeList) : _decode(jsonApi); + static Map decode(Map jsonApi, + {String includeList}) { + return (includeList != null) + ? _japxDecodeList(jsonApi, includeList) + : _decode(jsonApi); } - static Map _japxDecodeList(Map jsonApi, String includeList) { - final params = includeList.split(',').map((e) => e.split('.').toList()).toList(); + static Map _japxDecodeList( + Map jsonApi, String includeList) { + final params = + includeList.split(',').map((e) => e.split('.').toList()).toList(); final paramsMap = Map(); for (var lineArray in params) { @@ -83,12 +93,15 @@ class Japx { final dataObjectsArray = _array(jsonApi, _data); final includedObjectsArray = _array(jsonApi, _included); final allObjectsArray = dataObjectsArray + includedObjectsArray; - final allObjects = allObjectsArray.fold(Map<_TypeIdPair, Map>(), (map, element) { + final allObjects = allObjectsArray + .fold(Map<_TypeIdPair, Map>(), (map, element) { map[_TypeIdPair.from(element)] = element; return map; }); - final objects = dataObjectsArray.map((e) => _resolve(e, allObjects, paramsMap)).toList(); + final objects = dataObjectsArray + .map((e) => _resolve(e, allObjects, paramsMap)) + .toList(); final isObject = jsonApi[_data] is List ? false : true; if (isObject && objects.length == 1) { @@ -101,26 +114,38 @@ class Japx { } static _resolve( - Map object, Map<_TypeIdPair, Map> allObjects, Map paramsMap) { - final attributes = (object[_attributes] ?? Map()) as Map; + Map object, + Map<_TypeIdPair, Map> allObjects, + Map paramsMap) { + final attributes = + (object[_attributes] ?? Map()) as Map; attributes[_type] = object[_type]; attributes[_id] = object[_id]; - final relationshipsReferences = (object[_relationships] ?? Map()) as Map; + final relationshipsReferences = (object[_relationships] ?? + Map()) as Map; - final relationships = paramsMap.keys.fold(Map(), (result, relationshipsKey) { + final relationships = + paramsMap.keys.fold(Map(), (result, relationshipsKey) { if (relationshipsReferences[relationshipsKey] == null) { return result; } - final relationship = relationshipsReferences[relationshipsKey] as Map; + final relationship = + relationshipsReferences[relationshipsKey] as Map; final otherObjectsData = _array(relationship, _data); - final otherObjects = - otherObjectsData.map((e) => _TypeIdPair.from(e)).map((e) => allObjects[e]).where((e) => e != null).map((e) { + final otherObjects = otherObjectsData + .map((e) => _TypeIdPair.from(e)) + .map((e) => allObjects[e]) + .where((e) => e != null) + .map((e) { final objectCopy = jsonDecode(jsonEncode(e)); return _resolve( - objectCopy, allObjects, (paramsMap[relationshipsKey] ?? Map()) as Map); + objectCopy, + allObjects, + (paramsMap[relationshipsKey] ?? Map()) + as Map); }).toList(); final isObject = relationship[_data] is List ? false : true; @@ -171,7 +196,8 @@ class Japx { return jsonApi; } - static Map _encodeAttributesAndRelationships(Map json) { + static Map _encodeAttributesAndRelationships( + Map json) { final attributes = Map(); final relationships = Map(); final keys = json.keys.toList(); @@ -181,14 +207,18 @@ class Japx { continue; } if (json[key] is List) { - final array = (json[key] as List).map((e) => e as Map).toList(); + final array = + (json[key] as List).map((e) => e as Map).toList(); final isArrayOfRelationships = _TypeIdPair.from(array.first) != null; if (!isArrayOfRelationships) { attributes[key] = array; json.remove(key); continue; } - final dataArray = array.map((e) => _TypeIdPair.from(e)).map((e) => e.toMap()).toList(); + final dataArray = array + .map((e) => _TypeIdPair.from(e)) + .map((e) => e.toMap()) + .toList(); relationships[key] = {_data: dataArray}; json.remove(key); } @@ -214,7 +244,8 @@ class Japx { return json; } - static void _resolveAttributes(Map<_TypeIdPair, Map> objects) { + static void _resolveAttributes( + Map<_TypeIdPair, Map> objects) { for (Map object in objects.values) { if (object == null) { continue; @@ -227,7 +258,8 @@ class Japx { } } - static void _resolveRelationships(Map<_TypeIdPair, Map> objects) { + static void _resolveRelationships( + Map<_TypeIdPair, Map> objects) { objects.values.forEach((object) { if (object == null) { return; @@ -243,7 +275,10 @@ class Japx { final others = _array(relationshipParams, _data); // Fetch those object from `objects` - final othersObjects = others.map((e) => _TypeIdPair.from(e)).map((e) => objects[e]).toList(); + final othersObjects = others + .map((e) => _TypeIdPair.from(e)) + .map((e) => objects[e]) + .toList(); final isObject = relationshipParams[_data] is List ? false : true; @@ -256,7 +291,8 @@ class Japx { }); } - static List> _array(Map json, String key) { + static List> _array( + Map json, String key) { if (json[key] is List) { final list = json[key] as List; return list.map((e) => e as Map).toList(); diff --git a/pubspec.yaml b/pubspec.yaml index 9543f8e..a53713a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: japx description: Lightweight parser for the JSON:API (http://jsonapi.org/) structure. -version: 1.0.0 +version: 1.0.1 repository: https://github.com/infinum/flutter-plugins-japx homepage: https://github.com/infinum/flutter-plugins-japx diff --git a/test/japx_test.dart b/test/japx_test.dart index e37c020..6c7266a 100644 --- a/test/japx_test.dart +++ b/test/japx_test.dart @@ -6,31 +6,64 @@ import 'test_data.dart'; void main() { TestWidgetsFlutterBinding.ensureInitialized(); - test('Basic decoding', () async => compare(Japx.decode(await decodingSample1()), await resultDecoding1())); - test('Relationship decoding', () async => compare(Japx.decode(await decodingSample2()), await resultDecoding2())); - test('Additional info decoding', () async => compare(Japx.decode(await decodingSample3()), await resultDecoding3())); + test( + 'Basic decoding', + () async => compare( + Japx.decode(await decodingSample1()), await resultDecoding1())); + test( + 'Relationship decoding', + () async => compare( + Japx.decode(await decodingSample2()), await resultDecoding2())); + test( + 'Additional info decoding', + () async => compare( + Japx.decode(await decodingSample3()), await resultDecoding3())); test( 'Recursive decoding', - () async => - compare(Japx.decode(await decodingSample4(), includeList: 'author.article.author'), await resultDecoding4())); + () async => compare( + Japx.decode(await decodingSample4(), + includeList: 'author.article.author'), + await resultDecoding4())); test( 'Relationship no include decoding', - () async => - compare(Japx.decode(await decodingSample5(), includeList: 'author.article.author'), await resultDecoding5())); + () async => compare( + Japx.decode(await decodingSample5(), + includeList: 'author.article.author'), + await resultDecoding5())); test( 'Advanced decoding', () async => compare( - Japx.decode(await decodingSample6(), includeList: 'author,comments.author'), await resultDecoding6())); + Japx.decode(await decodingSample6(), + includeList: 'author,comments.author'), + await resultDecoding6())); test( 'Empty relationship decoding', - () async => compare(Japx.decode(await decodingSample7(), includeList: 'author.categories,author.article.author'), + () async => compare( + Japx.decode(await decodingSample7(), + includeList: 'author.categories,author.article.author'), await resultDecoding7())); - test('Basic encoding', () async => compare(Japx.encode(await encodingSample1()), await resultEncoding1())); - test('Extra params encoding', () async => compare(Japx.encode(await encodingSample2()), await resultEncoding2())); - test('Meta encoding', () async => compare(Japx.encode(await encodingSample3()), await resultEncoding3())); - test('Recursive encoding', () async => compare(Japx.encode(await encodingSample4()), await resultEncoding4())); - test('Simple encoding', () async => compare(Japx.encode(await encodingSample5()), await resultEncoding5())); + test( + 'Basic encoding', + () async => compare( + Japx.encode(await encodingSample1()), await resultEncoding1())); + test( + 'Extra params encoding', + () async => compare( + Japx.encode(await encodingSample2()), await resultEncoding2())); + test( + 'Meta encoding', + () async => compare( + Japx.encode(await encodingSample3()), await resultEncoding3())); + test( + 'Recursive encoding', + () async => compare( + Japx.encode(await encodingSample4()), await resultEncoding4())); + test( + 'Simple encoding', + () async => compare( + Japx.encode(await encodingSample5()), await resultEncoding5())); } -void compare(Map sample, Map result) => expect(sample, result); +void compare(Map sample, Map result) => + expect(sample, result); diff --git a/test/test_data.dart b/test/test_data.dart index 9acf153..5f72409 100644 --- a/test/test_data.dart +++ b/test/test_data.dart @@ -28,31 +28,31 @@ Future> encodingSample4() async => Future> encodingSample5() async => parseJsonFromAssets('packages/japx/assets/encoding/encoding-5.json'); -Future> resultDecoding1() async => - parseJsonFromAssets('packages/japx/assets/result/decoding/result-decoding-1.json'); -Future> resultDecoding2() async => - parseJsonFromAssets('packages/japx/assets/result/decoding/result-decoding-2.json'); -Future> resultDecoding3() async => - parseJsonFromAssets('packages/japx/assets/result/decoding/result-decoding-3.json'); -Future> resultDecoding4() async => - parseJsonFromAssets('packages/japx/assets/result/decoding/result-decoding-4.json'); -Future> resultDecoding5() async => - parseJsonFromAssets('packages/japx/assets/result/decoding/result-decoding-5.json'); -Future> resultDecoding6() async => - parseJsonFromAssets('packages/japx/assets/result/decoding/result-decoding-6.json'); -Future> resultDecoding7() async => - parseJsonFromAssets('packages/japx/assets/result/decoding/result-decoding-7.json'); +Future> resultDecoding1() async => parseJsonFromAssets( + 'packages/japx/assets/result/decoding/result-decoding-1.json'); +Future> resultDecoding2() async => parseJsonFromAssets( + 'packages/japx/assets/result/decoding/result-decoding-2.json'); +Future> resultDecoding3() async => parseJsonFromAssets( + 'packages/japx/assets/result/decoding/result-decoding-3.json'); +Future> resultDecoding4() async => parseJsonFromAssets( + 'packages/japx/assets/result/decoding/result-decoding-4.json'); +Future> resultDecoding5() async => parseJsonFromAssets( + 'packages/japx/assets/result/decoding/result-decoding-5.json'); +Future> resultDecoding6() async => parseJsonFromAssets( + 'packages/japx/assets/result/decoding/result-decoding-6.json'); +Future> resultDecoding7() async => parseJsonFromAssets( + 'packages/japx/assets/result/decoding/result-decoding-7.json'); -Future> resultEncoding1() async => - parseJsonFromAssets('packages/japx/assets/result/encoding/result-encoding-1.json'); -Future> resultEncoding2() async => - parseJsonFromAssets('packages/japx/assets/result/encoding/result-encoding-2.json'); -Future> resultEncoding3() async => - parseJsonFromAssets('packages/japx/assets/result/encoding/result-encoding-3.json'); -Future> resultEncoding4() async => - parseJsonFromAssets('packages/japx/assets/result/encoding/result-encoding-4.json'); -Future> resultEncoding5() async => - parseJsonFromAssets('packages/japx/assets/result/encoding/result-encoding-5.json'); +Future> resultEncoding1() async => parseJsonFromAssets( + 'packages/japx/assets/result/encoding/result-encoding-1.json'); +Future> resultEncoding2() async => parseJsonFromAssets( + 'packages/japx/assets/result/encoding/result-encoding-2.json'); +Future> resultEncoding3() async => parseJsonFromAssets( + 'packages/japx/assets/result/encoding/result-encoding-3.json'); +Future> resultEncoding4() async => parseJsonFromAssets( + 'packages/japx/assets/result/encoding/result-encoding-4.json'); +Future> resultEncoding5() async => parseJsonFromAssets( + 'packages/japx/assets/result/encoding/result-encoding-5.json'); Future> parseJsonFromAssets(String assetsPath) async => jsonDecode(await rootBundle.loadString(assetsPath));