mirror of
https://github.com/flutter/packages.git
synced 2025-07-01 07:08:10 +08:00
[pigeon] Fix C++ enum naming (#7094)
The C++ generator wasn't adjusting enum value names, so they were just using the Dart camelCase style. This makes them style-guide compliant by adding the `k` prefix and making the first actual letter upper-case. Fixes https://github.com/flutter/flutter/issues/147328
This commit is contained in:
@ -1,3 +1,9 @@
|
|||||||
|
## 21.0.0
|
||||||
|
|
||||||
|
* **Breaking Change** [cpp] Fixes style of enum names. References to enum values
|
||||||
|
will need to be updated to `EnumType.kValue` style, instead of the previous
|
||||||
|
`EnumType.value`.
|
||||||
|
|
||||||
## 20.0.2
|
## 20.0.2
|
||||||
|
|
||||||
* [java] Adds `equals` and `hashCode` support for data classes.
|
* [java] Adds `equals` and `hashCode` support for data classes.
|
||||||
|
@ -182,7 +182,7 @@ class PigeonApiImplementation : public ExampleHostApi {
|
|||||||
}
|
}
|
||||||
void SendMessage(const MessageData& message,
|
void SendMessage(const MessageData& message,
|
||||||
std::function<void(ErrorOr<bool> reply)> result) {
|
std::function<void(ErrorOr<bool> reply)> result) {
|
||||||
if (message.code == Code.one) {
|
if (message.code == Code.kOne) {
|
||||||
result(FlutterError("code", "message", "details"));
|
result(FlutterError("code", "message", "details"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -29,7 +29,7 @@ class PigeonApiImplementation : public ExampleHostApi {
|
|||||||
}
|
}
|
||||||
void SendMessage(const MessageData& message,
|
void SendMessage(const MessageData& message,
|
||||||
std::function<void(ErrorOr<bool> reply)> result) {
|
std::function<void(ErrorOr<bool> reply)> result) {
|
||||||
if (message.code == Code.one) {
|
if (message.code == Code.kOne) {
|
||||||
result(FlutterError("code", "message", "details"));
|
result(FlutterError("code", "message", "details"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -59,7 +59,7 @@ class ErrorOr {
|
|||||||
std::variant<T, FlutterError> v_;
|
std::variant<T, FlutterError> v_;
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class Code { one = 0, two = 1 };
|
enum class Code { kOne = 0, kTwo = 1 };
|
||||||
|
|
||||||
// Generated class from Pigeon that represents data sent in messages.
|
// Generated class from Pigeon that represents data sent in messages.
|
||||||
class MessageData {
|
class MessageData {
|
||||||
|
@ -181,8 +181,9 @@ class CppHeaderGenerator extends StructuredGenerator<CppOptions> {
|
|||||||
enumerate(anEnum.members, (int index, final EnumMember member) {
|
enumerate(anEnum.members, (int index, final EnumMember member) {
|
||||||
addDocumentationComments(
|
addDocumentationComments(
|
||||||
indent, member.documentationComments, _docCommentSpec);
|
indent, member.documentationComments, _docCommentSpec);
|
||||||
|
final String valueName = 'k${_pascalCaseFromCamelCase(member.name)}';
|
||||||
indent.writeln(
|
indent.writeln(
|
||||||
'${member.name} = $index${index == anEnum.members.length - 1 ? '' : ','}');
|
'$valueName = $index${index == anEnum.members.length - 1 ? '' : ','}');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ import 'ast.dart';
|
|||||||
/// The current version of pigeon.
|
/// The current version of pigeon.
|
||||||
///
|
///
|
||||||
/// This must match the version in pubspec.yaml.
|
/// This must match the version in pubspec.yaml.
|
||||||
const String pigeonVersion = '20.0.2';
|
const String pigeonVersion = '21.0.0';
|
||||||
|
|
||||||
/// Prefix for all local variables in methods.
|
/// Prefix for all local variables in methods.
|
||||||
///
|
///
|
||||||
|
@ -66,11 +66,11 @@ class ErrorOr {
|
|||||||
};
|
};
|
||||||
|
|
||||||
enum class AnEnum {
|
enum class AnEnum {
|
||||||
one = 0,
|
kOne = 0,
|
||||||
two = 1,
|
kTwo = 1,
|
||||||
three = 2,
|
kThree = 2,
|
||||||
fortyTwo = 3,
|
kFortyTwo = 3,
|
||||||
fourHundredTwentyTwo = 4
|
kFourHundredTwentyTwo = 4
|
||||||
};
|
};
|
||||||
|
|
||||||
// A class containing all supported types.
|
// A class containing all supported types.
|
||||||
|
@ -66,13 +66,13 @@ TEST(NullFields, BuildWithValues) {
|
|||||||
reply.set_error("error");
|
reply.set_error("error");
|
||||||
reply.set_indices(EncodableList({1, 2, 3}));
|
reply.set_indices(EncodableList({1, 2, 3}));
|
||||||
reply.set_request(request);
|
reply.set_request(request);
|
||||||
reply.set_type(NullFieldsSearchReplyType::success);
|
reply.set_type(NullFieldsSearchReplyType::kSuccess);
|
||||||
|
|
||||||
EXPECT_EQ(*reply.result(), "result");
|
EXPECT_EQ(*reply.result(), "result");
|
||||||
EXPECT_EQ(*reply.error(), "error");
|
EXPECT_EQ(*reply.error(), "error");
|
||||||
EXPECT_EQ(reply.indices()->size(), 3);
|
EXPECT_EQ(reply.indices()->size(), 3);
|
||||||
EXPECT_EQ(*reply.request()->query(), "hello");
|
EXPECT_EQ(*reply.request()->query(), "hello");
|
||||||
EXPECT_EQ(*reply.type(), NullFieldsSearchReplyType::success);
|
EXPECT_EQ(*reply.type(), NullFieldsSearchReplyType::kSuccess);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(NullFields, BuildRequestWithNulls) {
|
TEST(NullFields, BuildRequestWithNulls) {
|
||||||
@ -121,7 +121,7 @@ TEST_F(NullFieldsTest, ReplyFromListWithValues) {
|
|||||||
EncodableValue("error"),
|
EncodableValue("error"),
|
||||||
EncodableValue(EncodableList({1, 2, 3})),
|
EncodableValue(EncodableList({1, 2, 3})),
|
||||||
CustomEncodableValue(request),
|
CustomEncodableValue(request),
|
||||||
CustomEncodableValue(NullFieldsSearchReplyType::success),
|
CustomEncodableValue(NullFieldsSearchReplyType::kSuccess),
|
||||||
};
|
};
|
||||||
NullFieldsSearchReply reply = ReplyFromList(list);
|
NullFieldsSearchReply reply = ReplyFromList(list);
|
||||||
|
|
||||||
@ -130,7 +130,7 @@ TEST_F(NullFieldsTest, ReplyFromListWithValues) {
|
|||||||
EXPECT_EQ(reply.indices()->size(), 3);
|
EXPECT_EQ(reply.indices()->size(), 3);
|
||||||
EXPECT_EQ(*reply.request()->query(), "hello");
|
EXPECT_EQ(*reply.request()->query(), "hello");
|
||||||
EXPECT_EQ(reply.request()->identifier(), 1);
|
EXPECT_EQ(reply.request()->identifier(), 1);
|
||||||
EXPECT_EQ(*reply.type(), NullFieldsSearchReplyType::success);
|
EXPECT_EQ(*reply.type(), NullFieldsSearchReplyType::kSuccess);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(NullFieldsTest, ReplyFromListWithNulls) {
|
TEST_F(NullFieldsTest, ReplyFromListWithNulls) {
|
||||||
@ -178,7 +178,7 @@ TEST_F(NullFieldsTest, ReplyToMapWithValues) {
|
|||||||
reply.set_error("error");
|
reply.set_error("error");
|
||||||
reply.set_indices(EncodableList({1, 2, 3}));
|
reply.set_indices(EncodableList({1, 2, 3}));
|
||||||
reply.set_request(request);
|
reply.set_request(request);
|
||||||
reply.set_type(NullFieldsSearchReplyType::success);
|
reply.set_type(NullFieldsSearchReplyType::kSuccess);
|
||||||
|
|
||||||
const EncodableList list = ListFromReply(reply);
|
const EncodableList list = ListFromReply(reply);
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@ name: pigeon
|
|||||||
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
|
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
|
||||||
repository: https://github.com/flutter/packages/tree/main/packages/pigeon
|
repository: https://github.com/flutter/packages/tree/main/packages/pigeon
|
||||||
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+pigeon%22
|
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+pigeon%22
|
||||||
version: 20.0.2 # This must match the version in lib/generator_tools.dart
|
version: 21.0.0 # This must match the version in lib/generator_tools.dart
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
sdk: ^3.2.0
|
sdk: ^3.2.0
|
||||||
|
@ -101,6 +101,10 @@ void main() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('naming follows style', () {
|
test('naming follows style', () {
|
||||||
|
final Enum anEnum = Enum(name: 'AnEnum', members: <EnumMember>[
|
||||||
|
EnumMember(name: 'one'),
|
||||||
|
EnumMember(name: 'fortyTwo'),
|
||||||
|
]);
|
||||||
final Root root = Root(apis: <Api>[
|
final Root root = Root(apis: <Api>[
|
||||||
AstHostApi(name: 'Api', methods: <Method>[
|
AstHostApi(name: 'Api', methods: <Method>[
|
||||||
Method(
|
Method(
|
||||||
@ -137,9 +141,19 @@ void main() {
|
|||||||
baseName: 'bool',
|
baseName: 'bool',
|
||||||
isNullable: false,
|
isNullable: false,
|
||||||
),
|
),
|
||||||
name: 'outputField')
|
name: 'outputField'),
|
||||||
|
NamedType(
|
||||||
|
type: TypeDeclaration(
|
||||||
|
baseName: anEnum.name,
|
||||||
|
isNullable: false,
|
||||||
|
associatedEnum: anEnum,
|
||||||
|
),
|
||||||
|
name: 'code',
|
||||||
|
)
|
||||||
])
|
])
|
||||||
], enums: <Enum>[]);
|
], enums: <Enum>[
|
||||||
|
anEnum
|
||||||
|
]);
|
||||||
{
|
{
|
||||||
final StringBuffer sink = StringBuffer();
|
final StringBuffer sink = StringBuffer();
|
||||||
const CppGenerator generator = CppGenerator();
|
const CppGenerator generator = CppGenerator();
|
||||||
@ -161,6 +175,9 @@ void main() {
|
|||||||
// Instance variables should be adjusted.
|
// Instance variables should be adjusted.
|
||||||
expect(code, contains('bool input_field_'));
|
expect(code, contains('bool input_field_'));
|
||||||
expect(code, contains('bool output_field_'));
|
expect(code, contains('bool output_field_'));
|
||||||
|
// Enum values should be adjusted.
|
||||||
|
expect(code, contains('kOne'));
|
||||||
|
expect(code, contains('kFortyTwo'));
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
final StringBuffer sink = StringBuffer();
|
final StringBuffer sink = StringBuffer();
|
||||||
|
Reference in New Issue
Block a user