Skip to content

Commit b98b5b4

Browse files
authored
[native_assets_cli] Expose tagged union tag in syntax (#2141)
Bug: #2132 Take the `Asset.type`s from the syntax. (This will prevent any misspellings when using `EncodedAsset.type` as a String in `native_assets_cli`.)
1 parent 875d06e commit b98b5b4

File tree

8 files changed

+44
-2
lines changed

8 files changed

+44
-2
lines changed

pkgs/hooks/tool/generate_syntax.dart

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ void main(List<String> args) {
4545
'HookOutput',
4646
'LinkOutput',
4747
],
48+
visbleUnionTagValues: ['Asset'],
4849
).analyze();
4950
final textDumpFile = File.fromUri(
5051
packageUri.resolve(

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

+11
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class ClassGenerator {
2727
buffer.writeln('''
2828
class $className $extendsString {
2929
''');
30+
buffer.writeln(_generateTag());
3031
buffer.writeln(_generateFields());
3132
buffer.writeln(_generateJsonFactory());
3233
buffer.writeln(_generateJsonConstructor());
@@ -41,6 +42,16 @@ class $className $extendsString {
4142
''');
4243
}
4344

45+
/// If this is a tagged union value, expose the tag.
46+
String _generateTag() {
47+
if (classInfo.superclass?.visibleTaggedUnion != true) return '';
48+
final tagProperty = classInfo.superclass!.taggedUnionProperty;
49+
final tagValue = classInfo.taggedUnionValue;
50+
return '''
51+
static const ${tagProperty}Value = '$tagValue';
52+
''';
53+
}
54+
4455
String _generateFields() {
4556
if (classInfo.superclass != null) {
4657
// The super class already has the required fields.

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

+7
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class NormalClassInfo extends ClassInfo {
4040
/// Only set in the sub classes.
4141
final String? taggedUnionValue;
4242

43+
/// If the tagged union tags are available in the generated API.
44+
///
45+
/// Only set in the parent class.
46+
final bool visibleTaggedUnion;
47+
4348
bool get isTaggedUnion =>
4449
taggedUnionProperty != null || taggedUnionValue != null;
4550

@@ -52,6 +57,7 @@ class NormalClassInfo extends ClassInfo {
5257
this.taggedUnionProperty,
5358
this.taggedUnionValue,
5459
this.extraValidation = const [],
60+
this.visibleTaggedUnion = false,
5561
}) : super() {
5662
superclass?.subclasses.add(this);
5763
if (taggedUnionValue != null) {
@@ -77,6 +83,7 @@ $propertiesString
7783
],
7884
taggedUnionProperty: $taggedUnionProperty,
7985
taggedUnionValue: $taggedUnionValue,
86+
visibleTaggedUnion: $visibleTaggedUnion,
8087
extraValidation: [
8188
$extraValidationString
8289
],

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

+19
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,29 @@ class SchemaAnalyzer {
4444
/// Generate public setters for these class names.
4545
final List<String> publicSetters;
4646

47+
/// For subtypes of these classes, the union tag values are exposed.
48+
///
49+
/// For example, if `Asset.type` is `NativeCodeAsset.typeValue`, then the
50+
/// asset is a native code asset. Listing `Asset` in [visbleUnionTagValues]
51+
/// will add `static const typeValue = 'native_code';`:
52+
///
53+
/// ```dart
54+
/// class NativeCodeAsset extends Asset {
55+
/// static const typeValue = 'native_code';
56+
/// }
57+
///
58+
/// class Asset {
59+
/// String? get type => _reader.get<String?>('type');
60+
/// }
61+
/// ```
62+
final List<String> visbleUnionTagValues;
63+
4764
SchemaAnalyzer(
4865
this.schema, {
4966
this.capitalizationOverrides = const {},
5067
this.classSorting,
5168
this.publicSetters = const [],
69+
this.visbleUnionTagValues = const [],
5270
});
5371

5472
/// Accumulator for all classes during the analysis.
@@ -164,6 +182,7 @@ class SchemaAnalyzer {
164182
taggedUnionValue: taggedUnionValue,
165183
taggedUnionProperty:
166184
schemas.generateSubClasses ? properties.single.name : null,
185+
visibleTaggedUnion: visbleUnionTagValues.contains(typeName),
167186
extraValidation: extraValidation,
168187
);
169188
_classes[typeName] = classInfo;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ extension CodeAssetType on CodeAsset {
171171
// TODO(https://github.com/dart-lang/native/issues/2132): Change to be
172172
// namespaced by package name. (We'll temporarily need to support both the
173173
// old a new type.)
174-
static const String type = 'native_code';
174+
static const String type = syntax.NativeCodeAsset.typeValue;
175175
}
176176

177177
extension EncodedCodeAsset on EncodedAsset {

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

+2
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,8 @@ class MacOSCodeConfig {
815815
}
816816

817817
class NativeCodeAsset extends Asset {
818+
static const typeValue = 'native_code';
819+
818820
NativeCodeAsset.fromJson(super.json, {super.path}) : super._fromJson();
819821

820822
NativeCodeAsset({

pkgs/native_assets_cli/lib/src/data_assets/data_asset.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ extension DataAssetType on DataAsset {
8484
// TODO(https://github.com/dart-lang/native/issues/2132): Change to be
8585
// namespaced by package name. (We'll temporarily need to support both the
8686
// old a new type.)
87-
static const String type = 'data';
87+
static const String type = syntax.DataAsset.typeValue;
8888
}
8989

9090
extension EncodedDataAsset on EncodedAsset {

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

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class Asset {
5050
}
5151

5252
class DataAsset extends Asset {
53+
static const typeValue = 'data';
54+
5355
DataAsset.fromJson(super.json, {super.path}) : super._fromJson();
5456

5557
DataAsset({

0 commit comments

Comments
 (0)