Skip to content

Commit

Permalink
[pigeon] adds support for non nullable types in collections (flutter#…
Browse files Browse the repository at this point in the history
…7547)

[pigeon] adds support for non nullable types in collections

fixes flutter#97848
  • Loading branch information
tarrinneal authored Sep 13, 2024
1 parent 8f47459 commit 218fd4a
Show file tree
Hide file tree
Showing 38 changed files with 29,880 additions and 10,084 deletions.
4 changes: 4 additions & 0 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 22.4.0

* Adds support for non-nullable types in collections.

## 22.3.0

* Adds support for enums and classes in collections.
Expand Down
4 changes: 2 additions & 2 deletions packages/pigeon/lib/dart_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2159,8 +2159,8 @@ String _getMethodParameterSignature(Iterable<Parameter> parameters) {
String _flattenTypeArguments(List<TypeDeclaration> args) {
return args
.map<String>((TypeDeclaration arg) => arg.typeArguments.isEmpty
? '${arg.baseName}?'
: '${arg.baseName}<${_flattenTypeArguments(arg.typeArguments)}>?')
? '${arg.baseName}${arg.isNullable ? '?' : ''}'
: '${arg.baseName}<${_flattenTypeArguments(arg.typeArguments)}>${arg.isNullable ? '?' : ''}')
.join(', ');
}

Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/lib/generator_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import 'ast.dart';
/// The current version of pigeon.
///
/// This must match the version in pubspec.yaml.
const String pigeonVersion = '22.3.0';
const String pigeonVersion = '22.4.0';

/// Read all the content from [stdin] to a String.
String readStdin() {
Expand Down
24 changes: 12 additions & 12 deletions packages/pigeon/lib/objc_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ class ObjcHeaderGenerator extends StructuredGenerator<ObjcOptions> {
final _ObjcType returnType = _objcTypeForDartType(
generatorOptions.prefix, func.returnType,
// Nullability is required since the return must be nil if NSError is set.
forceNullability: true,
forceBox: true,
);
final String callbackType =
_callbackForType(func.returnType, returnType, generatorOptions);
Expand Down Expand Up @@ -336,7 +336,7 @@ class ObjcHeaderGenerator extends StructuredGenerator<ObjcOptions> {
func.returnType,
// Nullability is required since the return must be nil if NSError is
// set.
forceNullability: true,
forceBox: true,
);

String? lastArgName;
Expand Down Expand Up @@ -1072,7 +1072,7 @@ static FlutterError *createConnectionError(NSString *channelName) {
final _ObjcType returnType = _objcTypeForDartType(
generatorOptions.prefix, func.returnType,
// Nullability is required since the return must be nil if NSError is set.
forceNullability: true,
forceBox: true,
);
final Iterable<String> selectorComponents =
_getSelectorComponents(func, lastSelectorComponent);
Expand Down Expand Up @@ -1187,7 +1187,7 @@ void _writeMethod(
languageOptions.prefix,
func.returnType,
// Nullability is required since the return must be nil if NSError is set.
forceNullability: true,
forceBox: true,
);
final String callbackType =
_callbackForType(func.returnType, returnType, languageOptions);
Expand Down Expand Up @@ -1444,23 +1444,23 @@ String _flattenTypeArguments(String? classPrefix, List<TypeDeclaration> args) {
return _enumName(e.baseName,
prefix: classPrefix, box: true, suffix: ' *');
}
return _objcTypeForDartType(classPrefix, e).toString();
return _objcTypeForDartType(classPrefix, e, forceBox: true).toString();
}).join(', ');
return result;
}

_ObjcType? _objcTypeForPrimitiveDartType(TypeDeclaration type,
{bool forceNullability = false}) {
return forceNullability || type.isNullable
{bool forceBox = false}) {
return forceBox || type.isNullable
? _objcTypeForNullableDartTypeMap[type.baseName]
: _objcTypeForNonNullableDartTypeMap[type.baseName];
}

