Skip to content

[native_assets_cli] Prevent EncodedAsset and Metadata hashcode changing #2131

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

Merged
merged 1 commit into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion pkgs/native_assets_cli/lib/src/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ String _jsonChecksum(Map<String, Object?> json) {
final class BuildInput extends HookInput {
Map<String, Metadata> get metadata => {
for (final entry in (_syntaxBuildInput.dependencyMetadata ?? {}).entries)
entry.key: Metadata.fromJson(entry.value),
entry.key: Metadata(entry.value),
};

@override
Expand Down
24 changes: 17 additions & 7 deletions pkgs/native_assets_cli/lib/src/encoded_asset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,36 @@ final class EncodedAsset {
final String type;

/// The json encoding of the asset.
final Map<String, Object?> encoding;
final UnmodifiableMapView<String, Object?> encoding;

/// The path of this object in a larger JSON.
///
/// If provided, used for more precise error messages.
final List<Object>? jsonPath;

EncodedAsset(this.type, this.encoding, {this.jsonPath});
EncodedAsset._(this.type, this.encoding, {this.jsonPath});

EncodedAsset(this.type, Map<String, Object?> encoding, {this.jsonPath})
: encoding = UnmodifiableMapView(
// It would be better if `encoding` would be deep copied.
// https://github.com/dart-lang/native/issues/2045
Map.of(encoding),
);

/// Decode an [EncodedAsset] from json.
factory EncodedAsset.fromJson(
Map<String, Object?> json, [
List<Object>? path,
]) {
final syntax_ = syntax.Asset.fromJson(json);
return EncodedAsset(syntax_.type, {
for (final key in json.keys)
if (key != _typeKey) key: json[key],
}, jsonPath: path);
return EncodedAsset._(
syntax_.type,
UnmodifiableMapView({
for (final key in json.keys)
if (key != _typeKey) key: json[key],
}),
jsonPath: path,
);
}

/// Encode this [EncodedAsset] tojson.
Expand All @@ -42,7 +53,6 @@ final class EncodedAsset {
@override
String toString() => 'EncodedAsset($type, $encoding)';

// TODO(https://github.com/dart-lang/native/issues/2045): Fix this.
@override
int get hashCode => Object.hash(type, const DeepCollectionEquality().hash);

Expand Down
14 changes: 8 additions & 6 deletions pkgs/native_assets_cli/lib/src/metadata.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import 'package:collection/collection.dart';

class Metadata {
final Map<String, Object?> metadata;

const Metadata(this.metadata);

factory Metadata.fromJson(Map<String, Object?>? jsonMap) =>
Metadata(jsonMap ?? {});
final UnmodifiableMapView<String, Object?> metadata;

Metadata(Map<String, Object?> metadata)
: metadata = UnmodifiableMapView(
// It would be better if `jsonMap` would be deep copied.
// https://github.com/dart-lang/native/issues/2045
Map.of(metadata),
);

Map<String, Object?> toJson() => metadata;

Expand Down
4 changes: 2 additions & 2 deletions pkgs/native_assets_cli/test/build_input_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ void main() async {
packageName = 'my_package';
packageRootUri = tempUri.resolve('$packageName/');
metadata = {
'bar': const Metadata({
'bar': Metadata({
'key': 'value',
'foo': ['asdf', 'fdsa'],
}),
'foo': const Metadata({'key': 321}),
'foo': Metadata({'key': 321}),
};

inputJson = {
Expand Down
2 changes: 1 addition & 1 deletion pkgs/native_assets_cli/test/model/metadata_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:native_assets_cli/native_assets_cli.dart';
import 'package:test/test.dart';

void main() {
const metadata = Metadata({
final metadata = Metadata({
'key': 'value',
'my_list': [1, 2, 3],
'my_map': {3: 4, 'foo': 'bar'},
Expand Down