Skip to content

Commit

Permalink
update tool/generate_* scripts and sync files generated by those scri…
Browse files Browse the repository at this point in the history
…pts (#150)

* update tool/generate_* scripts and sync files generated by those scripts

* add double converter support, fix related tests

* ensure only fields of types annotated with BuiltValue are nested builders

* remove redundant import

* fix typo, sync build_value test goldens
  • Loading branch information
eEQK authored Dec 11, 2024
1 parent debc16c commit a88aa0b
Show file tree
Hide file tree
Showing 17 changed files with 425 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,26 @@ prefix1.PrimitiveFields rebuild(void Function(prefix1.PrimitiveFieldsBuilder) up
augment class NestedFields {
factory NestedFields([void Function(prefix1.NestedFieldsBuilder)? updates]) =>
(prefix1.NestedFieldsBuilder()..update(updates)).build();
NestedFields._({required this.aPrimitiveFields,required this.aString,}) {}
NestedFields._({required this.aPrimitiveFields,required this.stringWrapper,required this.aString,}) {}

prefix1.NestedFieldsBuilder toBuilder() => prefix1.NestedFieldsBuilder()..replace(this);
prefix1.NestedFields rebuild(void Function(prefix1.NestedFieldsBuilder) updates) =>
(toBuilder()..update(updates)).build();

prefix2.int get hashCode => prefix2.Object.hashAll([aPrimitiveFields,aString,]);
prefix2.int get hashCode => prefix2.Object.hashAll([aPrimitiveFields,stringWrapper,aString,]);

prefix2.bool operator==(prefix2.Object other) =>
other is prefix1.NestedFields&& aPrimitiveFields == other.aPrimitiveFields&& aString == other.aString;
other is prefix1.NestedFields&& aPrimitiveFields == other.aPrimitiveFields&& stringWrapper == other.stringWrapper&& aString == other.aString;

prefix2.String toString() => 'NestedFields(aPrimitiveFields: $aPrimitiveFields, aString: $aString)';
prefix2.String toString() => 'NestedFields(aPrimitiveFields: $aPrimitiveFields, stringWrapper: $stringWrapper, aString: $aString)';

}
augment class NestedFieldsBuilder {
prefix1.PrimitiveFieldsBuilder aPrimitiveFields = prefix1.PrimitiveFieldsBuilder();prefix2.String? aString;
prefix1.PrimitiveFieldsBuilder aPrimitiveFields = prefix1.PrimitiveFieldsBuilder();prefix1.StringWrapper? stringWrapper;prefix2.String? aString;

void replace(prefix1.NestedFields other) { this.aPrimitiveFields = other.aPrimitiveFields.toBuilder();this.aString = other.aString; }
void replace(prefix1.NestedFields other) { this.aPrimitiveFields = other.aPrimitiveFields.toBuilder();this.stringWrapper = other.stringWrapper;this.aString = other.aString; }
void update(void Function(prefix1.NestedFieldsBuilder)? updates) => updates?.call(this);
prefix1.NestedFields build() => prefix1.NestedFields._(aPrimitiveFields: aPrimitiveFields.build(),aString: aString!,);
prefix1.NestedFields build() => prefix1.NestedFields._(aPrimitiveFields: aPrimitiveFields.build(),stringWrapper: stringWrapper!,aString: aString!,);

}
augment class PrimitiveFieldsBuilder {
Expand Down
21 changes: 19 additions & 2 deletions goldens/foo/lib/built_value/built_value_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,33 @@ void main() {
b
..aPrimitiveFields.anInt = 3
..aPrimitiveFields.aString = 'four'
..aString = 'five',
..aString = 'five'
..stringWrapper = StringWrapper('six'),
);
expect(
value.toString(),
'NestedFields(aPrimitiveFields: PrimitiveFields('
'anInt: 3, aString: four, aNullableString: null), aString: five)',
'anInt: 3, aString: four, aNullableString: null), '
'stringWrapper: StringWrapper(aString: six), aString: five)',
);
});
});
}

class NonMacro {
const NonMacro();
}

@NonMacro()
class StringWrapper {
const StringWrapper(this.aString);

final String aString;

@override
String toString() => 'StringWrapper(aString: $aString)';
}

