Visitar URL original
Enum support in Pigeon by GaryQian · Pull Request #331 · flutter/packages · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Dart root generator enum support:
  • Loading branch information
GaryQian committed May 14, 2021
commit ba2339dbf5094c5f702485fad3b686234011f8a8
19 changes: 13 additions & 6 deletions packages/pigeon/lib/dart_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ String _escapeForDartSingleQuotedString(String raw) {
.replaceAll(r"'", r"\'");
}

void _writeHostApi(DartOptions opt, Indent indent, Api api) {
void _writeHostApi(DartOptions opt, Indent indent, Api api, List<String> customEnumNames) {
assert(api.location == ApiLocation.host);
final String nullTag = opt.isNullSafe ? '?' : '';
final String unwrapOperator = opt.isNullSafe ? '!' : '';
Expand Down Expand Up @@ -49,7 +49,11 @@ final BinaryMessenger$nullTag _binaryMessenger;
if (func.argType != 'void') {
argSignature = '${func.argType} arg';
sendArgument = 'encoded';
encodedDeclaration = 'final Object encoded = arg.encode();';
if (customEnumNames.contains(func.argType)) {
encodedDeclaration = 'final Object encoded = arg.index;';
} else {
encodedDeclaration = 'final Object encoded = arg.encode();';
}
}
indent.write(
'Future<${func.returnType}> ${func.name}($argSignature) async ',
Expand All @@ -66,9 +70,12 @@ final BinaryMessenger$nullTag _binaryMessenger;
'\'$channelName\', const StandardMessageCodec(), binaryMessenger: _binaryMessenger);',
);
});
final String returnStatement = func.returnType == 'void'
? '// noop'
: 'return ${func.returnType}.decode(replyMap[\'${Keys.result}\']$unwrapOperator);';
String returnStatement = '// noop';
if (customEnumNames.contains(func.returnType)) {
returnStatement = 'return ${func.returnType}.values[replyMap[\'${Keys.result}\']$unwrapOperator];';
} else if (func.returnType != 'void') {
returnStatement = 'return ${func.returnType}.decode(replyMap[\'${Keys.result}\']$unwrapOperator);';
}
indent.format('''
final Map<Object$nullTag, Object$nullTag>$nullTag replyMap =\n\t\tawait channel.send($sendArgument) as Map<Object$nullTag, Object$nullTag>$nullTag;
if (replyMap == null) {
Expand Down Expand Up @@ -299,7 +306,7 @@ pigeonMap['${field.name}'] != null
for (final Api api in root.apis) {
indent.writeln('');
if (api.location == ApiLocation.host) {
_writeHostApi(opt, indent, api);
_writeHostApi(opt, indent, api, customEnumNames);
} else if (api.location == ApiLocation.flutter) {
_writeFlutterApi(opt, indent, api);
}
Expand Down
14 changes: 12 additions & 2 deletions packages/pigeon/lib/pigeon_lib.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class FlutterApi {
const FlutterApi();
}

/// Metadata to annotate PigeonEnums with to distinguish it from other
/// enums that exist in the library.
class PigeonEnum {
const PigeonEnum();
}
Expand Down Expand Up @@ -258,10 +260,18 @@ class Pigeon {
in apiMirror.declarations.values) {
if (declaration is MethodMirror && !declaration.isConstructor) {
if (!isVoid(declaration.returnType)) {
classes.add(declaration.returnType as ClassMirror);
if ((declaration.returnType as ClassMirror).isEnum) {
enums.add(declaration.returnType as ClassMirror);
} else {
classes.add(declaration.returnType as ClassMirror);
}
}
if (declaration.parameters.isNotEmpty) {
classes.add(declaration.parameters[0].type as ClassMirror);
if ((declaration.parameters[0].type as ClassMirror).isEnum) {
enums.add(declaration.parameters[0].type as ClassMirror);
} else {
classes.add(declaration.parameters[0].type as ClassMirror);
}
}
}
}
Expand Down