-
Notifications
You must be signed in to change notification settings - Fork 77
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 2 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 |
|---|---|---|
|
|
@@ -46,10 +46,8 @@ Dependency? _fromJson(Object? data, String name) { | |
| } | ||
|
|
||
| if (data is Map) { | ||
| final matchedKeys = data.keys | ||
| .cast<String>() | ||
| .where((key) => key != 'version') | ||
| .toList(); | ||
| final matchedKeys = | ||
| data.keys.cast<String>().where((key) => key != 'version').toList(); | ||
|
|
||
| if (data.isEmpty || (matchedKeys.isEmpty && data.containsKey('version'))) { | ||
| return _$HostedDependencyFromJson(data); | ||
|
|
@@ -82,8 +80,8 @@ Dependency? _fromJson(Object? data, String name) { | |
| 'path' => PathDependency.fromData(data[key]), | ||
| 'sdk' => _$SdkDependencyFromJson(data), | ||
| 'hosted' => _$HostedDependencyFromJson( | ||
| data, | ||
| )..hosted?._nameOfPackage = name, | ||
| data, | ||
| )..hosted?._nameOfPackage = name, | ||
| _ => throw StateError('There is a bug in pubspec_parse.'), | ||
| }; | ||
| }); | ||
|
|
@@ -94,7 +92,9 @@ Dependency? _fromJson(Object? data, String name) { | |
| return null; | ||
| } | ||
|
|
||
| sealed class Dependency {} | ||
| sealed class Dependency { | ||
| Map<String, dynamic> toJson(); | ||
| } | ||
|
|
||
| @JsonSerializable() | ||
| class SdkDependency extends Dependency { | ||
|
|
@@ -103,7 +103,7 @@ class SdkDependency extends Dependency { | |
| final VersionConstraint version; | ||
|
|
||
| SdkDependency(this.sdk, {VersionConstraint? version}) | ||
| : version = version ?? VersionConstraint.any; | ||
| : version = version ?? VersionConstraint.any; | ||
|
|
||
| @override | ||
| bool operator ==(Object other) => | ||
|
|
@@ -114,6 +114,9 @@ class SdkDependency extends Dependency { | |
|
|
||
| @override | ||
| String toString() => 'SdkDependency: $sdk'; | ||
|
|
||
| @override | ||
| Map<String, dynamic> toJson() => {'sdk': sdk, 'version': version.toString()}; | ||
| } | ||
|
|
||
| @JsonSerializable() | ||
|
|
@@ -149,6 +152,15 @@ class GitDependency extends Dependency { | |
|
|
||
| @override | ||
| String toString() => 'GitDependency: url@$url'; | ||
|
|
||
| @override | ||
| Map<String, dynamic> toJson() => { | ||
| 'git': { | ||
| 'url': url.toString(), | ||
| if (ref != null) 'ref': ref, | ||
| if (path != null) 'path': path, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| Uri? parseGitUriOrNull(String? value) => | ||
|
|
@@ -208,6 +220,9 @@ class PathDependency extends Dependency { | |
|
|
||
| @override | ||
| String toString() => 'PathDependency: path@$path'; | ||
|
|
||
| @override | ||
| Map<String, dynamic> toJson() => {'path': path}; | ||
| } | ||
|
|
||
| @JsonSerializable(disallowUnrecognizedKeys: true) | ||
|
|
@@ -219,7 +234,7 @@ class HostedDependency extends Dependency { | |
| final HostedDetails? hosted; | ||
|
|
||
| HostedDependency({VersionConstraint? version, this.hosted}) | ||
| : version = version ?? VersionConstraint.any; | ||
| : version = version ?? VersionConstraint.any; | ||
|
|
||
| @override | ||
| bool operator ==(Object other) => | ||
|
|
@@ -232,6 +247,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,7 +296,15 @@ 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) => | ||
| input == null ? VersionConstraint.any : VersionConstraint.parse(input); | ||
|
|
||
| Map<String, dynamic> serializeDeps(Map<String, Dependency> input) => | ||
|
||
| input.map((k, v) => MapEntry(k, v.toJson())); | ||
Dhruv-Maradiya marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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. | ||||||
|
|
@@ -126,16 +126,16 @@ class Pubspec { | |||||
| Map<String, Dependency>? dependencyOverrides, | ||||||
| this.flutter, | ||||||
| Map<String, String?>? executables, | ||||||
| }) : authors // ignore: deprecated_member_use_from_same_package | ||||||
| = _normalizeAuthors( | ||||||
| author, | ||||||
| authors, | ||||||
| ), | ||||||
| environment = environment ?? const {}, | ||||||
| dependencies = dependencies ?? const {}, | ||||||
| devDependencies = devDependencies ?? const {}, | ||||||
| executables = executables ?? const {}, | ||||||
| dependencyOverrides = dependencyOverrides ?? const {} { | ||||||
| }) : authors // ignore: deprecated_member_use_from_same_package | ||||||
| = _normalizeAuthors( | ||||||
| author, | ||||||
| authors, | ||||||
| ), | ||||||
| environment = environment ?? const {}, | ||||||
| dependencies = dependencies ?? const {}, | ||||||
| devDependencies = devDependencies ?? const {}, | ||||||
| executables = executables ?? const {}, | ||||||
| dependencyOverrides = dependencyOverrides ?? const {} { | ||||||
| if (name.isEmpty) { | ||||||
| throw ArgumentError.value(name, 'name', '"name" cannot be empty.'); | ||||||
| } | ||||||
|
|
@@ -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,13 @@ 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(MapEntry.new) ?? {}; | ||||||
Dhruv-Maradiya marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
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.
This new public method, and all its implementations in the subclasses (
SdkDependency,GitDependency, etc.), should have documentation comments explaining what they do. This is required by the repository's style guide.1For example:
/// Returns a JSON-serializable representation of this dependency.Style Guide References
Footnotes
The style guide requires that all public members have documentation. ↩
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'd say
/// Creates a JSON representation of the data of this object.(Never start DartDoc with the word "Returns".)