Skip to content

Commit 05cf603

Browse files
authored
[native_assets_cli] Introduce JsonObject base class for syntax (#2159)
1 parent 8c39c95 commit 05cf603

File tree

10 files changed

+232
-246
lines changed

10 files changed

+232
-246
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@
66
///
77
/// This simplifies the code generator.
88
const helperLib = r'''
9+
class JsonObject {
10+
final Map<String, Object?> json;
11+
12+
final List<Object> path;
13+
14+
JsonReader get _reader => JsonReader(json, path);
15+
16+
JsonObject() : json = {}, path = const [];
17+
18+
JsonObject.fromJson(this.json, {this.path = const []});
19+
20+
List<String> validate() => [];
21+
}
22+
923
class JsonReader {
1024
/// The JSON Object this reader is reading.
1125
final Map<String, Object?> json;

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

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,12 @@ class ClassGenerator {
2020

2121
void _generateClass(StringBuffer buffer) {
2222
final className = classInfo.name;
23-
final superclassName = classInfo.superclass?.name;
23+
final superclassName = classInfo.superclass?.name ?? 'JsonObject';
2424

25-
final extendsString =
26-
superclassName != null ? 'extends $superclassName' : '';
2725
buffer.writeln('''
28-
class $className $extendsString {
26+
class $className extends $superclassName {
2927
''');
3028
buffer.writeln(_generateTag());
31-
buffer.writeln(_generateFields());
3229
buffer.writeln(_generateJsonFactory());
3330
buffer.writeln(_generateJsonConstructor());
3431
buffer.writeln(_generateDefaultConstructor());
@@ -52,21 +49,6 @@ static const ${tagProperty}Value = '$tagValue';
5249
''';
5350
}
5451

55-
String _generateFields() {
56-
if (classInfo.superclass != null) {
57-
// The super class already has the required fields.
58-
return '';
59-
}
60-
61-
return '''
62-
final Map<String, Object?> json;
63-
64-
final List<Object> path;
65-
66-
JsonReader get _reader => JsonReader(json, path);
67-
''';
68-
}
69-
7052
/// If this is the parent class in a tagged union, generate a factory that
7153
/// branches to invoke subclass constructors.
7254
String _generateJsonFactory() {
@@ -106,9 +88,9 @@ static const ${tagProperty}Value = '$tagValue';
10688
final constructorName =
10789
classInfo.isTaggedUnion ? '_fromJson' : 'fromJson';
10890
return '''
109-
$className.$constructorName(this.json, {
110-
this.path = const [],
111-
});
91+
$className.$constructorName(super.json, {
92+
super.path = const [],
93+
}) : super.fromJson();
11294
''';
11395
}
11496

@@ -133,8 +115,7 @@ static const ${tagProperty}Value = '$tagValue';
133115
if (classInfo.superclass == null) {
134116
return '''
135117
$className($parametersString)
136-
: json = {},
137-
path = const []
118+
: super()
138119
$body
139120
''';
140121
}
@@ -260,14 +241,6 @@ static const ${tagProperty}Value = '$tagValue';
260241
];
261242
final validateCallsString = validateCalls.join(',\n');
262243

263-
if (classInfo.superclass == null) {
264-
return '''
265-
List<String> validate() => [
266-
$validateCallsString
267-
];
268-
''';
269-
}
270-
271244
return '''
272245
@override
273246
List<String> validate() => [

pkgs/json_syntax_generator/lib/src/model/dart_type.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ class ClassDartType extends DartType {
5555
String toNonNullableString() => classInfo.name;
5656
}
5757

58+
/// The [ClassInfo] for the `JsonObject` base class.
59+
final jsonObjectClassInfo = NormalClassInfo(name: 'JsonObject', properties: []);
60+
5861
class ListDartType extends DartType {
5962
final DartType itemType;
6063

pkgs/json_syntax_generator/lib/src/parser/schema_analyzer.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,8 @@ class SchemaAnalyzer {
365365
'Expected an object with arbitrary properties.',
366366
);
367367
}
368-
dartType = MapDartType(
369-
valueType: const ObjectDartType(isNullable: true),
368+
dartType = ClassDartType(
369+
classInfo: jsonObjectClassInfo,
370370
isNullable: !required,
371371
);
372372
default:

pkgs/native_assets_cli/lib/src/code_assets/config.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ extension CodeAssetBuildInputBuilder on HookConfigBuilder {
206206
macOS: macOS?.toSyntax(),
207207
);
208208
final baseHookConfig = hook_syntax.HookInput.fromJson(builder.json).config;
209-
baseHookConfig.extensions ??= {};
209+
baseHookConfig.extensions ??= hook_syntax.JsonObject.fromJson({});
210210
final hookConfig = syntax.Config.fromJson(baseHookConfig.json);
211211
hookConfig.extensions!.codeAssets = codeConfig;
212212
hookConfig.code = codeConfig; // old location

0 commit comments

Comments
 (0)