Skip to content

Commit 5401f8b

Browse files
authored
[pigeon] make swift enum CaseIterable (#11702)
instead of generating ```swift enum FooEnum: Int { case foo = 0 ... ``` we will now generate ```swift enum FooEnum: Int, CaseIterable { case foo = 0 ... ``` allowing consumers to use `FooEnum.allCases` to iterate over all possible enum values in swift platform code fixes flutter/flutter#186447 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent cb70d4e commit 5401f8b

10 files changed

Lines changed: 28 additions & 10 deletions

File tree

packages/pigeon/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 27.1.0
2+
3+
* [swift] Adds `CaseIterable` conformance to generated enums.
4+
15
## 27.0.0
26

37
* **Breaking Change** Overrides `toString` (or equivalent) methods on generated data classes

packages/pigeon/example/app/ios/Runner/Messages.g.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ private func nilOrValue<T>(_ value: Any?) -> T? {
192192
return value as! T?
193193
}
194194

195-
enum Code: Int {
195+
enum Code: Int, CaseIterable {
196196
case one = 0
197197
case two = 1
198198
}

packages/pigeon/lib/src/generator_tools.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import 'generator.dart';
1515
/// The current version of pigeon.
1616
///
1717
/// This must match the version in pubspec.yaml.
18-
const String pigeonVersion = '27.0.0';
18+
const String pigeonVersion = '27.1.0';
1919

2020
/// Default plugin package name.
2121
const String defaultPluginPackageName = 'dev.flutter.pigeon';

packages/pigeon/lib/src/swift/swift_generator.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ class SwiftGenerator extends StructuredGenerator<InternalSwiftOptions> {
234234
indent.newln();
235235
addDocumentationComments(indent, anEnum.documentationComments, _docCommentSpec);
236236

237-
indent.write('enum ${anEnum.name}: Int ');
237+
indent.write('enum ${anEnum.name}: Int, CaseIterable ');
238238
indent.addScoped('{', '}', () {
239239
enumerate(anEnum.members, (int index, EnumMember member) {
240240
addDocumentationComments(indent, member.documentationComments, _docCommentSpec);

packages/pigeon/platform_tests/test_plugin/darwin/test_plugin/Sources/test_plugin/CoreTests.gen.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,15 @@ private func nilOrValue<T>(_ value: Any?) -> T? {
193193
return value as! T?
194194
}
195195

196-
enum AnEnum: Int {
196+
enum AnEnum: Int, CaseIterable {
197197
case one = 0
198198
case two = 1
199199
case three = 2
200200
case fortyTwo = 3
201201
case fourHundredTwentyTwo = 4
202202
}
203203

204-
enum AnotherEnum: Int {
204+
enum AnotherEnum: Int, CaseIterable {
205205
case justInCase = 0
206206
}
207207

packages/pigeon/platform_tests/test_plugin/darwin/test_plugin/Sources/test_plugin/EventChannelTests.gen.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,15 @@ private func nilOrValue<T>(_ value: Any?) -> T? {
161161
return value as! T?
162162
}
163163

164-
enum EventEnum: Int {
164+
enum EventEnum: Int, CaseIterable {
165165
case one = 0
166166
case two = 1
167167
case three = 2
168168
case fortyTwo = 3
169169
case fourHundredTwentyTwo = 4
170170
}
171171

172-
enum AnotherEventEnum: Int {
172+
enum AnotherEventEnum: Int, CaseIterable {
173173
case justInCase = 0
174174
}
175175

packages/pigeon/platform_tests/test_plugin/darwin/test_plugin/Sources/test_plugin/ProxyApiTests.gen.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ private class ProxyApiTestsPigeonInternalProxyApiCodecReaderWriter: FlutterStand
590590
}
591591
}
592592

593-
enum ProxyApiTestEnum: Int {
593+
enum ProxyApiTestEnum: Int, CaseIterable {
594594
case one = 0
595595
case two = 1
596596
case three = 2

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,18 @@ struct EnumTests {
5757
}
5858
}
5959

60+
// Verifies that generated enums conform to `CaseIterable` and that
61+
// `allCases` returns every member in declaration order with the
62+
// expected raw values.
63+
@Test
64+
func enumIsCaseIterable() {
65+
let allCases = AnEnum.allCases
66+
#expect(allCases == [.one, .two, .three, .fortyTwo, .fourHundredTwentyTwo])
67+
#expect(allCases.count == 5)
68+
#expect(allCases.map { $0.rawValue } == [0, 1, 2, 3, 4])
69+
70+
// Single-member enums should also conform and report a single case.
71+
#expect(AnotherEnum.allCases == [.justInCase])
72+
}
73+
6074
}

packages/pigeon/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: pigeon
22
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
33
repository: https://github.com/flutter/packages/tree/main/packages/pigeon
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+pigeon%22
5-
version: 27.0.0 # This must match the version in lib/src/generator_tools.dart
5+
version: 27.1.0 # This must match the version in lib/src/generator_tools.dart
66

77
environment:
88
sdk: ^3.10.0

packages/pigeon/test/swift_generator_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void main() {
6161
const generator = SwiftGenerator();
6262
generator.generate(swiftOptions, root, sink, dartPackageName: DEFAULT_PACKAGE_NAME);
6363
final code = sink.toString();
64-
expect(code, contains('enum Foobar: Int'));
64+
expect(code, contains('enum Foobar: Int, CaseIterable'));
6565
expect(code, contains(' case one = 0'));
6666
expect(code, contains(' case two = 1'));
6767
expect(code, isNot(contains('if (')));

0 commit comments

Comments
 (0)