-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[pigeon] fix swift nsnull casting crash #3545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 12 commits
574ee0b
46ca75e
e350040
3d31aff
3be7eb1
ef111ee
6d5271f
a4625f8
29ac0ec
4a7b408
d1d9c6f
241f0e3
8d356cc
04dfdbd
ed57271
1bd3afa
d0f9b86
fab7ca3
2e2d337
5b39dbe
cbb8e0b
7fa0f7d
b0c7653
740abd8
748a0bb
d8f5fe3
06ab71e
547ed02
367009e
cdf35ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -188,30 +188,30 @@ import FlutterMacOS | |
| indent.write('if let ${field.name}List = $listValue as! [Any]? '); | ||
| indent.addScoped('{', '}', () { | ||
| indent.writeln( | ||
| '${field.name} = $fieldType.fromList(${field.name}List as [Any])'); | ||
| '${field.name} = $fieldType.fromList(${field.name}List)'); | ||
| }); | ||
| } else if (!hostDatatype.isBuiltin && | ||
| customEnumNames.contains(field.type.baseName)) { | ||
| indent.writeln('var ${field.name}: $fieldType? = nil'); | ||
| indent.write('if let ${field.name}RawValue = $listValue as! Int? '); | ||
| indent.writeln( | ||
| 'let enumVal$index: Int? = ${_castForceUnwrap(listValue, const TypeDeclaration(baseName: 'Int', isNullable: true), root)}'); | ||
| indent.write('if let ${field.name}RawValue = enumVal$index '); | ||
| indent.addScoped('{', '}', () { | ||
| indent.writeln( | ||
| '${field.name} = $fieldType(rawValue: ${field.name}RawValue)'); | ||
| }); | ||
| } else { | ||
| indent.writeln('let ${field.name} = $listValue as! $fieldType? '); | ||
| indent.writeln( | ||
| 'let ${field.name}: $fieldType? = ${_castForceUnwrap(listValue, field.type, root)} '); | ||
| } | ||
| } else { | ||
| if (!hostDatatype.isBuiltin && | ||
| customClassNames.contains(field.type.baseName)) { | ||
| indent.writeln( | ||
| 'let ${field.name} = $fieldType.fromList($listValue as! [Any])!'); | ||
| } else if (!hostDatatype.isBuiltin && | ||
| customEnumNames.contains(field.type.baseName)) { | ||
| indent.writeln( | ||
| 'let ${field.name} = $fieldType(rawValue: $listValue as! Int)!'); | ||
| } else { | ||
| indent.writeln('let ${field.name} = $listValue as! $fieldType'); | ||
| indent.writeln( | ||
| 'let ${field.name}: $fieldType = ${_castForceUnwrap(listValue, field.type, root)}'); | ||
tarrinneal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| }); | ||
|
|
@@ -346,7 +346,7 @@ import FlutterMacOS | |
| } else { | ||
| indent.addScoped('{ response in', '}', () { | ||
| indent.writeln( | ||
| 'let result = ${_castForceUnwrap("response", func.returnType, root)}'); | ||
| 'let result: ${_nullsafeSwiftTypeForDartType(func.returnType)} = ${_castForceUnwrap("response", func.returnType, root)}'); | ||
| indent.writeln('completion(result)'); | ||
| }); | ||
| } | ||
|
|
@@ -462,7 +462,7 @@ import FlutterMacOS | |
| _getSafeArgumentName(index, arg.namedType); | ||
| final String argIndex = 'args[$index]'; | ||
| indent.writeln( | ||
| 'let $argName = ${_castForceUnwrap(argIndex, arg.type, root)}'); | ||
| 'let $argName: ${_nullsafeSwiftTypeForDartType(arg.type)} = ${_castForceUnwrap(argIndex, arg.type, root)}'); | ||
|
|
||
| if (arg.label == '_') { | ||
| methodArgument.add(argName); | ||
|
|
@@ -637,11 +637,26 @@ import FlutterMacOS | |
| }); | ||
| } | ||
|
|
||
| void _writeNilOrType(Indent indent) { | ||
| indent.format(''' | ||
|
|
||
| private func nilOrValue<T>(value: Any?) -> T? { | ||
tarrinneal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if value is NSNull { | ||
| return nil | ||
| } | ||
| if let convertedValue = value as? T { | ||
tarrinneal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return convertedValue | ||
| } | ||
| return (value as Any) as! T? | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discussed offline - this is indeed a weird Swift behavior that |
||
| }'''); | ||
| } | ||
|
|
||
| @override | ||
| void writeGeneralUtilities( | ||
| SwiftOptions generatorOptions, Root root, Indent indent) { | ||
| _writeWrapResult(indent); | ||
| _writeWrapError(indent); | ||
| _writeNilOrType(indent); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -668,19 +683,15 @@ String _camelCase(String text) { | |
| } | ||
|
|
||
| String _castForceUnwrap(String value, TypeDeclaration type, Root root) { | ||
| final String forceUnwrap = type.isNullable ? '' : '!'; | ||
| final String castUnwrap = type.isNullable ? '?' : ''; | ||
| if (isEnum(root, type)) { | ||
| final String nullableConditionPrefix = | ||
| type.isNullable ? '$value == nil ? nil : ' : ''; | ||
| return '$nullableConditionPrefix${_swiftTypeForDartType(type)}(rawValue: $value as! Int)$forceUnwrap'; | ||
| return '${_swiftTypeForDartType(type)}(rawValue: $value as! Int)!'; | ||
|
||
| } else if (type.baseName == 'Object') { | ||
tarrinneal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Special-cased to avoid warnings about using 'as' with Any. | ||
| return value; | ||
| } else if (type.baseName == 'int') { | ||
| return '($value is Int) ? Int64($value as! Int) : $value as! Int64$castUnwrap'; | ||
tarrinneal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } else if (type.isNullable) { | ||
| return 'nilOrValue(value: $value)'; | ||
| } else { | ||
| return '$value as! ${_swiftTypeForDartType(type)}$castUnwrap'; | ||
| return '$value as! ${_swiftTypeForDartType(type)}'; | ||
| } | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.