From ff808bba649f7c23fee7f8c67bd0b5e7013c776c Mon Sep 17 00:00:00 2001 From: stuartmorgan Date: Wed, 18 May 2022 16:42:10 -0400 Subject: [PATCH] [pigeon] Minor Obj-C simplification (#1980) --- packages/pigeon/CHANGELOG.md | 4 ++- packages/pigeon/lib/generator_tools.dart | 2 +- packages/pigeon/lib/objc_generator.dart | 26 +++++++++---------- packages/pigeon/pubspec.yaml | 2 +- packages/pigeon/test/objc_generator_test.dart | 2 +- 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/packages/pigeon/CHANGELOG.md b/packages/pigeon/CHANGELOG.md index df1a43d41a..6785e2049b 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 5fbb29bec6..4d92c386a1 100644 --- a/packages/pigeon/lib/generator_tools.dart +++ b/packages/pigeon/lib/generator_tools.dart @@ -8,7 +8,7 @@ import 'dart:mirrors'; 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 df900d598f..301c7b8dee 100644 --- a/packages/pigeon/lib/objc_generator.dart +++ b/packages/pigeon/lib/objc_generator.dart @@ -572,7 +572,7 @@ String _dictValue( } 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 @@ void _writeFlutterApiSource(Indent indent, ObjcOptions options, Api api) { 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 @@ static NSDictionary *wrapResult(id result, FlutterError *error) \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 @@ static id GetNullableObjectAtIndex(NSArray* array, NSInteger key) { 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 6b884e2bb8..25ef681717 100644 --- a/packages/pigeon/pubspec.yaml +++ b/packages/pigeon/pubspec.yaml @@ -2,7 +2,7 @@ name: pigeon 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 590d710c9f..3ef7b7840a 100644 --- a/packages/pigeon/test/objc_generator_test.dart +++ b/packages/pigeon/test/objc_generator_test.dart @@ -1507,7 +1507,7 @@ void main() { 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:')); } });