[record_use] Instance constant definitions
diff --git a/pkgs/record_use/doc/schema/record_use.schema.json b/pkgs/record_use/doc/schema/record_use.schema.json index 04b77f7..8727970 100644 --- a/pkgs/record_use/doc/schema/record_use.schema.json +++ b/pkgs/record_use/doc/schema/record_use.schema.json
@@ -114,11 +114,17 @@ }, "then": { "properties": { + "definition_index": { + "type": "integer" + }, "value": { "type": "object", "additionalProperties": true } - } + }, + "required": [ + "definition_index" + ] } }, {
diff --git a/pkgs/record_use/lib/src/constant.dart b/pkgs/record_use/lib/src/constant.dart index 4350875..0311f06 100644 --- a/pkgs/record_use/lib/src/constant.dart +++ b/pkgs/record_use/lib/src/constant.dart
@@ -4,6 +4,7 @@ import 'package:meta/meta.dart'; +import 'definition.dart'; import 'helper.dart'; import 'serialization_context.dart'; import 'syntax.g.dart'; @@ -80,11 +81,13 @@ ) .toList(), ), - InstanceConstantSyntax(value: final value) => InstanceConstant( - fields: (value?.json ?? {}).map( - (key, value) => MapEntry(key, context.constants[value as int]), + InstanceConstantSyntax(value: final value, :final definitionIndex) => + InstanceConstant( + definition: context.definitions[definitionIndex], + fields: (value?.json ?? {}).map( + (key, value) => MapEntry(key, context.constants[value as int]), + ), ), - ), UnsupportedConstantSyntax(:final message) => UnsupportedConstant(message), _ => throw UnimplementedError( '"${syntax.type}" is not a supported constant type', @@ -371,15 +374,20 @@ /// Only as far as they can also be represented by constants. This is more or /// less the same as a [MapConstant]. final class InstanceConstant extends Constant { + /// The definition of the class of this instance. + final Definition definition; + /// The fields of this instance, mapped from field name to [Constant] value. final Map<String, Constant> fields; - /// Creates an [InstanceConstant] object with the given [fields]. - const InstanceConstant({required this.fields}); + /// Creates an [InstanceConstant] object with the given [definition] and + /// [fields]. + const InstanceConstant({required this.definition, required this.fields}); @override InstanceConstantSyntax _toSyntax(SerializationContext context) => InstanceConstantSyntax( + definitionIndex: context.definitions[definition]!, value: fields.isNotEmpty ? JsonObjectSyntax.fromJson( fields.map( @@ -394,15 +402,17 @@ bool operator ==(Object other) { if (identical(this, other)) return true; - return other is InstanceConstant && deepEquals(other.fields, fields); + return other is InstanceConstant && + other.definition == definition && + deepEquals(other.fields, fields); } @override - int get hashCode => deepHash(fields); + int get hashCode => Object.hash(definition, deepHash(fields)); @override String toString() => - 'InstanceConstant({' + 'InstanceConstant($definition, {' '${fields.entries.map((e) => '${e.key}: ${e.value}').join(', ')}})'; @override @@ -411,6 +421,8 @@ bool allowPromotionOfUnsupported, ) { if (other is! InstanceConstant) return false; + // ignore: invalid_use_of_visible_for_testing_member + if (!definition.semanticEquals(other.definition)) return false; if (fields.length != other.fields.length) return false; for (final entry in fields.entries) { final otherField = other.fields[entry.key];
diff --git a/pkgs/record_use/lib/src/recordings.dart b/pkgs/record_use/lib/src/recordings.dart index 82d4af0..c92352d 100644 --- a/pkgs/record_use/lib/src/recordings.dart +++ b/pkgs/record_use/lib/src/recordings.dart
@@ -230,6 +230,7 @@ final allDefinitions = { ...calls.keys, ...instances.keys, + ...allConstants.whereType<InstanceConstant>().map((c) => c.definition), }.toList(); return DefinitionSerializationContext( allDefinitions.asMapToIndices,
diff --git a/pkgs/record_use/lib/src/serialization_context.dart b/pkgs/record_use/lib/src/serialization_context.dart index 5e1488c..16dd438 100644 --- a/pkgs/record_use/lib/src/serialization_context.dart +++ b/pkgs/record_use/lib/src/serialization_context.dart
@@ -31,7 +31,7 @@ /// /// 1. **Definitions**: [Definition] objects are deserialized from or /// serialized to the [RecordedUsesSyntax.definitions] pool first. -/// 2. **Constants**: [Constant] objects are (de)serialized from or serialized +/// 2. **Constants**: [Constant] objects are deserialized from or serialized /// to the [RecordedUsesSyntax.constants] pool second. They may contain /// references to the definitions pool (e.g. for [InstanceConstant]) and the /// constants pool (for recursive collections).
diff --git a/pkgs/record_use/lib/src/syntax.g.dart b/pkgs/record_use/lib/src/syntax.g.dart index 8cd6b77..6fed7c4 100644 --- a/pkgs/record_use/lib/src/syntax.g.dart +++ b/pkgs/record_use/lib/src/syntax.g.dart
@@ -187,7 +187,19 @@ List<String> _validateType() => _reader.validate<String>('type'); @override - List<String> validate() => [...super.validate(), ..._validateType()]; + List<String> validate() => [ + ...super.validate(), + ..._validateType(), + ..._validateExtraRulesConstant(), + ]; + + List<String> _validateExtraRulesConstant() { + final result = <String>[]; + if (_reader.tryTraverse(['type']) == 'instance') { + result.addAll(_reader.validate<Object>('definition_index')); + } + return result; + } @override String toString() => 'ConstantSyntax($json)'; @@ -584,19 +596,33 @@ super.path, }) : super._fromJson(); - InstanceConstantSyntax({JsonObjectSyntax? value, super.path = const []}) - : super(type: 'instance') { + InstanceConstantSyntax({ + required int definitionIndex, + JsonObjectSyntax? value, + super.path = const [], + }) : super(type: 'instance') { + _definitionIndex = definitionIndex; _value = value; json.sortOnKey(); } /// Setup all fields for [InstanceConstantSyntax] that are not in /// [ConstantSyntax]. - void setup({required JsonObjectSyntax? value}) { + void setup({required int definitionIndex, required JsonObjectSyntax? value}) { + _definitionIndex = definitionIndex; _value = value; json.sortOnKey(); } + int get definitionIndex => _reader.get<int>('definition_index'); + + set _definitionIndex(int value) { + json.setOrRemove('definition_index', value); + } + + List<String> _validateDefinitionIndex() => + _reader.validate<int>('definition_index'); + JsonObjectSyntax? get value { final jsonValue = _reader.optionalMap('value'); if (jsonValue == null) return null; @@ -616,7 +642,11 @@ } @override - List<String> validate() => [...super.validate(), ..._validateValue()]; + List<String> validate() => [ + ...super.validate(), + ..._validateDefinitionIndex(), + ..._validateValue(), + ]; @override String toString() => 'InstanceConstantSyntax($json)';
diff --git a/pkgs/record_use/test/complex_keys_test.dart b/pkgs/record_use/test/complex_keys_test.dart index 69aae0c..7f9f45d 100644 --- a/pkgs/record_use/test/complex_keys_test.dart +++ b/pkgs/record_use/test/complex_keys_test.dart
@@ -7,8 +7,14 @@ import 'package:test/test.dart'; void main() { + const classDefinition = Definition( + 'package:test/test.dart', + [Name('MyClass')], + ); + test('MapConstant with InstanceConstant keys round-trip', () { const instanceKey = InstanceConstant( + definition: classDefinition, fields: { 'id': IntConstant(1), 'tag': StringConstant('key'), @@ -49,6 +55,7 @@ test('MapConstant equality with InstanceConstant keys', () { const instanceKey = InstanceConstant( + definition: classDefinition, fields: { 'id': IntConstant(1), 'tag': StringConstant('key'), @@ -62,6 +69,7 @@ expect( mapConstant.entries.first.key, const InstanceConstant( + definition: classDefinition, fields: { 'id': IntConstant(1), 'tag': StringConstant('key'),
diff --git a/pkgs/record_use/test/test_data.dart b/pkgs/record_use/test/test_data.dart index 4c968fa..9b44d24 100644 --- a/pkgs/record_use/test/test_data.dart +++ b/pkgs/record_use/test/test_data.dart
@@ -63,12 +63,13 @@ instanceId: [ const InstanceConstantReference( instanceConstant: InstanceConstant( + definition: instanceId, fields: {'a': IntConstant(42), 'b': NullConstant()}, ), loadingUnit: '3', ), const InstanceConstantReference( - instanceConstant: InstanceConstant(fields: {}), + instanceConstant: InstanceConstant(definition: instanceId, fields: {}), loadingUnit: '3', ), ], @@ -176,6 +177,7 @@ "type": "null" }, { + "definition_index": 1, "type": "instance", "value": { "a": 14, @@ -183,6 +185,7 @@ } }, { + "definition_index": 1, "type": "instance" } ],
diff --git a/pkgs/record_use/test_data/json/const_argument_instance.json b/pkgs/record_use/test_data/json/const_argument_instance.json index b2a3f21..b37e0f0 100644 --- a/pkgs/record_use/test_data/json/const_argument_instance.json +++ b/pkgs/record_use/test_data/json/const_argument_instance.json
@@ -6,6 +6,7 @@ "value": 14 }, { + "definition_index": 0, "type": "instance", "value": { "i": 0
diff --git a/pkgs/record_use/test_data/json/enum_const_arg.json b/pkgs/record_use/test_data/json/enum_const_arg.json index e448987..1d71897 100644 --- a/pkgs/record_use/test_data/json/enum_const_arg.json +++ b/pkgs/record_use/test_data/json/enum_const_arg.json
@@ -10,6 +10,7 @@ "value": "b" }, { + "definition_index": 0, "type": "instance", "value": { "index": 0,
diff --git a/pkgs/record_use/test_data/json/instance_class.json b/pkgs/record_use/test_data/json/instance_class.json index 071c9a8..4fe56cd 100644 --- a/pkgs/record_use/test_data/json/instance_class.json +++ b/pkgs/record_use/test_data/json/instance_class.json
@@ -6,6 +6,7 @@ "value": 42 }, { + "definition_index": 0, "type": "instance", "value": { "i": 0
diff --git a/pkgs/record_use/test_data/json/instance_complex.json b/pkgs/record_use/test_data/json/instance_complex.json index a2e112e..c53705c 100644 --- a/pkgs/record_use/test_data/json/instance_complex.json +++ b/pkgs/record_use/test_data/json/instance_complex.json
@@ -57,6 +57,7 @@ "type": "null" }, { + "definition_index": 0, "type": "instance", "value": { "i": 0,
diff --git a/pkgs/record_use/test_data/json/instance_duplicates.json b/pkgs/record_use/test_data/json/instance_duplicates.json index fb85faa..08fa441 100644 --- a/pkgs/record_use/test_data/json/instance_duplicates.json +++ b/pkgs/record_use/test_data/json/instance_duplicates.json
@@ -6,6 +6,7 @@ "value": 42 }, { + "definition_index": 0, "type": "instance", "value": { "i": 0 @@ -16,6 +17,7 @@ "value": 43 }, { + "definition_index": 0, "type": "instance", "value": { "i": 2
diff --git a/pkgs/record_use/test_data/json/instance_not_annotation.json b/pkgs/record_use/test_data/json/instance_not_annotation.json index 17b346c..db0ad66 100644 --- a/pkgs/record_use/test_data/json/instance_not_annotation.json +++ b/pkgs/record_use/test_data/json/instance_not_annotation.json
@@ -2,6 +2,7 @@ "$schema": "../../doc/schema/record_use.schema.json", "constants": [ { + "definition_index": 0, "type": "instance" } ],
diff --git a/pkgs/record_use/test_data/json/nested.json b/pkgs/record_use/test_data/json/nested.json index bc9a309..486ad81 100644 --- a/pkgs/record_use/test_data/json/nested.json +++ b/pkgs/record_use/test_data/json/nested.json
@@ -6,6 +6,7 @@ "value": 42 }, { + "definition_index": 0, "type": "instance", "value": { "i": 0 @@ -16,12 +17,14 @@ "value": "test" }, { + "definition_index": 0, "type": "instance", "value": { "i": 2 } }, { + "definition_index": 1, "type": "instance" } ],
diff --git a/pkgs/record_use/test_data/json/nested_instance_constant.json b/pkgs/record_use/test_data/json/nested_instance_constant.json index abd0271..06873fd 100644 --- a/pkgs/record_use/test_data/json/nested_instance_constant.json +++ b/pkgs/record_use/test_data/json/nested_instance_constant.json
@@ -6,6 +6,7 @@ "value": "id" }, { + "definition_index": 0, "type": "instance", "value": { "id": 0
diff --git a/pkgs/record_use/test_data/json/record_enum.json b/pkgs/record_use/test_data/json/record_enum.json index c648305..94dfc49 100644 --- a/pkgs/record_use/test_data/json/record_enum.json +++ b/pkgs/record_use/test_data/json/record_enum.json
@@ -10,6 +10,7 @@ "value": "a" }, { + "definition_index": 0, "type": "instance", "value": { "index": 0, @@ -17,6 +18,7 @@ } }, { + "definition_index": 0, "type": "instance", "value": { "a": 2
diff --git a/pkgs/record_use/test_data/json/record_instance_constant_empty.json b/pkgs/record_use/test_data/json/record_instance_constant_empty.json index 1c27859..52c2f60 100644 --- a/pkgs/record_use/test_data/json/record_instance_constant_empty.json +++ b/pkgs/record_use/test_data/json/record_instance_constant_empty.json
@@ -2,9 +2,11 @@ "$schema": "../../doc/schema/record_use.schema.json", "constants": [ { + "definition_index": 0, "type": "instance" }, { + "definition_index": 0, "type": "instance", "value": { "a": 0
diff --git a/pkgs/record_use/test_data/json/recorded_uses.json b/pkgs/record_use/test_data/json/recorded_uses.json index 1c6fb88..50112b8 100644 --- a/pkgs/record_use/test_data/json/recorded_uses.json +++ b/pkgs/record_use/test_data/json/recorded_uses.json
@@ -30,6 +30,7 @@ "value": 42 }, { + "definition_index": 1, "type": "instance", "value": { "i": 5
diff --git a/pkgs/record_use/test_data/json/unsupported_instance.json b/pkgs/record_use/test_data/json/unsupported_instance.json index 5d4f9a7..311b49f 100644 --- a/pkgs/record_use/test_data/json/unsupported_instance.json +++ b/pkgs/record_use/test_data/json/unsupported_instance.json
@@ -6,6 +6,7 @@ "type": "unsupported" }, { + "definition_index": 1, "type": "instance", "value": { "field": 0