Skip to content

Commit

Permalink
Merge pull request #6 from infinum/maintenance-fixes
Browse files Browse the repository at this point in the history
Fixed some maintenance issues
  • Loading branch information
itsJoKr authored Jul 10, 2020
2 parents 061d4ee + 0f9b867 commit 5f343c0
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 65 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.0.0

* Initial release with created Japx package.
* Initial release with created Japx package.

## 1.0.1

* Updated package documentation.
* Resolved maintenance issues and suggestions.
84 changes: 60 additions & 24 deletions lib/src/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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]';
Expand All @@ -41,10 +42,14 @@ class Japx {
/// - parameter additionalParams: Additional Map<String: dynamic> to add with `data` to JSON:API object.
///
/// - returns: JSON:API object.
static Map<String, dynamic> encode(Object json, {Map<String, dynamic> additionalParams}) {
static Map<String, dynamic> encode(Object json,
{Map<String, dynamic> additionalParams}) {
final params = additionalParams ?? {};
if (json is List) {
final list = json.map((e) => e as Map<String, dynamic>).map((e) => _encodeAttributesAndRelationships(e)).toList();
final list = json
.map((e) => e as Map<String, dynamic>)
.map((e) => _encodeAttributesAndRelationships(e))
.toList();
params[_data] = list;
}
if (json is Map<String, dynamic>) {
Expand All @@ -59,12 +64,17 @@ class Japx {
/// - parameter includeList: The include list for deserializing JSON:API relationships.
///
/// - returns: JSON object.
static Map<String, dynamic> decode(Map<String, dynamic> jsonApi, {String includeList}) {
return (includeList != null) ? _japxDecodeList(jsonApi, includeList) : _decode(jsonApi);
static Map<String, dynamic> decode(Map<String, dynamic> jsonApi,
{String includeList}) {
return (includeList != null)
? _japxDecodeList(jsonApi, includeList)
: _decode(jsonApi);
}

static Map<String, dynamic> _japxDecodeList(Map<String, dynamic> jsonApi, String includeList) {
final params = includeList.split(',').map((e) => e.split('.').toList()).toList();
static Map<String, dynamic> _japxDecodeList(
Map<String, dynamic> jsonApi, String includeList) {
final params =
includeList.split(',').map((e) => e.split('.').toList()).toList();

final paramsMap = Map<String, dynamic>();
for (var lineArray in params) {
Expand All @@ -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<String, dynamic>>(), (map, element) {
final allObjects = allObjectsArray
.fold(Map<_TypeIdPair, Map<String, dynamic>>(), (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) {
Expand All @@ -101,26 +114,38 @@ class Japx {
}

static _resolve(
Map<String, dynamic> object, Map<_TypeIdPair, Map<String, dynamic>> allObjects, Map<String, dynamic> paramsMap) {
final attributes = (object[_attributes] ?? Map<String, dynamic>()) as Map<String, dynamic>;
Map<String, dynamic> object,
Map<_TypeIdPair, Map<String, dynamic>> allObjects,
Map<String, dynamic> paramsMap) {
final attributes =
(object[_attributes] ?? Map<String, dynamic>()) as Map<String, dynamic>;
attributes[_type] = object[_type];
attributes[_id] = object[_id];

final relationshipsReferences = (object[_relationships] ?? Map<String, dynamic>()) as Map<String, dynamic>;
final relationshipsReferences = (object[_relationships] ??
Map<String, dynamic>()) as Map<String, dynamic>;

final relationships = paramsMap.keys.fold(Map<String, dynamic>(), (result, relationshipsKey) {
final relationships =
paramsMap.keys.fold(Map<String, dynamic>(), (result, relationshipsKey) {
if (relationshipsReferences[relationshipsKey] == null) {
return result;
}
final relationship = relationshipsReferences[relationshipsKey] as Map<String, dynamic>;
final relationship =
relationshipsReferences[relationshipsKey] as Map<String, dynamic>;

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<String, dynamic>()) as Map<String, dynamic>);
objectCopy,
allObjects,
(paramsMap[relationshipsKey] ?? Map<String, dynamic>())
as Map<String, dynamic>);
}).toList();

final isObject = relationship[_data] is List ? false : true;
Expand Down Expand Up @@ -171,7 +196,8 @@ class Japx {
return jsonApi;
}

static Map<String, dynamic> _encodeAttributesAndRelationships(Map<String, dynamic> json) {
static Map<String, dynamic> _encodeAttributesAndRelationships(
Map<String, dynamic> json) {
final attributes = Map<String, dynamic>();
final relationships = Map<String, dynamic>();
final keys = json.keys.toList();
Expand All @@ -181,14 +207,18 @@ class Japx {
continue;
}
if (json[key] is List) {
final array = (json[key] as List).map((e) => e as Map<String, dynamic>).toList();
final array =
(json[key] as List).map((e) => e as Map<String, dynamic>).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);
}
Expand All @@ -214,7 +244,8 @@ class Japx {
return json;
}

static void _resolveAttributes(Map<_TypeIdPair, Map<String, dynamic>> objects) {
static void _resolveAttributes(
Map<_TypeIdPair, Map<String, dynamic>> objects) {
for (Map<String, dynamic> object in objects.values) {
if (object == null) {
continue;
Expand All @@ -227,7 +258,8 @@ class Japx {
}
}

static void _resolveRelationships(Map<_TypeIdPair, Map<String, dynamic>> objects) {
static void _resolveRelationships(
Map<_TypeIdPair, Map<String, dynamic>> objects) {
objects.values.forEach((object) {
if (object == null) {
return;
Expand All @@ -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;

Expand All @@ -256,7 +291,8 @@ class Japx {
});
}

static List<Map<String, dynamic>> _array(Map<String, dynamic> json, String key) {
static List<Map<String, dynamic>> _array(
Map<String, dynamic> json, String key) {
if (json[key] is List) {
final list = json[key] as List;
return list.map((e) => e as Map<String, dynamic>).toList();
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand Down
63 changes: 48 additions & 15 deletions test/japx_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, dynamic> sample, Map<String, dynamic> result) => expect(sample, result);
void compare(Map<String, dynamic> sample, Map<String, dynamic> result) =>
expect(sample, result);
48 changes: 24 additions & 24 deletions test/test_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,31 @@ Future<Map<String, dynamic>> encodingSample4() async =>
Future<Map<String, dynamic>> encodingSample5() async =>
parseJsonFromAssets('packages/japx/assets/encoding/encoding-5.json');

Future<Map<String, dynamic>> resultDecoding1() async =>
parseJsonFromAssets('packages/japx/assets/result/decoding/result-decoding-1.json');
Future<Map<String, dynamic>> resultDecoding2() async =>
parseJsonFromAssets('packages/japx/assets/result/decoding/result-decoding-2.json');
Future<Map<String, dynamic>> resultDecoding3() async =>
parseJsonFromAssets('packages/japx/assets/result/decoding/result-decoding-3.json');
Future<Map<String, dynamic>> resultDecoding4() async =>
parseJsonFromAssets('packages/japx/assets/result/decoding/result-decoding-4.json');
Future<Map<String, dynamic>> resultDecoding5() async =>
parseJsonFromAssets('packages/japx/assets/result/decoding/result-decoding-5.json');
Future<Map<String, dynamic>> resultDecoding6() async =>
parseJsonFromAssets('packages/japx/assets/result/decoding/result-decoding-6.json');
Future<Map<String, dynamic>> resultDecoding7() async =>
parseJsonFromAssets('packages/japx/assets/result/decoding/result-decoding-7.json');
Future<Map<String, dynamic>> resultDecoding1() async => parseJsonFromAssets(
'packages/japx/assets/result/decoding/result-decoding-1.json');
Future<Map<String, dynamic>> resultDecoding2() async => parseJsonFromAssets(
'packages/japx/assets/result/decoding/result-decoding-2.json');
Future<Map<String, dynamic>> resultDecoding3() async => parseJsonFromAssets(
'packages/japx/assets/result/decoding/result-decoding-3.json');
Future<Map<String, dynamic>> resultDecoding4() async => parseJsonFromAssets(
'packages/japx/assets/result/decoding/result-decoding-4.json');
Future<Map<String, dynamic>> resultDecoding5() async => parseJsonFromAssets(
'packages/japx/assets/result/decoding/result-decoding-5.json');
Future<Map<String, dynamic>> resultDecoding6() async => parseJsonFromAssets(
'packages/japx/assets/result/decoding/result-decoding-6.json');
Future<Map<String, dynamic>> resultDecoding7() async => parseJsonFromAssets(
'packages/japx/assets/result/decoding/result-decoding-7.json');

Future<Map<String, dynamic>> resultEncoding1() async =>
parseJsonFromAssets('packages/japx/assets/result/encoding/result-encoding-1.json');
Future<Map<String, dynamic>> resultEncoding2() async =>
parseJsonFromAssets('packages/japx/assets/result/encoding/result-encoding-2.json');
Future<Map<String, dynamic>> resultEncoding3() async =>
parseJsonFromAssets('packages/japx/assets/result/encoding/result-encoding-3.json');
Future<Map<String, dynamic>> resultEncoding4() async =>
parseJsonFromAssets('packages/japx/assets/result/encoding/result-encoding-4.json');
Future<Map<String, dynamic>> resultEncoding5() async =>
parseJsonFromAssets('packages/japx/assets/result/encoding/result-encoding-5.json');
Future<Map<String, dynamic>> resultEncoding1() async => parseJsonFromAssets(
'packages/japx/assets/result/encoding/result-encoding-1.json');
Future<Map<String, dynamic>> resultEncoding2() async => parseJsonFromAssets(
'packages/japx/assets/result/encoding/result-encoding-2.json');
Future<Map<String, dynamic>> resultEncoding3() async => parseJsonFromAssets(
'packages/japx/assets/result/encoding/result-encoding-3.json');
Future<Map<String, dynamic>> resultEncoding4() async => parseJsonFromAssets(
'packages/japx/assets/result/encoding/result-encoding-4.json');
Future<Map<String, dynamic>> resultEncoding5() async => parseJsonFromAssets(
'packages/japx/assets/result/encoding/result-encoding-5.json');

Future<Map<String, dynamic>> parseJsonFromAssets(String assetsPath) async =>
jsonDecode(await rootBundle.loadString(assetsPath));

0 comments on commit 5f343c0

Please sign in to comment.