Skip to content

Commit 3be7eb1

Browse files
committed
fix enums and tests
1 parent 3d31aff commit 3be7eb1

File tree

4 files changed

+11
-25
lines changed

4 files changed

+11
-25
lines changed

packages/pigeon/lib/swift_generator.dart

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,8 @@ import FlutterMacOS
192192
});
193193
} else if (!hostDatatype.isBuiltin &&
194194
customEnumNames.contains(field.type.baseName)) {
195-
indent.writeln('var ${field.name}: $fieldType? = nil');
196195
indent.writeln(
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-
});
196+
'let ${field.name} = ${_castForceUnwrap(listValue, field.type, root)}');
203197
} else {
204198
indent.writeln(
205199
'let ${field.name} = ${_castForceUnwrap(listValue, field.type, root)} ');
@@ -683,12 +677,15 @@ String _camelCase(String text) {
683677
}
684678

685679
String _castForceUnwrap(String value, TypeDeclaration type, Root root) {
686-
final String forceUnwrap = type.isNullable ? '' : '!';
687680
final String castUnwrap = type.isNullable ? '?' : '';
688681
if (isEnum(root, type)) {
689682
final String nullableConditionPrefix =
690-
type.isNullable ? '$value == nil ? nil : ' : '';
691-
return '$nullableConditionPrefix${_swiftTypeForDartType(type)}(rawValue: $value as! Int)$forceUnwrap';
683+
type.isNullable ? 'nilOrValue(value: $value) != nil ? ' : '';
684+
final String nullableConditionSuffix = type.isNullable
685+
? ' : nilOrValue(value: $value) as! ${type.baseName}?'
686+
: '';
687+
688+
return '$nullableConditionPrefix${_swiftTypeForDartType(type)}(rawValue: $value as! Int)$nullableConditionSuffix';
692689
} else if (type.baseName == 'Object') {
693690
// Special-cased to avoid warnings about using 'as' with Any.
694691
return value;

packages/pigeon/platform_tests/test_plugin/ios/Classes/CoreTests.gen.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,7 @@ struct AllNullableTypes {
142142
let nullableNestedList = nilOrValue(value: list[10]) as! [[Bool?]?]?
143143
let nullableMapWithAnnotations = nilOrValue(value: list[11]) as! [String?: String?]?
144144
let nullableMapWithObject = nilOrValue(value: list[12]) as! [String?: Any?]?
145-
var aNullableEnum: AnEnum? = nil
146-
let enumVal13 = nilOrValue(value: list[13]) as! Int?
147-
if let aNullableEnumRawValue = enumVal13{
148-
aNullableEnum = AnEnum(rawValue: aNullableEnumRawValue)
149-
}
145+
let aNullableEnum = nilOrValue(value: list[13]) != nil ? AnEnum(rawValue: list[13] as! Int) : nilOrValue(value: list[13]) as! AnEnum?
150146
let aNullableString = nilOrValue(value: list[14]) as! String?
151147

152148
return AllNullableTypes(

packages/pigeon/platform_tests/test_plugin/macos/Classes/CoreTests.gen.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,7 @@ struct AllNullableTypes {
142142
let nullableNestedList = nilOrValue(value: list[10]) as! [[Bool?]?]?
143143
let nullableMapWithAnnotations = nilOrValue(value: list[11]) as! [String?: String?]?
144144
let nullableMapWithObject = nilOrValue(value: list[12]) as! [String?: Any?]?
145-
var aNullableEnum: AnEnum? = nil
146-
let enumVal13 = nilOrValue(value: list[13]) as! Int?
147-
if let aNullableEnumRawValue = enumVal13{
148-
aNullableEnum = AnEnum(rawValue: aNullableEnumRawValue)
149-
}
145+
let aNullableEnum = nilOrValue(value: list[13]) != nil ? AnEnum(rawValue: list[13] as! Int) : nilOrValue(value: list[13]) as! AnEnum?
150146
let aNullableString = nilOrValue(value: list[14]) as! String?
151147

152148
return AllNullableTypes(

packages/pigeon/test/swift_generator_test.dart

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void main() {
8383
generator.generate(swiftOptions, root, sink);
8484
final String code = sink.toString();
8585
expect(code, contains('enum Foo: Int'));
86-
expect(code, contains('let fooArg = Foo(rawValue: args[0] as! Int)!'));
86+
expect(code, contains('let fooArg = Foo(rawValue: args[0] as! Int)'));
8787
});
8888

8989
test('gen one host api', () {
@@ -915,10 +915,7 @@ void main() {
915915
const SwiftGenerator generator = SwiftGenerator();
916916
generator.generate(swiftOptions, root, sink);
917917
final String code = sink.toString();
918-
expect(
919-
code,
920-
contains(
921-
'let fooArg = (args[0] is NSNull) ? (nil as Any?) as! Int64? : args[0] as! Int64?'));
918+
expect(code, contains('nilOrValue(value: args[0]) as! Int64?'));
922919
});
923920

924921
test('nullable argument flutter', () {

0 commit comments

Comments
 (0)