diff --git a/pkgs/dart_model/lib/src/dart_model.g.dart b/pkgs/dart_model/lib/src/dart_model.g.dart index 788b0afd..26458085 100644 --- a/pkgs/dart_model/lib/src/dart_model.g.dart +++ b/pkgs/dart_model/lib/src/dart_model.g.dart @@ -1,6 +1,10 @@ // This file is generated. To make changes edit tool/dart_model_generator // then run from the repo root: dart tool/dart_model_generator/bin/main.dart +// ignore: implementation_imports,unused_import,prefer_relative_imports +import 'dart:typed_data'; +// ignore: implementation_imports,unused_import,prefer_relative_imports +import 'package:crypto/crypto.dart'; // ignore: implementation_imports,unused_import,prefer_relative_imports import 'package:dart_model/src/deep_cast_map.dart'; // ignore: implementation_imports,unused_import,prefer_relative_imports @@ -251,6 +255,11 @@ extension type Model.fromJson(Map node) implements Object { Scope.createGrowableMap(), types, )); +// A digest of all the bytes in the current buffer. + Digest get digest => md5.convert(switch (node) { + MapInBuffer mapInBuffer => mapInBuffer.buffer.serialize(), + _ => (JsonBufferBuilder()..map.addAll(node)).serialize(), + }); /// Libraries by URI. Map get uris => diff --git a/pkgs/dart_model/lib/src/json_buffer/iterables.dart b/pkgs/dart_model/lib/src/json_buffer/iterables.dart index d08aedee..3c24938d 100644 --- a/pkgs/dart_model/lib/src/json_buffer/iterables.dart +++ b/pkgs/dart_model/lib/src/json_buffer/iterables.dart @@ -42,7 +42,7 @@ class _IteratorFunctionIterable extends Iterable { /// A `Map` in a `JsonBufferBuilder`. abstract interface class MapInBuffer { - /// The buffer backing this `Map`. + /// The buffer backing this [Map]. JsonBufferBuilder get buffer; /// The `Map` that contains this value, or `null` if this value has not been diff --git a/pkgs/dart_model/lib/src/macro_metadata.g.dart b/pkgs/dart_model/lib/src/macro_metadata.g.dart index fd022236..129f6b40 100644 --- a/pkgs/dart_model/lib/src/macro_metadata.g.dart +++ b/pkgs/dart_model/lib/src/macro_metadata.g.dart @@ -1,6 +1,10 @@ // This file is generated. To make changes edit tool/dart_model_generator // then run from the repo root: dart tool/dart_model_generator/bin/main.dart +// ignore: implementation_imports,unused_import,prefer_relative_imports +import 'dart:typed_data'; +// ignore: implementation_imports,unused_import,prefer_relative_imports +import 'package:crypto/crypto.dart'; // ignore: implementation_imports,unused_import,prefer_relative_imports import 'package:dart_model/src/deep_cast_map.dart'; // ignore: implementation_imports,unused_import,prefer_relative_imports diff --git a/pkgs/dart_model/pubspec.yaml b/pkgs/dart_model/pubspec.yaml index 766e2593..903ea18c 100644 --- a/pkgs/dart_model/pubspec.yaml +++ b/pkgs/dart_model/pubspec.yaml @@ -12,6 +12,7 @@ environment: dependencies: collection: ^1.19.0 + crypto: ^3.0.6 dev_dependencies: benchmark_harness: ^2.2.2 diff --git a/pkgs/dart_model/test/model_test.dart b/pkgs/dart_model/test/model_test.dart index f17b0b6b..acae8f2f 100644 --- a/pkgs/dart_model/test/model_test.dart +++ b/pkgs/dart_model/test/model_test.dart @@ -201,6 +201,11 @@ void main() { expect(copiedModel.qualifiedNameOf(member.node), null); expect(model.qualifiedNameOf(copiedMember.node), null); }); + + test('can produce and compare digests for buffer and SDK maps', () { + final copiedModel = Model.fromJson(_copyMap(model.node)); + expect(model.digest, copiedModel.digest); + }); }); group('QualifiedName', () { diff --git a/pkgs/macro_service/lib/src/handshake.g.dart b/pkgs/macro_service/lib/src/handshake.g.dart index bfd6dbfc..066febaf 100644 --- a/pkgs/macro_service/lib/src/handshake.g.dart +++ b/pkgs/macro_service/lib/src/handshake.g.dart @@ -1,6 +1,10 @@ // This file is generated. To make changes edit tool/dart_model_generator // then run from the repo root: dart tool/dart_model_generator/bin/main.dart +// ignore: implementation_imports,unused_import,prefer_relative_imports +import 'dart:typed_data'; +// ignore: implementation_imports,unused_import,prefer_relative_imports +import 'package:crypto/crypto.dart'; // ignore: implementation_imports,unused_import,prefer_relative_imports import 'package:dart_model/src/deep_cast_map.dart'; // ignore: implementation_imports,unused_import,prefer_relative_imports diff --git a/pkgs/macro_service/lib/src/macro_service.g.dart b/pkgs/macro_service/lib/src/macro_service.g.dart index 83d16713..24aa0e08 100644 --- a/pkgs/macro_service/lib/src/macro_service.g.dart +++ b/pkgs/macro_service/lib/src/macro_service.g.dart @@ -1,6 +1,10 @@ // This file is generated. To make changes edit tool/dart_model_generator // then run from the repo root: dart tool/dart_model_generator/bin/main.dart +// ignore: implementation_imports,unused_import,prefer_relative_imports +import 'dart:typed_data'; +// ignore: implementation_imports,unused_import,prefer_relative_imports +import 'package:crypto/crypto.dart'; // ignore: implementation_imports,unused_import,prefer_relative_imports import 'package:dart_model/src/dart_model.g.dart'; // ignore: implementation_imports,unused_import,prefer_relative_imports diff --git a/tool/dart_model_generator/lib/definitions.dart b/tool/dart_model_generator/lib/definitions.dart index 00598f69..dbe1c09c 100644 --- a/tool/dart_model_generator/lib/definitions.dart +++ b/tool/dart_model_generator/lib/definitions.dart @@ -183,16 +183,21 @@ static Protocol handshakeProtocol = Protocol( description: 'The named parameters of this member, ' 'if it has them.'), ]), - Definition.clazz('Model', - description: 'Partial model of a corpus of Dart source code.', - createInBuffer: true, - properties: [ - Property('uris', - type: 'Map', description: 'Libraries by URI.'), - Property('types', - type: 'TypeHierarchy', - description: 'The resolved static type hierarchy.'), - ]), + Definition.clazz( + 'Model', + description: 'Partial model of a corpus of Dart source code.', + createInBuffer: true, + properties: [ + Property('uris', + type: 'Map', description: 'Libraries by URI.'), + Property('types', + type: 'TypeHierarchy', + description: 'The resolved static type hierarchy.'), + ], + extraCode: '// A digest of all the bytes in the current buffer.\n' + 'Digest get digest => ' + 'md5.convert((node as MapInBuffer).buffer.serialize());', + ), Definition.clazz('NamedFunctionTypeParameter', description: 'A resolved named parameter as part of a [FunctionTypeDesc].', diff --git a/tool/dart_model_generator/lib/generate_dart_model.dart b/tool/dart_model_generator/lib/generate_dart_model.dart index 23efa351..77d315b0 100644 --- a/tool/dart_model_generator/lib/generate_dart_model.dart +++ b/tool/dart_model_generator/lib/generate_dart_model.dart @@ -65,6 +65,8 @@ class GenerationContext { lookupDeclaringSchema(typeName), }.nonNulls; return ([ + "import 'dart:typed_data';", + "import 'package:crypto/crypto.dart';", "import 'package:dart_model/src/json_buffer/json_buffer_builder.dart';", "import 'package:dart_model/src/deep_cast_map.dart';", "import 'package:dart_model/src/scopes.dart';",