Skip to content

Commit ef111ee

Browse files
committed
enum fixes, and Any casting
1 parent 3be7eb1 commit ef111ee

File tree

5 files changed

+129
-120
lines changed

5 files changed

+129
-120
lines changed

packages/pigeon/lib/swift_generator.dart

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,14 @@ import FlutterMacOS
192192
});
193193
} else if (!hostDatatype.isBuiltin &&
194194
customEnumNames.contains(field.type.baseName)) {
195+
indent.writeln('var ${field.name}: $fieldType? = nil');
195196
indent.writeln(
196-
'let ${field.name} = ${_castForceUnwrap(listValue, field.type, root)}');
197+
'let enumVal$index = ${_castForceUnwrap(listValue, const TypeDeclaration(baseName: 'Int', isNullable: true), root)}');
198+
indent.write('if let ${field.name}RawValue = enumVal$index ');
199+
indent.addScoped('{', '}', () {
200+
indent.writeln(
201+
'${field.name} = $fieldType(rawValue: ${field.name}RawValue)');
202+
});
197203
} else {
198204
indent.writeln(
199205
'let ${field.name} = ${_castForceUnwrap(listValue, field.type, root)} ');
@@ -677,22 +683,17 @@ String _camelCase(String text) {
677683
}
678684

679685
String _castForceUnwrap(String value, TypeDeclaration type, Root root) {
680-
final String castUnwrap = type.isNullable ? '?' : '';
681686
if (isEnum(root, type)) {
682-
final String nullableConditionPrefix =
683-
type.isNullable ? 'nilOrValue(value: $value) != nil ? ' : '';
684-
final String nullableConditionSuffix = type.isNullable
685-
? ' : nilOrValue(value: $value) as! ${type.baseName}?'
686-
: '';
687-
687+
final String nullableConditionPrefix = type.isNullable ? '' : '';
688+
final String nullableConditionSuffix = type.isNullable ? '' : '!';
688689
return '$nullableConditionPrefix${_swiftTypeForDartType(type)}(rawValue: $value as! Int)$nullableConditionSuffix';
689690
} else if (type.baseName == 'Object') {
690691
// Special-cased to avoid warnings about using 'as' with Any.
691692
return value;
692693
} else if (type.isNullable) {
693-
return 'nilOrValue(value: $value) as! ${_swiftTypeForDartType(type)}$castUnwrap';
694+
return '(nilOrValue(value: $value) as Any) as! ${_swiftTypeForDartType(type)}?';
694695
} else {
695-
return '$value as! ${_swiftTypeForDartType(type)}$castUnwrap';
696+
return '$value as! ${_swiftTypeForDartType(type)}';
696697
}
697698
}
698699

packages/pigeon/platform_tests/test_plugin/example/ios/RunnerTests/RunnerTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ class RunnerTests: XCTestCase {
1010
func testToListAndBack() throws {
1111
let reply = MessageSearchReply(result: "foobar")
1212
let dict = reply.toList()
13-
let copy = MessageSearchReply.fromList(dict)
13+
let copy = MessageSearchReply.fromList(dict as [Any])
1414
XCTAssertEqual(reply.result, copy?.result)
1515
}
1616

1717
func testHandlesNull() throws {
1818
let reply = MessageSearchReply()
1919
let dict = reply.toList()
20-
let copy = MessageSearchReply.fromList(dict)
20+
let copy = MessageSearchReply.fromList(dict as [Any])
2121
XCTAssertNil(copy?.result)
2222
}
2323

2424
func testHandlesNullFirst() throws {
2525
let reply = MessageSearchReply(error: "foobar")
2626
let dict = reply.toList()
27-
let copy = MessageSearchReply.fromList(dict)
27+
let copy = MessageSearchReply.fromList(dict as [Any])
2828
XCTAssertEqual(reply.error, copy?.error)
2929
}
3030
}

0 commit comments

Comments
 (0)