Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pkgs/pubspec_parse/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 1.5.1-wip
## 1.6.0-wip

- Added `toJson` method to `Pubspec` to serialize the object back to a `Map`.
- Require Dart 3.8

## 1.5.0
Expand Down
32 changes: 29 additions & 3 deletions pkgs/pubspec_parse/lib/src/dependency.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ 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});
Expand Down Expand Up @@ -90,11 +92,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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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

  1. The style guide requires that all public members have documentation.

Copy link
Member

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".)

}

@JsonSerializable()
class SdkDependency extends Dependency {
Expand All @@ -114,6 +118,9 @@ class SdkDependency extends Dependency {

@override
String toString() => 'SdkDependency: $sdk';

@override
Map<String, dynamic> toJson() => {'sdk': sdk, 'version': version.toString()};
}

@JsonSerializable()
Expand Down Expand Up @@ -149,6 +156,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) =>
Expand Down Expand Up @@ -208,6 +220,9 @@ class PathDependency extends Dependency {

@override
String toString() => 'PathDependency: path@$path';

@override
Map<String, dynamic> toJson() => {'path': path};
}

@JsonSerializable(disallowUnrecognizedKeys: true)
Expand All @@ -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)
Expand Down Expand Up @@ -275,6 +296,11 @@ class HostedDetails {

@override
int get hashCode => Object.hash(name, url);

Map<String, dynamic> toJson() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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

  1. The style guide requires that all public members have documentation.

if (declaredName != null) 'name': declaredName,
'url': url.toString(),
};
}

VersionConstraint _constraintFromString(String? input) =>
Expand Down
33 changes: 25 additions & 8 deletions pkgs/pubspec_parse/lib/src/pubspec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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/)
Expand All @@ -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.
Expand Down Expand Up @@ -171,6 +171,9 @@ class Pubspec {
return _$PubspecFromJson(json);
}

// Returns a JSON-serializable map for this instance.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Per Effective Dart, documentation comments should use /// instead of //.1

Suggested change
// Returns a JSON-serializable map for this instance.
/// Returns a JSON-serializable map for this instance.

Style Guide References

Footnotes

  1. The style guide recommends following the guidance described in Effective Dart, which includes using /// for documentation comments.

Map<String, dynamic> toJson() => _$PubspecToJson(this);

/// Parses source [yaml] into [Pubspec].
///
/// When [lenient] is set, top-level property-parsing or type cast errors are
Expand Down Expand Up @@ -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(),
};
25 changes: 25 additions & 0 deletions pkgs/pubspec_parse/lib/src/pubspec.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pkgs/pubspec_parse/lib/src/screenshot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,8 @@ List<Screenshot> parseScreenshots(List? input) {
}
return res;
}

List<Map<String, String>> serializeScreenshots(List<Screenshot>? input) => [
if (input != null)
for (var e in input) {'description': e.description, 'path': e.path},
];
2 changes: 1 addition & 1 deletion pkgs/pubspec_parse/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: pubspec_parse
version: 1.5.1-wip
version: 1.6.0-wip
description: >-
Simple package for parsing pubspec.yaml files with a type-safe API and rich
error reporting.
Expand Down
Loading