Skip to content

Commit

Permalink
Version 0.2.4
Browse files Browse the repository at this point in the history
Merge pull request #7 from Djcharles26/work
  • Loading branch information
Djcharles26 authored May 19, 2022
2 parents 995719c + 4bd14df commit b19460b
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 12 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@
- Fixed missing default value return in json class field

## 0.2.3
- Added skip exceptions value in json list field
- Added skip exceptions value in json list field

## 0.2.4
- Small changes applied on json class field to handle correctly null item
- Refactor nullOnException for skipException
3 changes: 2 additions & 1 deletion CHANGELOG_LATEST.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- Added skip exceptions value in json list field
- Small changes applied on json class field to handle correctly null item
- Refactor nullOnException for skipException
14 changes: 9 additions & 5 deletions lib/body_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,14 @@ dynamic jsonListField<T> (
}

dynamic jsonClassField<T> (
dynamic json, List<String> field, T Function (dynamic) fromJson,
{bool nullable = true, bool nullOnException = false, T? defaultValue}
dynamic json, List<String> field, T? Function (dynamic) fromJson,
{bool nullable = true, bool skipException = false, T? defaultValue}
) {
assert (
(
nullable && ( nullOnException || !nullOnException )
nullable && ( skipException || !skipException )
)
|| (!nullable && !nullOnException)
|| (!nullable && !skipException)
);

T? retval;
Expand All @@ -130,6 +130,10 @@ dynamic jsonClassField<T> (
if (body != null) {
try {
retval = fromJson (body);

if (retval == null && !nullable) {
throw NoSuchMethodError;
}
} on BodyException {
rethrow;
} on NoSuchMethodError catch (_) {
Expand All @@ -148,7 +152,7 @@ dynamic jsonClassField<T> (
}

} on BodyException {
if (!nullOnException) {
if (!skipException) {
rethrow;
}
}
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: http_request_utils
description: Http requests utils
version: 0.2.3
version: 0.2.4
homepage: https://github.com/Djcharles26/
repository: https://github.com/Djcharles26/http_utils.git

Expand Down
6 changes: 3 additions & 3 deletions test/body_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,19 @@ void main() {

test ('Check for class json fields', () {
expect (jsonClassField<TestClass>(json, [], TestClass.fromJson, nullable: false), isA<TestClass> ());
expect (jsonClassField<TestClass>(json, ["badClass"], TestClass.fromJson, nullOnException: true, nullable: true), null);
expect (jsonClassField<TestClass>(json, ["badClass"], TestClass.fromJson, skipException: true, nullable: true), null);
expect (jsonClassField<TestClass>(json, ["object"], (item) => TestClass.fromJson (item)), null);
expect (
jsonClassField<TestClass>(
json, ["object"], TestClass.fromJson,
nullable: true, nullOnException: true, defaultValue: TestClass.empty ()
nullable: true, skipException: true, defaultValue: TestClass.empty ()
), isA<TestClass>()
);
});

test ('Check for class json fields with incorrect field type', () {
expect (() => jsonClassField<TestListClass> (json, [], TestListClass.fromJson, nullable: false), throwsA(isA<BodyException> ()));
expect (() => jsonClassField<TestClass> (json, [], TestClass.fromJson, nullOnException: true, nullable: false), throwsA (isA<AssertionError> ()));
expect (() => jsonClassField<TestClass> (json, [], TestClass.fromJson, skipException: true, nullable: false), throwsA (isA<AssertionError> ()));
});

test ("Check for exceptions with incorrect file types", () {
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.3
0.2.4

0 comments on commit b19460b

Please sign in to comment.