-
Notifications
You must be signed in to change notification settings - Fork 78
Added the toJson method in Pubspec
#2214
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
base: main
Are you sure you want to change the base?
Changes from all commits
adb6830
421b6cf
e6058da
ca5c509
f9656ff
c74d45c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,7 +39,10 @@ Map<String, Dependency> parseDeps(Map? source) => | |
|
|
||
| const _sourceKeys = ['sdk', 'git', 'path', 'hosted']; | ||
|
|
||
| /// Returns `null` if the data could not be parsed. | ||
| /// Converts [data] into a [Dependency] object. | ||
| /// | ||
| /// If [data] is not a valid representation of a dependency, | ||
| /// returns null so that the parent logic can throw the proper error. | ||
| Dependency? _fromJson(Object? data, String name) { | ||
| if (data is String || data == null) { | ||
| return _$HostedDependencyFromJson({'version': data}); | ||
|
|
@@ -90,11 +93,13 @@ Dependency? _fromJson(Object? data, String name) { | |
| } | ||
| } | ||
|
|
||
| // Not a String or a Map – return null so parent logic can throw proper error | ||
| return null; | ||
| } | ||
|
|
||
| sealed class Dependency {} | ||
| sealed class Dependency { | ||
| /// Creates a JSON representation of the data of this object. | ||
| Map<String, dynamic> toJson(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This new public method, and all its implementations in the subclasses ( For example: /// Returns a JSON-serializable representation of this dependency.Style Guide ReferencesFootnotes
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd say /// Creates a JSON representation of the data of this object.(Never start DartDoc with the word "Returns".) |
||
| } | ||
|
|
||
| @JsonSerializable() | ||
| class SdkDependency extends Dependency { | ||
|
|
@@ -114,6 +119,9 @@ class SdkDependency extends Dependency { | |
|
|
||
| @override | ||
| String toString() => 'SdkDependency: $sdk'; | ||
|
|
||
| @override | ||
| Map<String, dynamic> toJson() => {'sdk': sdk, 'version': version.toString()}; | ||
| } | ||
|
|
||
| @JsonSerializable() | ||
|
|
@@ -149,6 +157,11 @@ class GitDependency extends Dependency { | |
|
|
||
| @override | ||
| String toString() => 'GitDependency: url@$url'; | ||
|
|
||
| @override | ||
| Map<String, dynamic> toJson() => { | ||
| 'git': {'url': url.toString(), 'ref': ?ref, 'path': ?path}, | ||
| }; | ||
| } | ||
|
|
||
| Uri? parseGitUriOrNull(String? value) => | ||
|
|
@@ -208,6 +221,9 @@ class PathDependency extends Dependency { | |
|
|
||
| @override | ||
| String toString() => 'PathDependency: path@$path'; | ||
|
|
||
| @override | ||
| Map<String, dynamic> toJson() => {'path': path}; | ||
| } | ||
|
|
||
| @JsonSerializable(disallowUnrecognizedKeys: true) | ||
|
|
@@ -232,6 +248,12 @@ class HostedDependency extends Dependency { | |
|
|
||
| @override | ||
| String toString() => 'HostedDependency: $version'; | ||
|
|
||
| @override | ||
| Map<String, dynamic> toJson() => { | ||
| 'version': version.toString(), | ||
| if (hosted != null) 'hosted': hosted!.toJson(), | ||
| }; | ||
| } | ||
|
|
||
| @JsonSerializable(disallowUnrecognizedKeys: true) | ||
|
|
@@ -275,6 +297,11 @@ class HostedDetails { | |
|
|
||
| @override | ||
| int get hashCode => Object.hash(name, url); | ||
|
|
||
| Map<String, dynamic> toJson() => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This new public method should have a documentation comment explaining what it does, as per the repository's style guide.1 For example: /// Returns a JSON-serializable representation of this object.Style Guide ReferencesFootnotes
|
||
| if (declaredName != null) 'name': declaredName, | ||
| 'url': url.toString(), | ||
| }; | ||
| } | ||
|
|
||
| VersionConstraint _constraintFromString(String? input) => | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -11,13 +11,13 @@ import 'screenshot.dart'; | |||||
|
|
||||||
| part 'pubspec.g.dart'; | ||||||
|
|
||||||
| @JsonSerializable() | ||||||
| @JsonSerializable(createToJson: true) | ||||||
| class Pubspec { | ||||||
| // TODO: executables | ||||||
|
|
||||||
| final String name; | ||||||
|
|
||||||
| @JsonKey(fromJson: _versionFromString) | ||||||
| @JsonKey(fromJson: _versionFromString, toJson: _versionToString) | ||||||
| final Version? version; | ||||||
|
|
||||||
| final String? description; | ||||||
|
|
@@ -51,7 +51,7 @@ class Pubspec { | |||||
| final List<String>? ignoredAdvisories; | ||||||
|
|
||||||
| /// Optional field for specifying included screenshot files. | ||||||
| @JsonKey(fromJson: parseScreenshots) | ||||||
| @JsonKey(fromJson: parseScreenshots, toJson: serializeScreenshots) | ||||||
| final List<Screenshot>? screenshots; | ||||||
|
|
||||||
| /// If there is exactly 1 value in [authors], returns it. | ||||||
|
|
@@ -69,16 +69,16 @@ class Pubspec { | |||||
| final List<String> authors; | ||||||
| final String? documentation; | ||||||
|
|
||||||
| @JsonKey(fromJson: _environmentMap) | ||||||
| @JsonKey(fromJson: _environmentMap, toJson: _serializeEnvironment) | ||||||
| final Map<String, VersionConstraint?> environment; | ||||||
|
|
||||||
| @JsonKey(fromJson: parseDeps) | ||||||
| @JsonKey(fromJson: parseDeps, toJson: _serializeDeps) | ||||||
| final Map<String, Dependency> dependencies; | ||||||
|
|
||||||
| @JsonKey(fromJson: parseDeps) | ||||||
| @JsonKey(fromJson: parseDeps, toJson: _serializeDeps) | ||||||
| final Map<String, Dependency> devDependencies; | ||||||
|
|
||||||
| @JsonKey(fromJson: parseDeps) | ||||||
| @JsonKey(fromJson: parseDeps, toJson: _serializeDeps) | ||||||
| final Map<String, Dependency> dependencyOverrides; | ||||||
|
|
||||||
| /// Optional configuration specific to [Flutter](https://flutter.io/) | ||||||
|
|
@@ -90,7 +90,7 @@ class Pubspec { | |||||
| final Map<String, dynamic>? flutter; | ||||||
|
|
||||||
| /// Optional field to specify executables | ||||||
| @JsonKey(fromJson: _executablesMap) | ||||||
| @JsonKey(fromJson: _executablesMap, toJson: _serializeExecutables) | ||||||
| final Map<String, String?> executables; | ||||||
|
|
||||||
| /// If this package is a Pub Workspace, this field lists the sub-packages. | ||||||
|
|
@@ -171,6 +171,9 @@ class Pubspec { | |||||
| return _$PubspecFromJson(json); | ||||||
| } | ||||||
|
|
||||||
| // Returns a JSON-serializable map for this instance. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per Effective Dart, documentation comments should use
Suggested change
Style Guide ReferencesFootnotes
|
||||||
| Map<String, dynamic> toJson() => _$PubspecToJson(this); | ||||||
|
|
||||||
| /// Parses source [yaml] into [Pubspec]. | ||||||
| /// | ||||||
| /// When [lenient] is set, top-level property-parsing or type cast errors are | ||||||
|
|
@@ -247,3 +250,17 @@ Map<String, String?> _executablesMap(Map? source) => | |||||
| } | ||||||
| }) ?? | ||||||
| {}; | ||||||
|
|
||||||
| Map<String, String?> _serializeEnvironment( | ||||||
| Map<String, VersionConstraint?> map, | ||||||
| ) => map.map((key, value) => MapEntry(key, value?.toString())); | ||||||
|
|
||||||
| String? _versionToString(Version? version) => version?.toString(); | ||||||
|
|
||||||
| Map<String, String?> _serializeExecutables(Map<String, String?>? map) => { | ||||||
| ...?map, | ||||||
| }; | ||||||
|
|
||||||
| Map<String, dynamic> _serializeDeps(Map<String, Dependency> input) => { | ||||||
| for (final MapEntry(:key, :value) in input.entries) key: value.toJson(), | ||||||
| }; | ||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.