[pigeon] Minor Obj-C simplification (#1980)
diff --git a/packages/pigeon/CHANGELOG.md b/packages/pigeon/CHANGELOG.md
index df1a43d..6785e20 100644
--- a/packages/pigeon/CHANGELOG.md
+++ b/packages/pigeon/CHANGELOG.md
@@ -1,5 +1,7 @@
-## NEXT
+## 3.0.4
+* [objc] Simplified some code output, including avoiding Xcode warnings about
+ using `NSNumber*` directly as boolean value.
* [tests] Moved test script to enable CI.
## 3.0.3
diff --git a/packages/pigeon/lib/generator_tools.dart b/packages/pigeon/lib/generator_tools.dart
index 5fbb29b..4d92c38 100644
--- a/packages/pigeon/lib/generator_tools.dart
+++ b/packages/pigeon/lib/generator_tools.dart
@@ -8,7 +8,7 @@
import 'ast.dart';
/// The current version of pigeon. This must match the version in pubspec.yaml.
-const String pigeonVersion = '3.0.3';
+const String pigeonVersion = '3.0.4';
/// Read all the content from [stdin] to a String.
String readStdin() {
diff --git a/packages/pigeon/lib/objc_generator.dart b/packages/pigeon/lib/objc_generator.dart
index df900d5..301c7b8 100644
--- a/packages/pigeon/lib/objc_generator.dart
+++ b/packages/pigeon/lib/objc_generator.dart
@@ -572,7 +572,7 @@
} else if (enumNames.contains(field.type.baseName)) {
return '@(self.${field.name})';
} else {
- return '(self.${field.name} ? self.${field.name} : [NSNull null])';
+ return '(self.${field.name} ?: [NSNull null])';
}
}
@@ -761,8 +761,7 @@
if (func.arguments.isEmpty) {
sendArgument = 'nil';
} else {
- String makeVarOrNSNullExpression(String x) =>
- '($x == nil) ? [NSNull null] : $x';
+ String makeVarOrNSNullExpression(String x) => '$x ?: [NSNull null]';
sendArgument = '@[${argNames.map(makeVarOrNSNullExpression).join(', ')}]';
}
indent.write(_makeObjcSignature(
@@ -838,13 +837,13 @@
\tNSDictionary *errorDict = (NSDictionary *)[NSNull null];
\tif (error) {
\t\terrorDict = @{
-\t\t\t\t@"${Keys.errorCode}": (error.code ? error.code : [NSNull null]),
-\t\t\t\t@"${Keys.errorMessage}": (error.message ? error.message : [NSNull null]),
-\t\t\t\t@"${Keys.errorDetails}": (error.details ? error.details : [NSNull null]),
+\t\t\t\t@"${Keys.errorCode}": (error.code ?: [NSNull null]),
+\t\t\t\t@"${Keys.errorMessage}": (error.message ?: [NSNull null]),
+\t\t\t\t@"${Keys.errorDetails}": (error.details ?: [NSNull null]),
\t\t\t\t};
\t}
\treturn @{
-\t\t\t@"${Keys.result}": (result ? result : [NSNull null]),
+\t\t\t@"${Keys.result}": (result ?: [NSNull null]),
\t\t\t@"${Keys.error}": errorDict,
\t\t\t};
}''');
@@ -908,12 +907,13 @@
void writeToMap() {
indent.write('- (NSDictionary *)toMap ');
indent.scoped('{', '}', () {
- indent.write('return [NSDictionary dictionaryWithObjectsAndKeys:');
- for (final NamedType field in klass.fields) {
- indent.add(_dictValue(classNames, enumNames, field) +
- ', @"${field.name}", ');
- }
- indent.addln('nil];');
+ indent.write('return');
+ indent.scoped(' @{', '};', () {
+ for (final NamedType field in klass.fields) {
+ indent.writeln(
+ '@"${field.name}" : ${_dictValue(classNames, enumNames, field)},');
+ }
+ });
});
}
diff --git a/packages/pigeon/pubspec.yaml b/packages/pigeon/pubspec.yaml
index 6b884e2..25ef681 100644
--- a/packages/pigeon/pubspec.yaml
+++ b/packages/pigeon/pubspec.yaml
@@ -2,7 +2,7 @@
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
repository: https://github.com/flutter/packages/tree/main/packages/pigeon
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3Apigeon
-version: 3.0.3 # This must match the version in lib/generator_tools.dart
+version: 3.0.4 # This must match the version in lib/generator_tools.dart
environment:
sdk: ">=2.12.0 <3.0.0"
diff --git a/packages/pigeon/test/objc_generator_test.dart b/packages/pigeon/test/objc_generator_test.dart
index 590d710..3ef7b78 100644
--- a/packages/pigeon/test/objc_generator_test.dart
+++ b/packages/pigeon/test/objc_generator_test.dart
@@ -1507,7 +1507,7 @@
expect(
code,
contains(
- '[channel sendMessage:@[(arg_x == nil) ? [NSNull null] : arg_x, (arg_y == nil) ? [NSNull null] : arg_y] reply:'));
+ '[channel sendMessage:@[arg_x ?: [NSNull null], arg_y ?: [NSNull null]] reply:'));
}
});