Skip to content

Commit f56ada5

Browse files
committed
[native_assets_cli] Expose tagged union tag
1 parent e8b7b05 commit f56ada5

File tree

8 files changed

+30
-2
lines changed

8 files changed

+30
-2
lines changed

pkgs/hooks/tool/generate_syntax.dart

Lines changed: 1 addition & 0 deletions
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

Lines changed: 11 additions & 0 deletions
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,15 @@ 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+
final List<String> visbleUnionTagValues;
49+
4750
SchemaAnalyzer(
4851
this.schema, {
4952
this.capitalizationOverrides = const {},
5053
this.classSorting,
5154
this.publicSetters = const [],
55+
this.visbleUnionTagValues = const [],
5256
});
5357

5458
/// Accumulator for all classes during the analysis.
@@ -164,6 +168,7 @@ class SchemaAnalyzer {
164168
taggedUnionValue: taggedUnionValue,
165169
taggedUnionProperty:
166170
schemas.generateSubClasses ? properties.single.name : null,
171+
visibleTaggedUnion: visbleUnionTagValues.contains(typeName),
167172
extraValidation: extraValidation,
168173
);
169174
_classes[typeName] = classInfo;

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 0 deletions
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)