-
Notifications
You must be signed in to change notification settings - Fork 420
feat: implement first version of sealed class serialization #1483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
O-Hannonen
wants to merge
18
commits into
google:master
Choose a base branch
from
O-Hannonen:feat/1342-implement-sealed-class-serialization
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 15 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
e303f17
feat: implement first version of sealed class serialization
O-Hannonen 1f12cd4
fix: add assertions for sealed classes
O-Hannonen 6bf34e8
Fix exception name
kevmoo 4b9e805
fix: review fixes
O-Hannonen 4ca71f7
fix: fix existing tests
O-Hannonen 9acdce5
feat: throw on invalid config and add support for complex sealed classes
O-Hannonen 7ae5d44
fix: consistency improvements and more invalid config validations
O-Hannonen 5a87138
test: add tests for new features
O-Hannonen c77bf7c
test: add integration tests
O-Hannonen 3b22672
test: add integration tests and refine examples
O-Hannonen 30847d5
docs: bump version number and update documentation
O-Hannonen 2c7354d
fix: own review fixes
O-Hannonen 3e79e24
fix: add wip suffix to json_annotation version number
O-Hannonen b62058d
fix: fix formatting for new tests
O-Hannonen 55a821b
fix: fixes after rebase
O-Hannonen 199b280
chore: update version constraint syntax
O-Hannonen 92f8499
fix: review fixes
O-Hannonen 21972ed
fix: review fixes
O-Hannonen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import 'package:json_annotation/json_annotation.dart'; | ||
|
||
part 'complex_sealed_class_examples.g.dart'; | ||
|
||
@JsonSerializable( | ||
unionDiscriminator: 'organization', | ||
) | ||
sealed class Organization { | ||
final String name; | ||
|
||
Organization({required this.name}); | ||
|
||
factory Organization.fromJson(Map<String, dynamic> json) => | ||
_$OrganizationFromJson(json); | ||
|
||
Map<String, dynamic> toJson() => _$OrganizationToJson(this); | ||
} | ||
|
||
@JsonSerializable( | ||
unionDiscriminator: 'department', | ||
) | ||
sealed class Department extends Organization { | ||
final String departmentHead; | ||
|
||
Department({ | ||
required this.departmentHead, | ||
required super.name, | ||
}); | ||
|
||
factory Department.fromJson(Map<String, dynamic> json) => | ||
_$DepartmentFromJson(json); | ||
} | ||
|
||
@JsonSerializable() | ||
class Team extends Department { | ||
final String teamLead; | ||
|
||
Team({ | ||
required this.teamLead, | ||
required super.departmentHead, | ||
required super.name, | ||
}); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import 'package:json_annotation/json_annotation.dart'; | ||
|
||
part 'sealed_class_example.g.dart'; | ||
|
||
@JsonSerializable( | ||
unionDiscriminator: 'vehicle_type', | ||
unionRename: UnionRename.snake, | ||
) | ||
sealed class Vehicle { | ||
final String vehicleID; | ||
|
||
Vehicle({required this.vehicleID}); | ||
|
||
factory Vehicle.fromJson(Map<String, dynamic> json) => | ||
_$VehicleFromJson(json); | ||
|
||
Map<String, dynamic> toJson() => _$VehicleToJson(this); | ||
} | ||
|
||
@JsonSerializable() | ||
class Car extends Vehicle { | ||
final int numberOfDoors; | ||
|
||
Car({ | ||
required this.numberOfDoors, | ||
required super.vehicleID, | ||
}); | ||
} | ||
|
||
@JsonSerializable() | ||
class Bicycle extends Vehicle { | ||
final bool hasBell; | ||
|
||
Bicycle({ | ||
required this.hasBell, | ||
required super.vehicleID, | ||
}); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sad that we're creating a new type here. We could just use
FieldRename
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My thought process was that it might be confusing for the package users if union rename is of type
FieldRename
, since its renaming class names and not any field names. Although after the renaming the string is used as key of a map so there the field would make a bit more sense again.Technically there shouldn't be a limitation for not just using
FieldRename
since they have the same rename options (at least now, probably no need to ever have different options). So should we remove theUnionRename
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Our users will be fine. A better thing would probably be to rename
FieldRename
to just beRenameType
and thentypedef FieldRename = RenameType;
and mark as deprecated.Just put a
todo
in for that and I can follow-up.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, this is now done. Super nice and handy the typedef deprecation trick!