Skip to content

Commit

Permalink
Version 0.2.1
Browse files Browse the repository at this point in the history
Merge pull request #4 from Djcharles26/work
  • Loading branch information
Djcharles26 authored May 17, 2022
2 parents ea78cf8 + 8546f23 commit d402ee5
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 23 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@
## 0.2.0
- Added json list field method for handling lists
- Added json class field method for handling custom classes
- Updated tests with new methods
- Updated tests with new methods

## 0.2.1
- Updated how null on exception class is executed
- Added tests for null classes and assertion errors
5 changes: 2 additions & 3 deletions CHANGELOG_LATEST.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
- Added json list field method for handling lists
- Added json class field method for handling custom classes
- Updated tests with new methods
- Updated how null on exception class is executed
- Added tests for null classes and assertion errors
42 changes: 25 additions & 17 deletions lib/body_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,25 +112,33 @@ dynamic jsonClassField<T> (

T? retval;

dynamic body = jsonField<dynamic> (json, field, nullable: nullable);
try {

dynamic body = jsonField<dynamic> (json, field, nullable: nullable);

if (body != null) {
try {
retval = fromJson (body);
} on BodyException {
rethrow;
} on NoSuchMethodError catch (_) {
throw BodyException(
type: BodyExceptionType.isNull,
fieldName: field.join ("_"),
failedType: T,
currentType: retval.runtimeType
);
} catch (error) {
throw BodyException(
type: BodyExceptionType.undefined,
fieldName: field.join ("_"),
);
}
}

if (body != null) {
try {
retval = fromJson (body);
} on BodyException {
} on BodyException {
if (!nullOnException) {
rethrow;
} on NoSuchMethodError catch (_) {
throw BodyException(
type: BodyExceptionType.isNull,
fieldName: field.join ("_"),
failedType: T,
currentType: retval.runtimeType
);
} catch (error) {
throw BodyException(
type: BodyExceptionType.undefined,
fieldName: field.join ("_"),
);
}
}

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.0
version: 0.2.1
homepage: https://github.com/Djcharles26/
repository: https://github.com/Djcharles26/http_utils.git

Expand Down
10 changes: 10 additions & 0 deletions test/body_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ void main() {
"\$oid": "123"
},
"stringList": ["Hello", "World"],
"badClass": {
"_id": {
"\$oid": "456"
},
"integer": 4,
"name": null,
"lastname": "Marin"
},
"organization": null,
"integer": 15,
"number": 0.45,
Expand Down Expand Up @@ -92,11 +100,13 @@ 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, ["object"], (item) => TestClass.fromJson (item)), null);
});

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> ()));
});

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.0
0.2.1

0 comments on commit d402ee5

Please sign in to comment.