@BuiltValue()
class Empty {}

Expand All @@ -78,5 +94,6 @@ class PrimitiveFields {
@BuiltValue()
class NestedFields {
final PrimitiveFields aPrimitiveFields;
final StringWrapper stringWrapper;
final String aString;
}
16 changes: 12 additions & 4 deletions goldens/foo/lib/foo.analyzer.json
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,18 @@
"value": {
"reference": {
"type": "ClassReference",
"value": {}
"value": {
"name": "QueryClass"
}
},
"typeArguments": []
}
},
"constructor": {
"type": "ConstructorReference",
"value": {}
"value": {
"name": "new"
}
},
"arguments": []
}
Expand Down Expand Up @@ -272,14 +276,18 @@
"value": {
"reference": {
"type": "ClassReference",
"value": {}
"value": {
"name": "QueryClass"
}
},
"typeArguments": []
}
},
"constructor": {
"type": "ConstructorReference",
"value": {}
"value": {
"name": "new"
}
},
"arguments": []
}
Expand Down
4 changes: 2 additions & 2 deletions goldens/foo/lib/literal_params.analyzer.augmentations
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ augment class Foo {
// aNum: 8.0, double
// aDouble: 9.0, double
// aString: 10, String
// anObject: {type: {type: NamedTypeAnnotation, value: {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
// anObject: {type: {type: NamedTypeAnnotation, value: {reference: {type: ClassReference, value: {name: Bar}}, typeArguments: []}}, constructor: {type: ConstructorReference, value: {name: new}}, arguments: [{type: NamedArgument, value: {name: a, expression: {type: BooleanLiteral, value: {value: true}}}}, {type: NamedArgument, value: {name: b, expression: {type: BooleanLiteral, value: {value: 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: {type: NamedTypeAnnotation, value: {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>
// objects: [19, {type: {type: NamedTypeAnnotation, value: {reference: {type: ClassReference, value: {name: Bar}}, typeArguments: []}}, constructor: {type: ConstructorReference, value: {name: new}}, arguments: [{type: NamedArgument, value: {name: a, expression: {type: BooleanLiteral, value: {value: true}}}}, {type: NamedArgument, value: {name: b, expression: {type: BooleanLiteral, value: {value: false}}}}]}], List<Object>
}
21 changes: 15 additions & 6 deletions goldens/foo/lib/metadata.analyzer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@
"value": {
"reference": {
"type": "ClassReference",
"value": {}
"value": {
"name": "QueryClass"
}
},
"typeArguments": []
}
},
"constructor": {
"type": "ConstructorReference",
"value": {}
"value": {
"name": "new"
}
},
"arguments": []
}
Expand All @@ -50,14 +54,18 @@
"value": {
"reference": {
"type": "ClassReference",
"value": {}
"value": {
"name": "Annotation"
}
},
"typeArguments": []
}
},
"constructor": {
"type": "ConstructorReference",
"value": {}
"value": {
"name": "new"
}
},
"arguments": [
{
Expand All @@ -67,7 +75,7 @@
"expression": {
"type": "BooleanLiteral",
"value": {
"text": "true"
"value": true
}
}
}
Expand All @@ -79,7 +87,8 @@
"expression": {
"type": "IntegerLiteral",
"value": {
"text": "23"
"text": "23",
"value": 23
}
}
}
Expand Down
42 changes: 28 additions & 14 deletions pkgs/_analyzer_cfe_macros/lib/metadata_converter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ dart_model.Reference? convertToReference(Object? object) => switch (object) {
front_end.EnumReference o => dart_model.Reference.enumReference(
convert<dart_model.EnumReference>(o)!,
),
front_end.MixinReference o => dart_model.Reference.mixinReference(
convert<dart_model.MixinReference>(o)!,
),
front_end.FunctionTypeParameterReference o => dart_model
.Reference.functionTypeParameterReference(
convert<dart_model.FunctionTypeParameterReference>(o)!,
Expand Down Expand Up @@ -250,6 +253,8 @@ T? convert<T>(Object? object) => switch (object) {
front_end.BinaryOperator o => o.name as T,
front_end.LogicalOperator o => o.name as T,
front_end.UnaryOperator o => o.name as T,
front_end.AdjacentStringLiterals o =>
dart_model.AdjacentStringLiterals(expressions: convert(o.expressions)) as T,
front_end.AsExpression o =>
dart_model.AsExpression(
expression: convertToExpression(o.expression),
Expand All @@ -264,8 +269,9 @@ T? convert<T>(Object? object) => switch (object) {
)
as T,
front_end.BooleanLiteral o =>
dart_model.BooleanLiteral(text: convert(o.value.toString())) as T,
front_end.ClassReference o => dart_model.ClassReference() as T,
dart_model.BooleanLiteral(value: convert(o.value)) as T,
front_end.ClassReference o =>
dart_model.ClassReference(name: convert(o.name)) as T,
front_end.ConditionalExpression o =>
dart_model.ConditionalExpression(
condition: convertToExpression(o.condition),
Expand All @@ -280,19 +286,22 @@ T? convert<T>(Object? object) => switch (object) {
arguments: convert(o.arguments),
)
as T,
front_end.ConstructorReference o => dart_model.ConstructorReference() as T,
front_end.ConstructorReference o =>
dart_model.ConstructorReference(name: convert(o.name)) as T,
front_end.ConstructorTearOff o =>
dart_model.ConstructorTearOff(
type: convert(o.type),
reference: convert(o.reference),
)
as T,
front_end.DoubleLiteral o =>
dart_model.DoubleLiteral(text: convert(o.text)) as T,
dart_model.DoubleLiteral(text: convert(o.text), value: convert(o.value))
as T,
front_end.DynamicTypeAnnotation o =>
dart_model.DynamicTypeAnnotation(reference: convertToReference(o.reference))
as T,
front_end.EnumReference o => dart_model.EnumReference() as T,
front_end.EnumReference o =>
dart_model.EnumReference(name: convert(o.name)) as T,
front_end.EqualityExpression o =>
dart_model.EqualityExpression(
left: convertToExpression(o.left),
Expand All @@ -306,13 +315,16 @@ T? convert<T>(Object? object) => switch (object) {
isNullAware: convert(o.isNullAware),
)
as T,
front_end.ExtensionReference o => dart_model.ExtensionReference() as T,
front_end.ExtensionReference o =>
dart_model.ExtensionReference(name: convert(o.name)) as T,
front_end.ExtensionTypeReference o =>
dart_model.ExtensionTypeReference() as T,
front_end.FieldReference o => dart_model.FieldReference() as T,
dart_model.ExtensionTypeReference(name: convert(o.name)) as T,
front_end.FieldReference o =>
dart_model.FieldReference(name: convert(o.name)) as T,
front_end.FormalParameter o => dart_model.FormalParameter() as T,
front_end.FormalParameterGroup o => dart_model.FormalParameterGroup() as T,
front_end.FunctionReference o => dart_model.FunctionReference() as T,
front_end.FunctionReference o =>
dart_model.FunctionReference(name: convert(o.name)) as T,
front_end.FunctionTearOff o =>
dart_model.FunctionTearOff(reference: convert(o.reference)) as T,
front_end.FunctionTypeAnnotation o =>
Expand All @@ -324,7 +336,7 @@ T? convert<T>(Object? object) => switch (object) {
as T,
front_end.FunctionTypeParameter o => dart_model.FunctionTypeParameter() as T,
front_end.FunctionTypeParameterReference o =>
dart_model.FunctionTypeParameterReference() as T,
dart_model.FunctionTypeParameterReference(name: convert(o.name)) as T,
front_end.FunctionTypeParameterType o =>
dart_model.FunctionTypeParameterType(
functionTypeParameter: convert(o.functionTypeParameter),
Expand Down Expand Up @@ -357,7 +369,8 @@ T? convert<T>(Object? object) => switch (object) {
)
as T,
front_end.IntegerLiteral o =>
dart_model.IntegerLiteral(text: convert(o.text)) as T,
dart_model.IntegerLiteral(text: convert(o.text), value: convert(o.value))
as T,
front_end.InterpolationPart o =>
dart_model.InterpolationPart(expression: convertToExpression(o.expression))
as T,
Expand Down Expand Up @@ -399,6 +412,8 @@ T? convert<T>(Object? object) => switch (object) {
arguments: convert(o.arguments),
)
as T,
front_end.MixinReference o =>
dart_model.MixinReference(name: convert(o.name)) as T,
front_end.NamedArgument o =>
dart_model.NamedArgument(
name: convert(o.name),
Expand Down Expand Up @@ -479,14 +494,13 @@ T? convert<T>(Object? object) => switch (object) {
arguments: convert(o.arguments),
)
as T,
front_end.AdjacentStringLiterals o =>
dart_model.AdjacentStringLiterals(expressions: convert(o.expressions)) as T,
front_end.StringLiteral o =>
dart_model.StringLiteral(parts: convert(o.parts)) as T,
front_end.StringPart o => dart_model.StringPart(text: convert(o.text)) as T,
front_end.SymbolLiteral o =>
dart_model.SymbolLiteral(parts: convert(o.parts)) as T,
front_end.TypedefReference o => dart_model.TypedefReference() as T,
front_end.TypedefReference o =>
dart_model.TypedefReference(name: convert(o.name)) as T,
front_end.TypeLiteral o =>
dart_model.TypeLiteral(typeAnnotation: convert(o.typeAnnotation)) as T,
front_end.TypeReference o => dart_model.TypeReference() as T,
Expand Down
8 changes: 4 additions & 4 deletions pkgs/_analyzer_cfe_macros/test/metadata_converter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void main() {
expect(convert<Object>(invocation), <String, Object?>{
'receiver': {
'type': 'DoubleLiteral',
'value': {'text': '1.23'},
'value': {'text': '1.23', 'value': 1.23},
},
'name': 'round',
'typeArguments': [],
Expand All @@ -40,12 +40,12 @@ void main() {
expect(convert<Object>(expression), <String, Object?>{
'left': {
'type': 'DoubleLiteral',
'value': {'text': '1.23'},
'value': {'text': '1.23', 'value': 1.23},
},
'operator': 'minus',
'right': {
'type': 'DoubleLiteral',
'value': {'text': '1.24'},
'value': {'text': '1.24', 'value': 1.24},
},
});
});
Expand All @@ -63,7 +63,7 @@ void main() {
expect(convert<Object>(invocation), <String, Object?>{
'receiver': {
'type': 'DoubleLiteral',
'value': {'text': '1.23'},
'value': {'text': '1.23', 'value': 1.23},
},
'name': 'round',
'typeArguments': [],
Expand Down
16 changes: 14 additions & 2 deletions pkgs/_test_macros/lib/built_value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,20 @@ class BuiltValueBuilderImplementation implements ClassDeclarationsMacro {
TypeAnnotationType.namedTypeAnnotation) {
continue;
}
// TODO(davidmorgan): macro metadata model doesn't actually have the
// name yet, just assume any constructor annotation is `BuiltValue`.

final namedTypeAnnotation =
constructorInvocation.type.asNamedTypeAnnotation;
if (namedTypeAnnotation.reference.type !=
ReferenceType.classReference) {
continue;
}

final constructorReference =
namedTypeAnnotation.reference.asClassReference;
if (constructorReference.name != 'BuiltValue') {
continue;
}

nestedBuilderTypes.add(qualifiedName.asString);
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkgs/_test_macros/lib/literal_params.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ class LiteralParamsImplementation implements ClassDeclarationsMacro {
extension ExpressionExtension on Expression {
Object get evaluate => switch (type) {
ExpressionType.integerLiteral => int.parse(asIntegerLiteral.text),
ExpressionType.doubleLiteral => double.parse(asDoubleLiteral.text),
ExpressionType.doubleLiteral => asDoubleLiteral.value,
ExpressionType.stringLiteral => asStringLiteral.evaluate,
ExpressionType.booleanLiteral => bool.parse(asBooleanLiteral.text),
ExpressionType.booleanLiteral => asBooleanLiteral.value,
ExpressionType.listLiteral =>
asListLiteral.elements.map((e) => e.evaluate).toList(),
// TODO(davidmorgan): need the type name to do something useful here,
Expand Down
Loading

0 comments on commit a88aa0b

Please sign in to comment.