Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit

Permalink
refactor!: Seal the Dependency class (#140)
Browse files Browse the repository at this point in the history
* fix: Don't use runtimeType for toString
  • Loading branch information
spydon authored Dec 11, 2024
1 parent 74c350c commit 3290389
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 28 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 1.3.1-wip
## 1.4.0-wip

- Require Dart 3.2
- Seal the `Dependency` class.
- Set `Pubspec.environment` to non-nullable.
- Remove deprecated package_api_docs rule

Expand Down
43 changes: 17 additions & 26 deletions lib/src/dependency.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,7 @@ Dependency? _fromJson(Object? data, String name) {
return null;
}

abstract class Dependency {
Dependency._();

String get _info;

@override
String toString() => '$runtimeType: $_info';
}
sealed class Dependency {}

@JsonSerializable()
class SdkDependency extends Dependency {
Expand All @@ -106,18 +99,17 @@ class SdkDependency extends Dependency {
final VersionConstraint version;

SdkDependency(this.sdk, {VersionConstraint? version})
: version = version ?? VersionConstraint.any,
super._();

@override
String get _info => sdk;
: version = version ?? VersionConstraint.any;

@override
bool operator ==(Object other) =>
other is SdkDependency && other.sdk == sdk && other.version == version;

@override
int get hashCode => Object.hash(sdk, version);

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

@JsonSerializable()
Expand All @@ -127,7 +119,7 @@ class GitDependency extends Dependency {
final String? ref;
final String? path;

GitDependency(this.url, {this.ref, this.path}) : super._();
GitDependency(this.url, {this.ref, this.path});

factory GitDependency.fromData(Object? data) {
if (data is String) {
Expand All @@ -141,9 +133,6 @@ class GitDependency extends Dependency {
throw ArgumentError.value(data, 'git', 'Must be a String or a Map.');
}

@override
String get _info => 'url@$url';

@override
bool operator ==(Object other) =>
other is GitDependency &&
Expand All @@ -153,6 +142,9 @@ class GitDependency extends Dependency {

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

@override
String toString() => 'GitDependency: url@$url';
}

Uri? parseGitUriOrNull(String? value) =>
Expand Down Expand Up @@ -194,7 +186,7 @@ Uri? _tryParseScpUri(String value) {
class PathDependency extends Dependency {
final String path;

PathDependency(this.path) : super._();
PathDependency(this.path);

factory PathDependency.fromData(Object? data) {
if (data is String) {
Expand All @@ -203,15 +195,15 @@ class PathDependency extends Dependency {
throw ArgumentError.value(data, 'path', 'Must be a String.');
}

@override
String get _info => 'path@$path';

@override
bool operator ==(Object other) =>
other is PathDependency && other.path == path;

@override
int get hashCode => path.hashCode;

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

@JsonSerializable(disallowUnrecognizedKeys: true)
Expand All @@ -223,11 +215,7 @@ class HostedDependency extends Dependency {
final HostedDetails? hosted;

HostedDependency({VersionConstraint? version, this.hosted})
: version = version ?? VersionConstraint.any,
super._();

@override
String get _info => version.toString();
: version = version ?? VersionConstraint.any;

@override
bool operator ==(Object other) =>
Expand All @@ -237,6 +225,9 @@ class HostedDependency extends Dependency {

@override
int get hashCode => Object.hash(version, hosted);

@override
String toString() => 'HostedDependency: $version';
}

@JsonSerializable(disallowUnrecognizedKeys: true)
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: pubspec_parse
version: 1.3.1-wip
version: 1.4.0-wip
description: >-
Simple package for parsing pubspec.yaml files with a type-safe API and rich
error reporting.
Expand Down

0 comments on commit 3290389

Please sign in to comment.