Skip to content

Commit 9ebc083

Browse files
authored
[record_use] Update MapConstant to support non-string keys (dart-lang#3037)
Bug: dart-lang#2715 Will require in implementation in the Dart SDK, and a manual roll due to breaking change in json format.
1 parent 4c78a12 commit 9ebc083

10 files changed

Lines changed: 358 additions & 79 deletions

File tree

‎pkgs/record_use/doc/schema/record_use.schema.json‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,10 @@
169169
"then": {
170170
"properties": {
171171
"value": {
172-
"type": "object",
173-
"additionalProperties": true
172+
"type": "array",
173+
"items": {
174+
"$ref": "#/definitions/MapEntry"
175+
}
174176
}
175177
},
176178
"required": [
@@ -268,6 +270,21 @@
268270
"uri"
269271
]
270272
},
273+
"MapEntry": {
274+
"type": "object",
275+
"properties": {
276+
"key": {
277+
"type": "integer"
278+
},
279+
"value": {
280+
"type": "integer"
281+
}
282+
},
283+
"required": [
284+
"key",
285+
"value"
286+
]
287+
},
271288
"RecordedUses": {
272289
"type": "object",
273290
"properties": {

‎pkgs/record_use/lib/src/constant.dart‎

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ sealed class Constant {
2424
NullConstant() => null,
2525
final PrimitiveConstant p => p.value,
2626
final ListConstant<Constant> l => l.value.map((c) => c.toValue()).toList(),
27-
final MapConstant<Constant> m => m.value.map(
28-
(key, value) => MapEntry(key, value.toValue()),
27+
final MapConstant<Constant, Constant> m => Map.fromEntries(
28+
m.entries.map(
29+
(entry) => MapEntry(entry.key.toValue(), entry.value.toValue()),
30+
),
2931
),
3032
final InstanceConstant i => i.fields.map(
3133
(key, value) => MapEntry(key, value.toValue()),
@@ -45,7 +47,14 @@ sealed class Constant {
4547
value!.cast<int>().map((i) => constants[i]).toList(),
4648
),
4749
MapConstantSyntax(:final value) => MapConstant(
48-
value.json.map((key, value) => MapEntry(key, constants[value as int])),
50+
value
51+
.map(
52+
(e) => MapEntry(
53+
constants[e.key],
54+
constants[e.value],
55+
),
56+
)
57+
.toList(),
4958
),
5059
InstanceConstantSyntax(value: final value) => InstanceConstant(
5160
fields: (value?.json ?? {}).map(
@@ -152,30 +161,46 @@ final class ListConstant<T extends Constant> extends Constant {
152161
);
153162
}
154163

155-
/// Represents a constant map from string keys to [Constant] values.
156-
final class MapConstant<T extends Constant> extends Constant {
164+
/// Represents a constant map from [Constant] keys to [Constant] values.
165+
final class MapConstant<K extends Constant, V extends Constant>
166+
extends Constant {
157167
/// The underlying map of constant values.
158-
final Map<String, T> value;
168+
final List<MapEntry<K, V>> entries;
159169

160-
/// Creates a [MapConstant] object with the given map of [value]s.
161-
const MapConstant(this.value);
170+
/// Creates a [MapConstant] object with the given map of entries.
171+
const MapConstant(this.entries);
162172

163173
@override
164-
int get hashCode => deepHash(value);
174+
int get hashCode => Object.hashAll(
175+
entries.map((e) => Object.hash(e.key, e.value)),
176+
);
165177

166178
@override
167179
bool operator ==(Object other) {
168180
if (identical(this, other)) return true;
169181

170-
return other is MapConstant && deepEquals(other.value, value);
182+
if (other is! MapConstant) return false;
183+
if (other.entries.length != entries.length) return false;
184+
for (var i = 0; i < entries.length; i++) {
185+
if (entries[i].key != other.entries[i].key ||
186+
entries[i].value != other.entries[i].value) {
187+
return false;
188+
}
189+
}
190+
return true;
171191
}
172192

173193
@override
174194
MapConstantSyntax _toSyntax(Map<Constant, int> constants) =>
175195
MapConstantSyntax(
176-
value: JsonObjectSyntax.fromJson(
177-
value.map((key, constant) => MapEntry(key, constants[constant]!)),
178-
),
196+
value: entries
197+
.map(
198+
(entry) => MapEntrySyntax(
199+
key: constants[entry.key]!,
200+
value: constants[entry.value]!,
201+
),
202+
)
203+
.toList(),
179204
);
180205
}
181206

‎pkgs/record_use/lib/src/recordings.dart‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,9 @@ extension FlattenConstantsExtension on Iterable<Constant> {
446446
void depthFirstSearch(Constant constant, Set<Constant> collected) {
447447
final children = switch (constant) {
448448
ListConstant<Constant>() => constant.value,
449-
MapConstant<Constant>() => constant.value.values,
449+
MapConstant<Constant, Constant>() => constant.entries.expand(
450+
(e) => [e.key, e.value],
451+
),
450452
InstanceConstant() => constant.fields.values,
451453
_ => <Constant>[],
452454
};

‎pkgs/record_use/lib/src/syntax.g.dart‎

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -531,34 +531,45 @@ class MapConstantSyntax extends ConstantSyntax {
531531
super.path,
532532
}) : super._fromJson();
533533

534-
MapConstantSyntax({required JsonObjectSyntax value, super.path = const []})
535-
: super(type: 'map') {
534+
MapConstantSyntax({
535+
required List<MapEntrySyntax> value,
536+
super.path = const [],
537+
}) : super(type: 'map') {
536538
_value = value;
537539
json.sortOnKey();
538540
}
539541

540542
/// Setup all fields for [MapConstantSyntax] that are not in
541543
/// [ConstantSyntax].
542-
void setup({required JsonObjectSyntax value}) {
544+
void setup({required List<MapEntrySyntax> value}) {
543545
_value = value;
544546
json.sortOnKey();
545547
}
546548

547-
JsonObjectSyntax get value {
548-
final jsonValue = _reader.map$('value');
549-
return JsonObjectSyntax.fromJson(jsonValue, path: [...path, 'value']);
549+
List<MapEntrySyntax> get value {
550+
final jsonValue = _reader.list('value');
551+
return [
552+
for (final (index, element) in jsonValue.indexed)
553+
MapEntrySyntax.fromJson(
554+
element as Map<String, Object?>,
555+
path: [...path, 'value', index],
556+
),
557+
];
550558
}
551559

552-
set _value(JsonObjectSyntax value) {
553-
json['value'] = value.json;
560+
set _value(List<MapEntrySyntax> value) {
561+
json['value'] = [for (final item in value) item.json];
554562
}
555563

556564
List<String> _validateValue() {
557-
final mapErrors = _reader.validate<Map<String, Object?>>('value');
558-
if (mapErrors.isNotEmpty) {
559-
return mapErrors;
565+
final listErrors = _reader.validateList<Map<String, Object?>>(
566+
'value',
567+
);
568+
if (listErrors.isNotEmpty) {
569+
return listErrors;
560570
}
561-
return value.validate();
571+
final elements = value;
572+
return [for (final element in elements) ...element.validate()];
562573
}
563574

564575
@override
@@ -575,6 +586,46 @@ extension MapConstantSyntaxExtension on ConstantSyntax {
575586
MapConstantSyntax.fromJson(json, path: path);
576587
}
577588

589+
class MapEntrySyntax extends JsonObjectSyntax {
590+
MapEntrySyntax.fromJson(
591+
super.json, {
592+
super.path = const [],
593+
}) : super.fromJson();
594+
595+
MapEntrySyntax({required int key, required int value, super.path = const []})
596+
: super() {
597+
_key = key;
598+
_value = value;
599+
json.sortOnKey();
600+
}
601+
602+
int get key => _reader.get<int>('key');
603+
604+
set _key(int value) {
605+
json.setOrRemove('key', value);
606+
}
607+
608+
List<String> _validateKey() => _reader.validate<int>('key');
609+
610+
int get value => _reader.get<int>('value');
611+
612+
set _value(int value) {
613+
json.setOrRemove('value', value);
614+
}
615+
616+
List<String> _validateValue() => _reader.validate<int>('value');
617+
618+
@override
619+
List<String> validate() => [
620+
...super.validate(),
621+
..._validateKey(),
622+
..._validateValue(),
623+
];
624+
625+
@override
626+
String toString() => 'MapEntrySyntax($json)';
627+
}
628+
578629
class MetadataSyntax extends JsonObjectSyntax {
579630
MetadataSyntax.fromJson(
580631
super.json, {
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:pub_semver/pub_semver.dart';
6+
import 'package:record_use/record_use_internal.dart';
7+
import 'package:test/test.dart';
8+
9+
void main() {
10+
test('MapConstant with InstanceConstant keys round-trip', () {
11+
const instanceKey = InstanceConstant(
12+
fields: {
13+
'id': IntConstant(1),
14+
'tag': StringConstant('key'),
15+
},
16+
);
17+
18+
const mapConstant = MapConstant([
19+
MapEntry(instanceKey, StringConstant('value')),
20+
]);
21+
22+
const definition = Definition(
23+
identifier: Identifier(
24+
importUri: 'package:test/test.dart',
25+
name: 'testMethod',
26+
),
27+
);
28+
29+
final recordings = Recordings(
30+
metadata: Metadata(
31+
version: Version(1, 0, 0),
32+
comment: 'Test complex keys',
33+
),
34+
callsForDefinition: {
35+
definition: [
36+
const CallWithArguments(
37+
positionalArguments: [mapConstant],
38+
namedArguments: {},
39+
loadingUnit: 'main.js',
40+
location: Location(uri: 'main.dart', line: 1, column: 1),
41+
),
42+
],
43+
},
44+
instancesForDefinition: {},
45+
);
46+
47+
final json = recordings.toJson();
48+
final backAgain = Recordings.fromJson(json);
49+
50+
expect(backAgain, recordings);
51+
});
52+
53+
test('toValue() with InstanceConstant keys', () {
54+
const instanceKey = InstanceConstant(
55+
fields: {
56+
'id': IntConstant(1),
57+
'tag': StringConstant('key'),
58+
},
59+
);
60+
61+
const mapConstant = MapConstant([
62+
MapEntry(instanceKey, StringConstant('value')),
63+
]);
64+
65+
final mapValue = mapConstant.toValue() as Map;
66+
67+
expect(mapValue.keys.first, {
68+
'id': 1,
69+
'tag': 'key',
70+
});
71+
expect(mapValue.values.first, 'value');
72+
});
73+
74+
test('Deeply nested MapConstant with complex keys round-trip', () {
75+
const listKey = ListConstant([IntConstant(1), IntConstant(2)]);
76+
const mapKey = MapConstant([
77+
MapEntry(StringConstant('inner'), IntConstant(3)),
78+
]);
79+
80+
const complexMap = MapConstant([
81+
MapEntry(listKey, mapKey),
82+
MapEntry(mapKey, listKey),
83+
]);
84+
85+
const definition = Definition(
86+
identifier: Identifier(
87+
importUri: 'package:test/test.dart',
88+
name: 'complexMethod',
89+
),
90+
);
91+
92+
final recordings = Recordings(
93+
metadata: Metadata(
94+
version: Version(1, 0, 0),
95+
comment: 'Test deeply nested complex keys',
96+
),
97+
callsForDefinition: {
98+
definition: [
99+
const CallWithArguments(
100+
positionalArguments: [complexMap],
101+
namedArguments: {},
102+
loadingUnit: 'main.js',
103+
location: Location(uri: 'main.dart', line: 1, column: 1),
104+
),
105+
],
106+
},
107+
instancesForDefinition: {},
108+
);
109+
110+
final json = recordings.toJson();
111+
final backAgain = Recordings.fromJson(json);
112+
113+
expect(backAgain, recordings);
114+
});
115+
116+
test('toValue() with deeply nested complex keys', () {
117+
const listKey = ListConstant([IntConstant(1), IntConstant(2)]);
118+
const mapKey = MapConstant([
119+
MapEntry(StringConstant('inner'), IntConstant(3)),
120+
]);
121+
122+
const complexMap = MapConstant([
123+
MapEntry(listKey, mapKey),
124+
MapEntry(mapKey, listKey),
125+
]);
126+
127+
final mapValue = complexMap.toValue() as Map;
128+
expect(mapValue.length, 2);
129+
final entries = mapValue.entries.toList();
130+
expect(entries[0].key, [1, 2]);
131+
expect(entries[0].value, {'inner': 3});
132+
expect(entries[1].key, {'inner': 3});
133+
expect(entries[1].value, [1, 2]);
134+
});
135+
}

‎pkgs/record_use/test/json_schema/schema_test.dart‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,22 @@ void main() {
3535
}
3636
}
3737

38-
const constNullIndex = 3;
39-
const constInstanceIndex = 5;
38+
const constNullIndex = 4;
39+
const constInstanceIndex = 6;
40+
const constMapIndex = 3;
4041
List<(List<Object>, void Function(ValidationResults result))>
4142
recordUseFields = [
4243
(['constants'], expectOptionalFieldMissing),
43-
for (var index = 0; index < 7; index++) ...[
44+
for (var index = 0; index < 8; index++) ...[
4445
(['constants', index, 'type'], expectRequiredFieldMissing),
4546
if (index != constNullIndex && index != constInstanceIndex)
4647
(['constants', index, 'value'], expectRequiredFieldMissing),
4748
if (index == constInstanceIndex)
4849
(['constants', index, 'value'], expectOptionalFieldMissing),
50+
if (index == constMapIndex) ...[
51+
(['constants', index, 'value', 0, 'key'], expectRequiredFieldMissing),
52+
(['constants', index, 'value', 0, 'value'], expectRequiredFieldMissing),
53+
],
4954
// Note the value for 'Instance' is optional because an empty map is
5055
// omitted. Also, Null has no value field.
5156
],

0 commit comments

Comments
 (0)