Skip to content

Commit 99ec517

Browse files
authored
[json_syntax_generator] Make JsonReader private (#2446)
Addressing #2445 (comment) We can't make `JsonObjectSyntax` private, we use it as the type for `Map<String, Object?>` to enable overriding fields with this type with a class extending `JsonObjectSyntax` in subclasses. ```dart AssetSyntax({required JsonObjectSyntax? encoding, required String type}) ``` And then `DataAssetEncodingSyntax` is a valid type to be passed to `encoding` ```dart class DataAssetEncodingSyntax extends _JsonObjectSyntax { ``` It would be cleaner if the runtime library would be a standalone package, and all the generated files would import that. But that requires us to publish that library on pub.dev for rolling hooks_runner into flutter_tools. So maybe the simplest solution is to keep it as is for now. (If we ever decide to make the syntax generator a fully supported project we can do it the clean way.) WDYT?
1 parent 839330b commit 99ec517

File tree

8 files changed

+25
-23
lines changed

8 files changed

+25
-23
lines changed

pkgs/code_assets/lib/src/code_assets/syntax.g.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ class CodeConfigSyntax extends JsonObjectSyntax {
355355
if (objectErrors.isEmpty) {
356356
final jsonValue = _reader.get<Map<String, Object?>?>('c_compiler');
357357
if (jsonValue != null) {
358-
final reader = JsonReader(jsonValue, [...path, 'c_compiler']);
358+
final reader = _JsonReader(jsonValue, [...path, 'c_compiler']);
359359
result.addAll(reader.validate<Object>('windows'));
360360
}
361361
}
@@ -964,7 +964,7 @@ class JsonObjectSyntax {
964964

965965
final List<Object> path;
966966

967-
JsonReader get _reader => JsonReader(json, path);
967+
_JsonReader get _reader => _JsonReader(json, path);
968968

969969
JsonObjectSyntax() : json = {}, path = const [];
970970

@@ -973,7 +973,7 @@ class JsonObjectSyntax {
973973
List<String> validate() => [];
974974
}
975975

976-
class JsonReader {
976+
class _JsonReader {
977977
/// The JSON Object this reader is reading.
978978
final Map<String, Object?> json;
979979

@@ -984,7 +984,7 @@ class JsonReader {
984984
/// This is used to give more precise error messages.
985985
final List<Object> path;
986986

987-
JsonReader(this.json, this.path);
987+
_JsonReader(this.json, this.path);
988988

989989
T get<T extends Object?>(String key) {
990990
final value = json[key];

pkgs/data_assets/lib/src/data_assets/syntax.g.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class JsonObjectSyntax {
153153

154154
final List<Object> path;
155155

156-
JsonReader get _reader => JsonReader(json, path);
156+
_JsonReader get _reader => _JsonReader(json, path);
157157

158158
JsonObjectSyntax() : json = {}, path = const [];
159159

@@ -162,7 +162,7 @@ class JsonObjectSyntax {
162162
List<String> validate() => [];
163163
}
164164

165-
class JsonReader {
165+
class _JsonReader {
166166
/// The JSON Object this reader is reading.
167167
final Map<String, Object?> json;
168168

@@ -173,7 +173,7 @@ class JsonReader {
173173
/// This is used to give more precise error messages.
174174
final List<Object> path;
175175

176-
JsonReader(this.json, this.path);
176+
_JsonReader(this.json, this.path);
177177

178178
T get<T extends Object?>(String key) {
179179
final value = json[key];

pkgs/hooks/lib/src/hooks/syntax.g.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class AssetSyntax extends JsonObjectSyntax {
7373
if (objectErrors.isEmpty) {
7474
final jsonValue = _reader.get<Map<String, Object?>?>('encoding');
7575
if (jsonValue != null) {
76-
final reader = JsonReader(jsonValue, [...path, 'encoding']);
76+
final reader = _JsonReader(jsonValue, [...path, 'encoding']);
7777
result.addAll(reader.validate<Object>('key'));
7878
}
7979
}
@@ -1016,7 +1016,7 @@ class JsonObjectSyntax {
10161016

10171017
final List<Object> path;
10181018

1019-
JsonReader get _reader => JsonReader(json, path);
1019+
_JsonReader get _reader => _JsonReader(json, path);
10201020

10211021
JsonObjectSyntax() : json = {}, path = const [];
10221022

@@ -1025,7 +1025,7 @@ class JsonObjectSyntax {
10251025
List<String> validate() => [];
10261026
}
10271027

1028-
class JsonReader {
1028+
class _JsonReader {
10291029
/// The JSON Object this reader is reading.
10301030
final Map<String, Object?> json;
10311031

@@ -1036,7 +1036,7 @@ class JsonReader {
10361036
/// This is used to give more precise error messages.
10371037
final List<Object> path;
10381038

1039-
JsonReader(this.json, this.path);
1039+
_JsonReader(this.json, this.path);
10401040

10411041
T get<T extends Object?>(String key) {
10421042
final value = json[key];

pkgs/json_syntax_generator/lib/src/generator/helper_library.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
/// Helper methods for the code generator that are added to the generated file.
66
///
77
/// This simplifies the code generator.
8+
// TODO(https://github.com/dart-lang/native/issues/2447): At some time, move
9+
// this to a helper package to avoid copying.
810
const helperLib = r'''
911
class JsonObjectSyntax {
1012
final Map<String, Object?> json;
1113
1214
final List<Object> path;
1315
14-
JsonReader get _reader => JsonReader(json, path);
16+
_JsonReader get _reader => _JsonReader(json, path);
1517
1618
JsonObjectSyntax() : json = {}, path = const [];
1719
@@ -20,7 +22,7 @@ class JsonObjectSyntax {
2022
List<String> validate() => [];
2123
}
2224
23-
class JsonReader {
25+
class _JsonReader {
2426
/// The JSON Object this reader is reading.
2527
final Map<String, Object?> json;
2628
@@ -31,7 +33,7 @@ class JsonReader {
3133
/// This is used to give more precise error messages.
3234
final List<Object> path;
3335
34-
JsonReader(this.json, this.path);
36+
_JsonReader(this.json, this.path);
3537
3638
T get<T extends Object?>(String key) {
3739
final value = json[key];

pkgs/json_syntax_generator/lib/src/generator/normal_class_generator.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ static const ${tagProperty}Value = '$tagValue';
334334
if (objectErrors.isEmpty) {
335335
final jsonValue = _reader.get<Map<String, Object?>?>('$jsonKey0');
336336
if (jsonValue != null) {
337-
final reader = JsonReader(jsonValue, [...path, '$jsonKey0']);
337+
final reader = _JsonReader(jsonValue, [...path, '$jsonKey0']);
338338
result.addAll(reader.validate<Object>('$jsonKey1'));
339339
}
340340
}

pkgs/pub_formats/lib/pubspec_formats.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
/// SDK, so when doing breaking changes please roll ASAP to the Dart SDK.
99
library;
1010

11-
export 'src/pubspec_syntax.g.dart' hide JsonObjectSyntax, JsonReader;
12-
export 'src/pubspec_lock_syntax.g.dart' hide JsonObjectSyntax, JsonReader;
11+
export 'src/pubspec_syntax.g.dart' hide JsonObjectSyntax;
12+
export 'src/pubspec_lock_syntax.g.dart' hide JsonObjectSyntax;

pkgs/pub_formats/lib/src/pubspec_lock_syntax.g.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ class JsonObjectSyntax {
514514

515515
final List<Object> path;
516516

517-
JsonReader get _reader => JsonReader(json, path);
517+
_JsonReader get _reader => _JsonReader(json, path);
518518

519519
JsonObjectSyntax() : json = {}, path = const [];
520520

@@ -523,7 +523,7 @@ class JsonObjectSyntax {
523523
List<String> validate() => [];
524524
}
525525

526-
class JsonReader {
526+
class _JsonReader {
527527
/// The JSON Object this reader is reading.
528528
final Map<String, Object?> json;
529529

@@ -534,7 +534,7 @@ class JsonReader {
534534
/// This is used to give more precise error messages.
535535
final List<Object> path;
536536

537-
JsonReader(this.json, this.path);
537+
_JsonReader(this.json, this.path);
538538

539539
T get<T extends Object?>(String key) {
540540
final value = json[key];

pkgs/pub_formats/lib/src/pubspec_syntax.g.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ class JsonObjectSyntax {
705705

706706
final List<Object> path;
707707

708-
JsonReader get _reader => JsonReader(json, path);
708+
_JsonReader get _reader => _JsonReader(json, path);
709709

710710
JsonObjectSyntax() : json = {}, path = const [];
711711

@@ -714,7 +714,7 @@ class JsonObjectSyntax {
714714
List<String> validate() => [];
715715
}
716716

717-
class JsonReader {
717+
class _JsonReader {
718718
/// The JSON Object this reader is reading.
719719
final Map<String, Object?> json;
720720

@@ -725,7 +725,7 @@ class JsonReader {
725725
/// This is used to give more precise error messages.
726726
final List<Object> path;
727727

728-
JsonReader(this.json, this.path);
728+
_JsonReader(this.json, this.path);
729729

730730
T get<T extends Object?>(String key) {
731731
final value = json[key];

0 commit comments

Comments
 (0)