- 
                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?
  
    Added the toJson method in Pubspec
  
  #2214
              Conversation
Closes dart-lang#1801 - Added the serializable to serialize object into . - Added tests to validate functionality round-trip the shape of the source. - Updated to document the changes.
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.
Code Review
This pull request successfully adds the toJson method to Pubspec and its related classes, enabling serialization. The implementation is correct and is well-covered by new round-trip tests, which is excellent. My review includes a few suggestions, mainly about adding documentation to new public members to adhere to the repository's style guide.
I also noticed that this pull request includes a significant refactoring of tests in the watcher package. While these changes seem to improve test stability, it's generally better to keep unrelated changes in separate pull requests. This helps keep the commit history clean and makes reviews more focused. For this review, I have focused only on the changes related to pubspec_parse.
|  | ||
| sealed class Dependency {} | ||
| sealed class Dependency { | ||
| Map<String, dynamic> toJson(); | 
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.1
For 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".)
| @override | ||
| int get hashCode => Object.hash(name, url); | ||
|  | ||
| Map<String, dynamic> toJson() => { | 
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 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 References
Footnotes
- 
The style guide requires that all public members have documentation. ↩ 
| VersionConstraint _constraintFromString(String? input) => | ||
| input == null ? VersionConstraint.any : VersionConstraint.parse(input); | ||
|  | ||
| Map<String, dynamic> serializeDeps(Map<String, Dependency> input) => | 
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 function should have a documentation comment explaining what it does, as per the repository's style guide.1
For example:
/// Serializes a map of [Dependency] objects into a JSON-serializable map.Style Guide References
Footnotes
- 
The style guide requires that all public members have documentation. ↩ 
| return _$PubspecFromJson(json); | ||
| } | ||
|  | ||
| // Returns a JSON-serializable map for this instance. | 
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.
Per Effective Dart, documentation comments should use /// instead of //.1
| // Returns a JSON-serializable map for this instance. | |
| /// Returns a JSON-serializable map for this instance. | 
Style Guide References
Footnotes
- 
The style guide recommends following the guidance described in Effective Dart, which includes using ///for documentation comments. ↩
| return res; | ||
| } | ||
|  | ||
| List<Map<String, String>> serializeScreenshots(List<Screenshot>? input) => | 
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 function should have a documentation comment explaining what it does, as per the repository's style guide.1
For example:
/// Serializes a list of [Screenshot] objects into a JSON-serializable list of maps.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.
Style comments only. I have no opinion on whether this is a good addition.
|  | ||
| sealed class Dependency {} | ||
| sealed class Dependency { | ||
| Map<String, dynamic> toJson(); | 
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".)
- Make serializeDeps private (_serializeDeps) - Use concise literals (for-in, null-aware spread) in screenshots, executables, and deps - Clarify _fromJson docs and tidy formatting across dependency/pubspec files - Regenerate pubspec.g.dart for updated serializer and formatting
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.
Style seems fine.
Someone who actually knows what this code does should review the actual change.
| 
 We'll probably want someone from @dart-lang/dart-pub-team to confirm they are happy with the API. | 
| final value = await parse(defaultPubspec); | ||
| final jsonValue = value.toJson(); | ||
|  | ||
| expect(jsonValue['name'], 'sample'); | 
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.
[optional] We can check the entire collection in the same call to expect as long as we don't need to ignore other keys that aren't checked here.
expect(jsonValue, {'name': 'sample', 'environment': {'sdk': '>=2.12.0 <3.0.0'}});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.
Or include the 'key': null pairs if they are included as literal nulls and not missing keys.
| expect(value.dependencies['google_fonts'], isA<SdkDependency>()); | ||
| expect(value.dependencies['flutter_bloc'], isA<GitDependency>()); | ||
| expect(value.dependencies['shared_preferences'], isA<GitDependency>()); | ||
| expect(value.dependencies['local_utils'], isA<PathDependency>()); | 
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.
| expect(value.dependencies['local_utils'], isA<PathDependency>()); | |
| expect(value.dependencies, { | |
| 'flutter': isA<SdkDependency>(), | |
| 'http': isA<HostedDependency>(), | |
| 'provider': isA<HostedDependency>(), | |
| 'firebase_core': isA<HostedDependency>(), | |
| 'google_fonts': isA<SdkDependency>(), | |
| 'flutter_bloc': isA<GitDependency>(), | |
| 'shared_preferences': isA<GitDependency>(), | |
| 'local_utils': isA<PathDependency>(), | |
| }); | 
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.
Looks like there was another merge mistake in this branch, maybe a commit came in after I had set the worktree up in the other branch? In any case, still less messy than before and we can clean up this PR easily.
| Package publishing
 Documentation at https://github.com/dart-lang/ecosystem/wiki/Publishing-automation. | 
| PR HealthBreaking changes ✔️
 This check can be disabled by tagging the PR with  Changelog Entry ✔️
 Changes to files need to be accounted for in their respective changelogs. This check can be disabled by tagging the PR with  Coverage ✔️
 This check for test coverage is informational (issues shown here will not fail the PR). This check can be disabled by tagging the PR with  API leaks ✔️The following packages contain symbols visible in the public API, but not exported by the library. Export these symbols or remove them from your publicly visible API. 
 This check can be disabled by tagging the PR with  License Headers ✔️
 All source files should start with a license header. Unrelated files missing license headers
 This check can be disabled by tagging the PR with  | 
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.
LGTM
Description
Fixes #1801 by adding
toJsonmethod inPubspecclass.Changes
toJsonserializable to serialize object intomap.CHANGELOG.mdto document the changes.Checklist