Skip to content

Commit

Permalink
fix up tests, generator, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
jakemac53 committed Oct 17, 2024
1 parent e94930d commit 3ffffc5
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 54 deletions.
16 changes: 8 additions & 8 deletions pkgs/_analyzer_macros/lib/macro_implementation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ class AnalyzerMacroExecutionResult
static Future<AnalyzerMacroExecutionResult> dartModelToInjected(
macros_api_v1.MacroTarget target, AugmentResponse augmentResponse) async {
final declarations = <macros_api_v1.DeclarationCode>[];
if (augmentResponse.typeAugmentations.isNotEmpty) {
if (augmentResponse.typeAugmentations?.isNotEmpty == true) {
// TODO: Handle multiple type augmentations, or augmentations where the
// target is itself a member of a type and not the type.
final entry = augmentResponse.typeAugmentations.entries.single;
final entry = augmentResponse.typeAugmentations!.entries.single;
if (entry.key != target.qualifiedName.name) {
throw UnimplementedError(
'Type augmentations are only implemented when the type is the '
Expand All @@ -158,16 +158,16 @@ class AnalyzerMacroExecutionResult
}
}

if (augmentResponse.enumValueAugmentations.isNotEmpty) {
if (augmentResponse.enumValueAugmentations?.isNotEmpty == true) {
throw UnimplementedError('Enum value augmentations are not implemented');
}
if (augmentResponse.extendsTypeAugmentations.isNotEmpty ||
augmentResponse.interfaceAugmentations.isNotEmpty ||
augmentResponse.mixinAugmentations.isNotEmpty) {
if (augmentResponse.extendsTypeAugmentations?.isNotEmpty == true ||
augmentResponse.interfaceAugmentations?.isNotEmpty == true ||
augmentResponse.mixinAugmentations?.isNotEmpty == true) {
throw UnimplementedError('Type augmentations are not implemented');
}
if (augmentResponse.libraryAugmentations.isNotEmpty ||
augmentResponse.newTypeNames.isNotEmpty) {
if (augmentResponse.libraryAugmentations?.isNotEmpty == true ||
augmentResponse.newTypeNames?.isNotEmpty == true) {
throw UnimplementedError('Library augmentations are not implemented');
}

Expand Down
16 changes: 8 additions & 8 deletions pkgs/_cfe_macros/lib/macro_implementation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ class CfeMacroExecutionResult implements macros_api_v1.MacroExecutionResult {
static Future<CfeMacroExecutionResult> dartModelToInjected(
macros_api_v1.MacroTarget target, AugmentResponse augmentResponse) async {
final declarations = <macros_api_v1.DeclarationCode>[];
if (augmentResponse.typeAugmentations.isNotEmpty) {
if (augmentResponse.typeAugmentations?.isNotEmpty == true) {
// TODO: Handle multiple type augmentations, or augmentations where the
// target is itself a member of a type and not the type.
final entry = augmentResponse.typeAugmentations.entries.single;
final entry = augmentResponse.typeAugmentations!.entries.single;
if (entry.key != target.qualifiedName.name) {
throw UnimplementedError(
'Type augmentations are only implemented when the type is the '
Expand All @@ -160,16 +160,16 @@ class CfeMacroExecutionResult implements macros_api_v1.MacroExecutionResult {
}
}

if (augmentResponse.enumValueAugmentations.isNotEmpty) {
if (augmentResponse.enumValueAugmentations?.isNotEmpty == true) {
throw UnimplementedError('Enum value augmentations are not implemented');
}
if (augmentResponse.extendsTypeAugmentations.isNotEmpty ||
augmentResponse.interfaceAugmentations.isNotEmpty ||
augmentResponse.mixinAugmentations.isNotEmpty) {
if (augmentResponse.extendsTypeAugmentations?.isNotEmpty == true ||
augmentResponse.interfaceAugmentations?.isNotEmpty == true ||
augmentResponse.mixinAugmentations?.isNotEmpty == true) {
throw UnimplementedError('Type augmentations are not implemented');
}
if (augmentResponse.libraryAugmentations.isNotEmpty ||
augmentResponse.newTypeNames.isNotEmpty) {
if (augmentResponse.libraryAugmentations?.isNotEmpty == true ||
augmentResponse.newTypeNames?.isNotEmpty == true) {
throw UnimplementedError('Library augmentations are not implemented');
}

Expand Down
4 changes: 2 additions & 2 deletions pkgs/_macro_host/test/macro_host_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void main() {
target: QualifiedName(
name: 'Foo', uri: 'package:foo/foo.dart'))),
Scope.macro.run(() => AugmentResponse()
..typeAugmentations['Foo'] = [
..typeAugmentations!['Foo'] = [
Augmentation(code: [Code.string('int get x => 3;')])
]));
});
Expand Down Expand Up @@ -69,7 +69,7 @@ void main() {
target: QualifiedName(
uri: 'package:foo/foo.dart', name: 'Foo'))),
Scope.macro.run(() => AugmentResponse()
..typeAugmentations['Foo'] = [
..typeAugmentations!['Foo'] = [
Augmentation(code: [
Code.string(
'// {"uris":{"package:foo/foo.dart":{"scopes":{"Foo":{'
Expand Down
2 changes: 1 addition & 1 deletion pkgs/_test_macros/lib/declare_x_macro.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class DeclareXImplementation implements ClassDeclarationsMacro {

// TODO(davidmorgan): still need to pass through the augment target.
return AugmentResponse()
..typeAugmentations[request.target.name] = [
..typeAugmentations![request.target.name] = [
Augmentation(code: expandTemplate('int get x => 3;'))
];
}
Expand Down
4 changes: 2 additions & 2 deletions pkgs/_test_macros/lib/json_codable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class JsonCodableImplementation
Host host, AugmentRequest request) async {
final target = request.target;
return AugmentResponse()
..typeAugmentations[request.target.name] = [
..typeAugmentations![request.target.name] = [
Augmentation(code: expandTemplate('''
// TODO(davidmorgan): see https://github.com/dart-lang/macros/issues/80.
// external ${target.name}.fromJson($_jsonMapType json);
Expand All @@ -51,7 +51,7 @@ class JsonCodableImplementation
final superclassName = MacroScope.current.typeSystem.supertypeOf(target);

return AugmentResponse()
..typeAugmentations[request.target.name] = [
..typeAugmentations![request.target.name] = [
await _generateFromJson(host, model, target, superclassName, clazz),
await _generateToJson(host, model, target, superclassName, clazz)
];
Expand Down
2 changes: 1 addition & 1 deletion pkgs/_test_macros/lib/query_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class QueryClassImplementation implements ClassDefinitionsMacro {
target: request.target,
));
return AugmentResponse()
..typeAugmentations[request.target.name] = [
..typeAugmentations![request.target.name] = [
Augmentation(code: expandTemplate('// ${json.encode(model)}'))
];
}
Expand Down
46 changes: 23 additions & 23 deletions pkgs/macro_service/lib/src/macro_service.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,47 +35,47 @@ extension type AugmentResponse.fromJson(Map<String, Object?> node)
List<Augmentation>? libraryAugmentations,
List<String>? newTypeNames,
}) : this.fromJson({
'enumValueAugmentations': {},
'extendsTypeAugmentations': {},
'interfaceAugmentations': {},
'enumValueAugmentations': <String, Object?>{},
'extendsTypeAugmentations': <String, Object?>{},
'interfaceAugmentations': <String, Object?>{},
if (libraryAugmentations != null)
'libraryAugmentations': libraryAugmentations,
'mixinAugmentations': {},
'mixinAugmentations': <String, Object?>{},
if (newTypeNames != null) 'newTypeNames': newTypeNames,
'typeAugmentations': {},
'typeAugmentations': <String, Object?>{},
});

/// Any augmentations to enum values that should be applied to an enum as a result of executing a macro, indexed by the name of the enum.
Map<String, List<Augmentation>> get enumValueAugmentations =>
(node['enumValueAugmentations'] as Map)
.deepCast<String, List<Augmentation>>((v) => (v as List).cast());
Map<String, List<Augmentation>>? get enumValueAugmentations =>
(node['enumValueAugmentations'] as Map?)
?.deepCast<String, List<Augmentation>>((v) => (v as List).cast());

/// Any extends clauses that should be added to types as a result of executing a macro, indexed by the name of the augmented type declaration.
Map<String, List<Augmentation>> get extendsTypeAugmentations =>
(node['extendsTypeAugmentations'] as Map)
.deepCast<String, List<Augmentation>>((v) => (v as List).cast());
Map<String, List<Augmentation>>? get extendsTypeAugmentations =>
(node['extendsTypeAugmentations'] as Map?)
?.deepCast<String, List<Augmentation>>((v) => (v as List).cast());

/// Any interfaces that should be added to types as a result of executing a macro, indexed by the name of the augmented type declaration.
Map<String, List<Augmentation>> get interfaceAugmentations =>
(node['interfaceAugmentations'] as Map)
.deepCast<String, List<Augmentation>>((v) => (v as List).cast());
Map<String, List<Augmentation>>? get interfaceAugmentations =>
(node['interfaceAugmentations'] as Map?)
?.deepCast<String, List<Augmentation>>((v) => (v as List).cast());

/// Any augmentations that should be applied to the library as a result of executing a macro.
List<Augmentation> get libraryAugmentations =>
(node['libraryAugmentations'] as List).cast();
List<Augmentation>? get libraryAugmentations =>
(node['libraryAugmentations'] as List?)?.cast();

/// Any mixins that should be added to types as a result of executing a macro, indexed by the name of the augmented type declaration.
Map<String, List<Augmentation>> get mixinAugmentations =>
(node['mixinAugmentations'] as Map)
.deepCast<String, List<Augmentation>>((v) => (v as List).cast());
Map<String, List<Augmentation>>? get mixinAugmentations =>
(node['mixinAugmentations'] as Map?)
?.deepCast<String, List<Augmentation>>((v) => (v as List).cast());

/// The names of any new types declared in [libraryAugmentations].
List<String> get newTypeNames => (node['newTypeNames'] as List).cast();
List<String>? get newTypeNames => (node['newTypeNames'] as List?)?.cast();

/// Any augmentations that should be applied to a class as a result of executing a macro, indexed by the name of the class.
Map<String, List<Augmentation>> get typeAugmentations =>
(node['typeAugmentations'] as Map)
.deepCast<String, List<Augmentation>>((v) => (v as List).cast());
Map<String, List<Augmentation>>? get typeAugmentations =>
(node['typeAugmentations'] as Map?)
?.deepCast<String, List<Augmentation>>((v) => (v as List).cast());
}

/// Request could not be handled.
Expand Down
21 changes: 14 additions & 7 deletions tool/dart_model_generator/lib/definitions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -365,40 +365,47 @@ static QualifiedName parse(String string) {
description:
'Any augmentations to enum values that should be applied '
'to an enum as a result of executing a macro, indexed by '
'the name of the enum.'),
'the name of the enum.',
nullable: true),
Property('extendsTypeAugmentations',
type: 'Map<List<Augmentation>>',
description:
'Any extends clauses that should be added to types as a '
'result of executing a macro, indexed by the name '
'of the augmented type declaration.'),
'of the augmented type declaration.',
nullable: true),
Property('interfaceAugmentations',
type: 'Map<List<Augmentation>>',
description:
'Any interfaces that should be added to types as a '
'result of executing a macro, indexed by the name '
'of the augmented type declaration.'),
'of the augmented type declaration.',
nullable: true),
Property('libraryAugmentations',
type: 'List<Augmentation>',
description:
'Any augmentations that should be applied to the library '
'as a result of executing a macro.'),
'as a result of executing a macro.',
nullable: true),
Property('mixinAugmentations',
type: 'Map<List<Augmentation>>',
description:
'Any mixins that should be added to types as a result of '
'executing a macro, indexed by the name of the '
'augmented type declaration.'),
'augmented type declaration.',
nullable: true),
Property('newTypeNames',
type: 'List<String>',
description: 'The names of any new types declared in '
'[libraryAugmentations].'),
'[libraryAugmentations].',
nullable: true),
Property('typeAugmentations',
type: 'Map<List<Augmentation>>',
description:
'Any augmentations that should be applied to a class as '
'a result of executing a macro, indexed by the '
'name of the class.'),
'name of the class.',
nullable: true),
]),
Definition.clazz('ErrorResponse',
description: 'Request could not be handled.',
Expand Down
4 changes: 2 additions & 2 deletions tool/dart_model_generator/lib/generate_dart_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ class TypeReference {
return '($rawCast)$q.deepCast<String, ${elementType!.dartType}>('
'(v) => ${elementType!.castExpression('v')})';
} else if (isList) {
if (elementType!.elementType == null) return '($rawCast).cast()';
if (elementType!.elementType == null) return '($rawCast)$q.cast()';
throw UnsupportedError('Deep casting for lists isn\'t yet supported.');
} else {
return rawCast;
Expand Down Expand Up @@ -515,7 +515,7 @@ class ClassTypeDefinition implements Definition {
result.writeln('{');
for (final property in properties) {
if (property.type.isMap) {
result.writeln("'${property.name}': {},");
result.writeln("'${property.name}': <String, Object?>{},");
} else {
result.writeln(property.namedArgumentCode);
}
Expand Down

0 comments on commit 3ffffc5

Please sign in to comment.