Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
47 changes: 38 additions & 9 deletions pkgs/pubspec_parse/lib/src/dependency.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.'),
};
});
Expand All @@ -94,7 +92,9 @@ Dependency? _fromJson(Object? data, String name) {
return null;
}

sealed class Dependency {}
sealed class Dependency {
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 @@ -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) =>
Expand All @@ -114,6 +114,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 +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) =>
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 @@ -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) =>
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,7 +296,15 @@ 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) =>
input == null ? VersionConstraint.any : VersionConstraint.parse(input);

Map<String, dynamic> serializeDeps(Map<String, Dependency> input) =>
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 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

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

input.map((k, v) => MapEntry(k, v.toJson()));
49 changes: 31 additions & 18 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 @@ -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.');
}
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,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) ?? {};
Loading