String? _objcTypeStringForPrimitiveDartType(
String? classPrefix, TypeDeclaration type,
{required bool beforeString, bool forceNullability = false}) {
{required bool beforeString, bool forceBox = false}) {
final _ObjcType? objcType;
if (forceNullability || type.isNullable) {
if (forceBox || type.isNullable) {
objcType = _objcTypeForNullableDartTypeMap.containsKey(type.baseName)
? _objcTypeForDartType(classPrefix, type)
: null;
Expand All @@ -1475,14 +1475,14 @@ String? _objcTypeStringForPrimitiveDartType(
/// Returns the Objective-C type for a Dart [field], prepending the
/// [classPrefix] for generated classes.
_ObjcType _objcTypeForDartType(String? classPrefix, TypeDeclaration field,
{bool forceNullability = false}) {
{bool forceBox = false}) {
final _ObjcType? primitiveType =
_objcTypeForPrimitiveDartType(field, forceNullability: forceNullability);
_objcTypeForPrimitiveDartType(field, forceBox: forceBox);
return primitiveType == null
? _ObjcType(
baseName: _className(classPrefix, field.baseName),
// Non-nullable enums are non-pointer types.
isPointer: !field.isEnum || (field.isNullable || forceNullability))
isPointer: !field.isEnum || (field.isNullable || forceBox))
: field.typeArguments.isEmpty
? primitiveType
: _ObjcType(
Expand Down
9 changes: 0 additions & 9 deletions packages/pigeon/lib/pigeon_lib.dart
Original file line number Diff line number Diff line change
Expand Up @@ -971,15 +971,6 @@ List<Error> _validateAst(Root root, String source) {
lineNumber: _calculateLineNumberNullable(source, field.offset),
));
}
for (final TypeDeclaration typeArgument in field.type.typeArguments) {
if (!typeArgument.isNullable) {
result.add(Error(
message:
'Generic type parameters must be nullable in field "${field.name}" in class "${classDefinition.name}".',
lineNumber: _calculateLineNumberNullable(source, field.offset),
));
}
}
if (!(validTypes.contains(field.type.baseName) ||
customClasses.contains(field.type.baseName) ||
customEnums.contains(field.type.baseName))) {
Expand Down
25 changes: 16 additions & 9 deletions packages/pigeon/lib/swift_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1031,8 +1031,7 @@ String _flattenTypeArguments(List<TypeDeclaration> args) {
}

String _swiftTypeForBuiltinGenericDartType(TypeDeclaration type) {
if (type.typeArguments.isEmpty ||
(type.typeArguments.first.baseName == 'Object')) {
if (type.typeArguments.isEmpty) {
if (type.baseName == 'List') {
return '[Any?]';
} else if (type.baseName == 'Map') {
Expand All @@ -1044,14 +1043,17 @@ String _swiftTypeForBuiltinGenericDartType(TypeDeclaration type) {
if (type.baseName == 'List') {
return '[${_nullSafeSwiftTypeForDartType(type.typeArguments.first)}]';
} else if (type.baseName == 'Map') {
return '[${_nullSafeSwiftTypeForDartType(type.typeArguments.first)}: ${_nullSafeSwiftTypeForDartType(type.typeArguments.last)}]';
return '[${_nullSafeSwiftTypeForDartType(type.typeArguments.first, mapKey: true)}: ${_nullSafeSwiftTypeForDartType(type.typeArguments.last)}]';
} else {
return '${type.baseName}<${_flattenTypeArguments(type.typeArguments)}>';
}
}
}

String? _swiftTypeForBuiltinDartType(TypeDeclaration type) {
String? _swiftTypeForBuiltinDartType(
TypeDeclaration type, {
bool mapKey = false,
}) {
const Map<String, String> swiftTypeForDartTypeMap = <String, String>{
'void': 'Void',
'bool': 'Bool',
Expand All @@ -1065,7 +1067,9 @@ String? _swiftTypeForBuiltinDartType(TypeDeclaration type) {
'Float64List': 'FlutterStandardTypedData',
'Object': 'Any',
};
if (swiftTypeForDartTypeMap.containsKey(type.baseName)) {
if (mapKey && type.baseName == 'Object') {
return 'AnyHashable';
} else if (swiftTypeForDartTypeMap.containsKey(type.baseName)) {
return swiftTypeForDartTypeMap[type.baseName];
} else if (type.baseName == 'List' || type.baseName == 'Map') {
return _swiftTypeForBuiltinGenericDartType(type);
Expand All @@ -1074,13 +1078,16 @@ String? _swiftTypeForBuiltinDartType(TypeDeclaration type) {
}
}

String _swiftTypeForDartType(TypeDeclaration type) {
return _swiftTypeForBuiltinDartType(type) ?? type.baseName;
String _swiftTypeForDartType(TypeDeclaration type, {bool mapKey = false}) {
return _swiftTypeForBuiltinDartType(type, mapKey: mapKey) ?? type.baseName;
}

String _nullSafeSwiftTypeForDartType(TypeDeclaration type) {
String _nullSafeSwiftTypeForDartType(
TypeDeclaration type, {
bool mapKey = false,
}) {
final String nullSafe = type.isNullable ? '?' : '';
return '${_swiftTypeForDartType(type)}$nullSafe';
return '${_swiftTypeForDartType(type, mapKey: mapKey)}$nullSafe';
}

String _getMethodSignature({
Expand Down
Loading

0 comments on commit 218fd4a

Please sign in to comment.