Skip to content

Commit 90baeee

Browse files
authored
[pigeon] Implement Screaming Snake Case Conversion for Kotlin Enum Cases (#5918)
This pull request addresses issue flutter/flutter#140938 in the Pigeon package, related to the naming convention of Kotlin enum cases generated from lower camel case Dart enums. The current implementation concatenates the enum cases in uppercase, deviating from the Kotlin naming convention, specifically when dealing with multi-word names. Changes - Kotlin Enum Generation: Modified the writeEnum function in the Pigeon package to ensure the Kotlin generator produces enum cases in SCREAMING_SNAKE_CASE. This adheres to the Kotlin coding conventions and allows a consistent cross-platform enum naming convention across Dart, Kotlin, and Swift. - Regex Handling: Enhanced the regex pattern to correctly transform lower camel case names to screaming snake case, considering edge cases involving numbers and special characters. - Testing: Updated the Dart unit tests to include cases for validating the correct transformation of multi-word and complex enum names from lower camel case to screaming snake case.
1 parent cd45100 commit 90baeee

18 files changed

Lines changed: 112 additions & 18 deletions

File tree

packages/pigeon/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## 17.0.0
2+
3+
* **Breaking Change** [kotlin] Converts Kotlin enum case generation to SCREAMING_SNAKE_CASE.
4+
* Updates `writeEnum` function to adhere to Kotlin naming conventions.
5+
* Improves handling of complex names with enhanced regex patterns.
6+
* Expands unit tests for comprehensive name conversion validation.
7+
* **Migration Note**: This change modifies the naming convention of Kotlin enum cases generated from the Pigeon package. It is recommended to review the impact on your existing codebase and update any dependent code accordingly.
8+
19
## 16.0.5
210

311
* Adds ProxyApi to AST generation.

packages/pigeon/lib/generator_tools.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import 'ast.dart';
1313
/// The current version of pigeon.
1414
///
1515
/// This must match the version in pubspec.yaml.
16-
const String pigeonVersion = '16.0.5';
16+
const String pigeonVersion = '17.0.0';
1717

1818
/// Read all the content from [stdin] to a String.
1919
String readStdin() {

packages/pigeon/lib/kotlin_generator.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,11 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
127127
enumerate(anEnum.members, (int index, final EnumMember member) {
128128
addDocumentationComments(
129129
indent, member.documentationComments, _docCommentSpec);
130-
indent.write('${member.name.toUpperCase()}($index)');
130+
final String nameScreamingSnakeCase = member.name
131+
.replaceAllMapped(
132+
RegExp(r'(?<=[a-z])[A-Z]'), (Match m) => '_${m.group(0)}')
133+
.toUpperCase();
134+
indent.write('$nameScreamingSnakeCase($index)');
131135
if (index != anEnum.members.length - 1) {
132136
indent.addln(',');
133137
} else {

packages/pigeon/pigeons/core_tests.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ enum AnEnum {
88
one,
99
two,
1010
three,
11+
fortyTwo,
12+
fourHundredTwentyTwo,
1113
}
1214

1315
/// A class containing all supported types.

packages/pigeon/pigeons/enum.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ enum EnumState {
1818

1919
/// This comment is to test enum member (Error) documentation comments.
2020
Error,
21+
22+
/// This comment is to test enum member (SnakeCase) documentation comments.
23+
SnakeCase,
2124
}
2225

2326
/// This comment is to test class documentation comments.

packages/pigeon/platform_tests/alternate_language_test_plugin/android/src/main/java/com/example/alternate_language_test_plugin/CoreTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ protected static FlutterError createConnectionError(@NonNull String channelName)
7777
public enum AnEnum {
7878
ONE(0),
7979
TWO(1),
80-
THREE(2);
80+
THREE(2),
81+
FORTY_TWO(3),
82+
FOUR_HUNDRED_TWENTY_TWO(4);
8183

8284
final int index;
8385

packages/pigeon/platform_tests/alternate_language_test_plugin/ios/Classes/CoreTests.gen.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ typedef NS_ENUM(NSUInteger, AnEnum) {
1818
AnEnumOne = 0,
1919
AnEnumTwo = 1,
2020
AnEnumThree = 2,
21+
AnEnumFortyTwo = 3,
22+
AnEnumFourHundredTwentyTwo = 4,
2123
};
2224

2325
/// Wrapper for AnEnum to allow for nullability.

packages/pigeon/platform_tests/alternate_language_test_plugin/macos/Classes/CoreTests.gen.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ typedef NS_ENUM(NSUInteger, AnEnum) {
1818
AnEnumOne = 0,
1919
AnEnumTwo = 1,
2020
AnEnumThree = 2,
21+
AnEnumFortyTwo = 3,
22+
AnEnumFourHundredTwentyTwo = 4,
2123
};
2224

2325
/// Wrapper for AnEnum to allow for nullability.

packages/pigeon/platform_tests/shared_test_plugin_code/lib/integration_tests.dart

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ void runPigeonIntegrationTests(TargetGenerator targetGenerator) {
138138
'd': false,
139139
'e': null
140140
},
141-
anEnum: AnEnum.two,
141+
anEnum: AnEnum.fortyTwo,
142142
anObject: 1,
143143
);
144144

@@ -166,7 +166,7 @@ void runPigeonIntegrationTests(TargetGenerator targetGenerator) {
166166
],
167167
nullableMapWithAnnotations: <String?, String?>{},
168168
nullableMapWithObject: <String?, Object?>{},
169-
aNullableEnum: AnEnum.two,
169+
aNullableEnum: AnEnum.fourHundredTwentyTwo,
170170
aNullableObject: 0,
171171
);
172172

@@ -443,6 +443,15 @@ void runPigeonIntegrationTests(TargetGenerator targetGenerator) {
443443
expect(receivedEnum, sentEnum);
444444
});
445445

446+
testWidgets('multi word enums serialize and deserialize correctly',
447+
(WidgetTester _) async {
448+
final HostIntegrationCoreApi api = HostIntegrationCoreApi();
449+
450+
const AnEnum sentEnum = AnEnum.fortyTwo;
451+
final AnEnum receivedEnum = await api.echoEnum(sentEnum);
452+
expect(receivedEnum, sentEnum);
453+
});
454+
446455
testWidgets('required named parameter', (WidgetTester _) async {
447456
final HostIntegrationCoreApi api = HostIntegrationCoreApi();
448457
// This number corresponds with the default value of this method.
@@ -648,6 +657,15 @@ void runPigeonIntegrationTests(TargetGenerator targetGenerator) {
648657
expect(echoEnum, sentEnum);
649658
});
650659

660+
testWidgets('multi word nullable enums serialize and deserialize correctly',
661+
(WidgetTester _) async {
662+
final HostIntegrationCoreApi api = HostIntegrationCoreApi();
663+
664+
const AnEnum sentEnum = AnEnum.fourHundredTwentyTwo;
665+
final AnEnum? echoEnum = await api.echoNullableEnum(sentEnum);
666+
expect(echoEnum, sentEnum);
667+
});
668+
651669
testWidgets('null lists serialize and deserialize correctly',
652670
(WidgetTester _) async {
653671
final HostIntegrationCoreApi api = HostIntegrationCoreApi();
@@ -887,6 +905,15 @@ void runPigeonIntegrationTests(TargetGenerator targetGenerator) {
887905
expect(echoEnum, sentEnum);
888906
});
889907

908+
testWidgets('multi word enums serialize and deserialize correctly',
909+
(WidgetTester _) async {
910+
final HostIntegrationCoreApi api = HostIntegrationCoreApi();
911+
912+
const AnEnum sentEnum = AnEnum.fourHundredTwentyTwo;
913+
final AnEnum echoEnum = await api.echoAsyncEnum(sentEnum);
914+
expect(echoEnum, sentEnum);
915+
});
916+
890917
testWidgets('nullable Int async serialize and deserialize correctly',
891918
(WidgetTester _) async {
892919
final HostIntegrationCoreApi api = HostIntegrationCoreApi();
@@ -1004,6 +1031,15 @@ void runPigeonIntegrationTests(TargetGenerator targetGenerator) {
10041031
expect(echoEnum, sentEnum);
10051032
});
10061033

1034+
testWidgets('nullable enums serialize and deserialize correctly',
1035+
(WidgetTester _) async {
1036+
final HostIntegrationCoreApi api = HostIntegrationCoreApi();
1037+
1038+
const AnEnum sentEnum = AnEnum.fortyTwo;
1039+
final AnEnum? echoEnum = await api.echoAsyncNullableEnum(sentEnum);
1040+
expect(echoEnum, sentEnum);
1041+
});
1042+
10071043
testWidgets('null Ints async serialize and deserialize correctly',
10081044
(WidgetTester _) async {
10091045
final HostIntegrationCoreApi api = HostIntegrationCoreApi();
@@ -1243,6 +1279,15 @@ void runPigeonIntegrationTests(TargetGenerator targetGenerator) {
12431279
expect(echoEnum, sentEnum);
12441280
});
12451281

1282+
testWidgets('multi word enums serialize and deserialize correctly',
1283+
(WidgetTester _) async {
1284+
final HostIntegrationCoreApi api = HostIntegrationCoreApi();
1285+
1286+
const AnEnum sentEnum = AnEnum.fortyTwo;
1287+
final AnEnum echoEnum = await api.callFlutterEchoEnum(sentEnum);
1288+
expect(echoEnum, sentEnum);
1289+
});
1290+
12461291
testWidgets('nullable booleans serialize and deserialize correctly',
12471292
(WidgetTester _) async {
12481293
final HostIntegrationCoreApi api = HostIntegrationCoreApi();
@@ -1407,6 +1452,15 @@ void runPigeonIntegrationTests(TargetGenerator targetGenerator) {
14071452
expect(echoEnum, sentEnum);
14081453
});
14091454

1455+
testWidgets('multi word nullable enums serialize and deserialize correctly',
1456+
(WidgetTester _) async {
1457+
final HostIntegrationCoreApi api = HostIntegrationCoreApi();
1458+
1459+
const AnEnum sentEnum = AnEnum.fourHundredTwentyTwo;
1460+
final AnEnum? echoEnum = await api.callFlutterEchoNullableEnum(sentEnum);
1461+
expect(echoEnum, sentEnum);
1462+
});
1463+
14101464
testWidgets('null enums serialize and deserialize correctly',
14111465
(WidgetTester _) async {
14121466
final HostIntegrationCoreApi api = HostIntegrationCoreApi();

packages/pigeon/platform_tests/shared_test_plugin_code/lib/src/generated/core_tests.gen.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ enum AnEnum {
3434
one,
3535
two,
3636
three,
37+
fortyTwo,
38+
fourHundredTwentyTwo,
3739
}
3840

3941
/// A class containing all supported types.

0 commit comments

Comments
 (0)