-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add LiteralParams macro. Fix converter for lists of union types. * Address review comments.
- Loading branch information
1 parent
65bf11d
commit 178c59f
Showing
7 changed files
with
202 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
part of 'package:foo/literal_params.dart'; | ||
|
||
augment class Foo { | ||
// anInt: 7, int | ||
// aNum: 8.0, double | ||
// aDouble: 9.0, double | ||
// aString: 10, String | ||
// anObject: {type: {reference: {type: ClassReference, value: {}}, typeArguments: []}, constructor: {type: ConstructorReference, value: {}}, arguments: [{type: NamedArgument, value: {name: a, expression: {type: BooleanLiteral, value: {text: true}}}}, {type: NamedArgument, value: {name: b, expression: {type: BooleanLiteral, value: {text: false}}}}]}, String | ||
// ints: [11, 12], List<Object> | ||
// nums: [13.0, 14], List<Object> | ||
// doubles: [15.0, 16], List<Object> | ||
// strings: [17, eighteen], List<Object> | ||
// objects: [19, {type: {reference: {type: ClassReference, value: {}}, typeArguments: []}, constructor: {type: ConstructorReference, value: {}}, arguments: [{type: NamedArgument, value: {name: a, expression: {type: BooleanLiteral, value: {text: true}}}}, {type: NamedArgument, value: {name: b, expression: {type: BooleanLiteral, value: {text: false}}}}]}], List<Object> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:_test_macros/literal_params.dart'; | ||
|
||
@LiteralParams( | ||
anInt: 7, | ||
aNum: 8.0, | ||
aDouble: 9.0, | ||
aString: '10', | ||
anObject: Bar(a: true, b: false), | ||
ints: [11, 12], | ||
nums: [13.0, 14], | ||
doubles: [15.0, 16], | ||
strings: ['17', 'eighteen'], | ||
objects: [ | ||
19, | ||
Bar(a: true, b: false), | ||
], | ||
) | ||
class Foo {} | ||
|
||
class Bar { | ||
final bool? a; | ||
final bool? b; | ||
|
||
const Bar({this.a, this.b}); | ||
} | ||
|
||
void main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:dart_model/dart_model.dart'; | ||
// ignore: implementation_imports | ||
import 'package:dart_model/src/macro_metadata.g.dart'; | ||
import 'package:macro/macro.dart'; | ||
import 'package:macro_service/macro_service.dart'; | ||
|
||
import 'templating.dart'; | ||
|
||
/// Covers macro metadata cases where the params will always be written as | ||
/// literals in the annotation. | ||
/// | ||
/// Outputs comments with evaluation results. | ||
/// | ||
/// Throws if the annotation has something other than supported literals. | ||
/// TODO(davidmorgan): support diagnostics, make failures diagnostics. | ||
class LiteralParams { | ||
final int? anInt; | ||
final num? aNum; | ||
final double? aDouble; | ||
final String? aString; | ||
final Object? anObject; | ||
final List<int>? ints; | ||
final List<num>? nums; | ||
final List<double>? doubles; | ||
final List<String>? strings; | ||
final List<Object>? objects; | ||
|
||
const LiteralParams( | ||
{required this.anInt, | ||
this.aNum, | ||
this.aDouble, | ||
this.aString, | ||
this.anObject, | ||
this.ints, | ||
this.nums, | ||
this.doubles, | ||
this.strings, | ||
this.objects}); | ||
} | ||
|
||
class LiteralParamsImplementation implements ClassDeclarationsMacro { | ||
// TODO(davidmorgan): this should be injected by the bootstrap script. | ||
@override | ||
MacroDescription get description => MacroDescription( | ||
annotation: QualifiedName( | ||
uri: 'package:_test_macros/literal_params.dart', | ||
name: 'LiteralParams'), | ||
runsInPhases: [2]); | ||
|
||
@override | ||
Future<void> buildDeclarationsForClass( | ||
ClassDeclarationsBuilder builder) async { | ||
// TODO(davidmorgan): need a way to find the correct annotation, this just | ||
// uses the first. | ||
final annotation = builder | ||
.target.metadataAnnotations.first.expression.asConstructorInvocation; | ||
|
||
final namedArguments = { | ||
for (final argument in annotation.arguments) | ||
if (argument.type == ArgumentType.namedArgument) | ||
argument.asNamedArgument.name: | ||
argument.asNamedArgument.expression.evaluate | ||
}; | ||
|
||
builder.declareInType(Augmentation( | ||
code: expandTemplate([ | ||
for (final entry in namedArguments.entries) | ||
' // ${entry.key}: ${entry.value}, ${entry.value.runtimeType}', | ||
].join('\n')))); | ||
} | ||
} | ||
|
||
// TODO(davidmorgan): common code for this in `dart_model` so macros don't | ||
// all have to write expression evaluation code. | ||
extension ExpressionExtension on Expression { | ||
Object get evaluate => switch (type) { | ||
ExpressionType.integerLiteral => int.parse(asIntegerLiteral.text), | ||
ExpressionType.doubleLiteral => double.parse(asDoubleLiteral.text), | ||
ExpressionType.stringLiteral => asStringLiteral.evaluate, | ||
ExpressionType.booleanLiteral => bool.parse(asBooleanLiteral.text), | ||
ExpressionType.listLiteral => | ||
asListLiteral.elements.map((e) => e.evaluate).toList(), | ||
// TODO(davidmorgan): need the type name to do something useful here, | ||
// for now just return the JSON. | ||
ExpressionType.constructorInvocation => | ||
asConstructorInvocation.toString(), | ||
// TODO(davidmorgan): need to follow references to do something useful | ||
// here, for now just return the JSON. | ||
ExpressionType.staticGet => asStaticGet.toString(), | ||
_ => throw UnsupportedError( | ||
'Not supported in @LiteralParams annotation: $this'), | ||
}; | ||
} | ||
|
||
extension ElementExtension on Element { | ||
Object get evaluate => switch (type) { | ||
ElementType.expressionElement => | ||
asExpressionElement.expression.evaluate, | ||
_ => throw UnsupportedError( | ||
'Not supported in @LiteralParams annotation: $this'), | ||
}; | ||
} | ||
|
||
extension StringLiteralExtension on StringLiteral { | ||
Object get evaluate { | ||
if (parts.length != 1) { | ||
throw UnsupportedError( | ||
'Not supported in @LiteralParams annotation: $this'); | ||
} | ||
final part = parts.single; | ||
return switch (part.type) { | ||
StringLiteralPartType.stringPart => part.asStringPart.text, | ||
_ => throw UnsupportedError( | ||
'Not supported in @LiteralParams annotation: $this'), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters