Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pigeon] Minor fixes to NNBD type handling in Dart #1543

Merged
merged 6 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
5 changes: 5 additions & 0 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 3.0.2

* Fixes non-nullable classes and enums as fields.
gaaclarke marked this conversation as resolved.
Show resolved Hide resolved
* Fixes nullable collections as return types.

## 3.0.0

* **BREAKING CHANGE**: Removes the `--dart_null_safety` flag. Generated Dart
Expand Down
36 changes: 26 additions & 10 deletions packages/pigeon/lib/dart_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,11 @@ final BinaryMessenger? _binaryMessenger;
final String returnType = _makeGenericTypeArguments(func.returnType);
final String castCall = _makeGenericCastCall(func.returnType);
const String accessor = 'replyMap[\'${Keys.result}\']';
final String unwrapper = func.returnType.isNullable ? '' : '!';
final String nullHandler =
func.returnType.isNullable ? (castCall.isEmpty ? '' : '?') : '!';
final String returnStatement = func.returnType.isVoid
? 'return;'
: 'return ($accessor as $returnType?)$unwrapper$castCall;';
: 'return ($accessor as $returnType?)$nullHandler$castCall;';
indent.format('''
final Map<Object?, Object?>? replyMap =\n\t\tawait channel.send($sendArgument) as Map<Object?, Object?>?;
if (replyMap == null) {
Expand Down Expand Up @@ -459,13 +460,14 @@ void generateDart(DartOptions opt, Root root, StringSink sink) {
);
for (final NamedType field in klass.fields) {
indent.write('pigeonMap[\'${field.name}\'] = ');
final String conditional = field.type.isNullable ? '?' : '';
if (customClassNames.contains(field.type.baseName)) {
indent.addln(
'${field.name}?.encode();',
'${field.name}$conditional.encode();',
);
} else if (customEnumNames.contains(field.type.baseName)) {
indent.addln(
'${field.name}?.index;',
'${field.name}$conditional.index;',
);
} else {
indent.addln('${field.name};');
Expand All @@ -478,15 +480,29 @@ void generateDart(DartOptions opt, Root root, StringSink sink) {
void writeDecode() {
void writeValueDecode(NamedType field) {
if (customClassNames.contains(field.type.baseName)) {
indent.format('''
final String nonNullValue =
"${field.type.baseName}.decode(pigeonMap['${field.name}']!)";
indent.format(
field.type.isNullable
? '''
pigeonMap['${field.name}'] != null
\t\t? ${field.type.baseName}.decode(pigeonMap['${field.name}']!)
\t\t: null''', leadingSpace: false, trailingNewline: false);
\t\t? $nonNullValue
\t\t: null'''
: nonNullValue,
leadingSpace: false,
trailingNewline: false);
} else if (customEnumNames.contains(field.type.baseName)) {
indent.format('''
final String nonNullValue =
"${field.type.baseName}.values[pigeonMap['${field.name}']! as int]";
indent.format(
field.type.isNullable
? '''
pigeonMap['${field.name}'] != null
\t\t? ${field.type.baseName}.values[pigeonMap['${field.name}']! as int]
\t\t: null''', leadingSpace: false, trailingNewline: false);
\t\t? $nonNullValue
\t\t: null'''
: nonNullValue,
leadingSpace: false,
trailingNewline: false);
} else if (field.type.typeArguments.isNotEmpty) {
final String genericType = _makeGenericTypeArguments(field.type);
final String castCall = _makeGenericCastCall(field.type);
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/lib/generator_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'dart:mirrors';
import 'ast.dart';

/// The current version of pigeon. This must match the version in pubspec.yaml.
const String pigeonVersion = '3.0.0';
const String pigeonVersion = '3.0.2';

/// Read all the content from [stdin] to a String.
String readStdin() {
Expand Down
12 changes: 11 additions & 1 deletion packages/pigeon/pigeons/non_null_fields.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,21 @@ class SearchRequest {
String query;
}

class ExtraData {
ExtraData({required this.detailA, required this.detailB});
String detailA;
String detailB;
}

enum ReplyType { success, error }

class SearchReply {
SearchReply(this.result, this.error, this.indices);
SearchReply(this.result, this.error, this.indices, this.extraData, this.type);
String result;
String error;
List<int?> indices;
ExtraData extraData;
ReplyType type;
}

@HostApi()
Expand Down
24 changes: 22 additions & 2 deletions packages/pigeon/pigeons/nullable_returns.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import 'package:pigeon/pigeon.dart';

@HostApi()
abstract class NonNullHostApi {
abstract class NullableReturnHostApi {
int? doit();
}

@FlutterApi()
abstract class NonNullFlutterApi {
abstract class NullableReturnFlutterApi {
int? doit();
}

Expand All @@ -26,3 +26,23 @@ abstract class NullableArgHostApi {
abstract class NullableArgFlutterApi {
int doit(int? x);
}

@HostApi()
abstract class NullableCollectionReturnHostApi {
List<String?>? doit();
}

@FlutterApi()
abstract class NullableCollectionReturnFlutterApi {
List<String?>? doit();
}

@HostApi()
abstract class NullableCollectionArgHostApi {
List<String?> doit(List<String?>? x);
}

@FlutterApi()
abstract class NullableCollectionArgFlutterApi {
List<String?> doit(List<String?>? x);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Autogenerated from Pigeon (v2.0.4), do not edit directly.
// Autogenerated from Pigeon (v3.0.2), do not edit directly.
// See also: https://pub.dev/packages/pigeon
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators, omit_local_variable_types, unused_shown_name
// @dart = 2.12
Expand All @@ -12,6 +12,11 @@ import 'dart:typed_data' show Uint8List, Int32List, Int64List, Float64List;
import 'package:flutter/foundation.dart' show WriteBuffer, ReadBuffer;
import 'package:flutter/services.dart';

enum ReplyType {
success,
error,
}

class SearchRequest {
SearchRequest({
required this.query,
Expand All @@ -33,22 +38,53 @@ class SearchRequest {
}
}

class ExtraData {
ExtraData({
required this.detailA,
required this.detailB,
});

String detailA;
String detailB;

Object encode() {
final Map<Object?, Object?> pigeonMap = <Object?, Object?>{};
pigeonMap['detailA'] = detailA;
pigeonMap['detailB'] = detailB;
return pigeonMap;
}

static ExtraData decode(Object message) {
final Map<Object?, Object?> pigeonMap = message as Map<Object?, Object?>;
return ExtraData(
detailA: pigeonMap['detailA']! as String,
detailB: pigeonMap['detailB']! as String,
);
}
}

class SearchReply {
SearchReply({
required this.result,
required this.error,
required this.indices,
required this.extraData,
required this.type,
});

String result;
String error;
List<int?> indices;
ExtraData extraData;
ReplyType type;

Object encode() {
final Map<Object?, Object?> pigeonMap = <Object?, Object?>{};
pigeonMap['result'] = result;
pigeonMap['error'] = error;
pigeonMap['indices'] = indices;
pigeonMap['extraData'] = extraData.encode();
pigeonMap['type'] = type.index;
return pigeonMap;
}

Expand All @@ -58,6 +94,8 @@ class SearchReply {
result: pigeonMap['result']! as String,
error: pigeonMap['error']! as String,
indices: (pigeonMap['indices'] as List<Object?>?)!.cast<int?>(),
extraData: ExtraData.decode(pigeonMap['extraData']!),
type: ReplyType.values[pigeonMap['type']! as int],
);
}
}
Expand All @@ -66,12 +104,15 @@ class _NonNullHostApiCodec extends StandardMessageCodec {
const _NonNullHostApiCodec();
@override
void writeValue(WriteBuffer buffer, Object? value) {
if (value is SearchReply) {
if (value is ExtraData) {
buffer.putUint8(128);
writeValue(buffer, value.encode());
} else if (value is SearchRequest) {
} else if (value is SearchReply) {
buffer.putUint8(129);
writeValue(buffer, value.encode());
} else if (value is SearchRequest) {
buffer.putUint8(130);
writeValue(buffer, value.encode());
} else {
super.writeValue(buffer, value);
}
Expand All @@ -81,9 +122,12 @@ class _NonNullHostApiCodec extends StandardMessageCodec {
Object? readValueOfType(int type, ReadBuffer buffer) {
switch (type) {
case 128:
return SearchReply.decode(readValue(buffer)!);
return ExtraData.decode(readValue(buffer)!);

case 129:
return SearchReply.decode(readValue(buffer)!);

case 130:
return SearchRequest.decode(readValue(buffer)!);

default:
Expand Down Expand Up @@ -137,12 +181,15 @@ class _NonNullFlutterApiCodec extends StandardMessageCodec {
const _NonNullFlutterApiCodec();
@override
void writeValue(WriteBuffer buffer, Object? value) {
if (value is SearchReply) {
if (value is ExtraData) {
buffer.putUint8(128);
writeValue(buffer, value.encode());
} else if (value is SearchRequest) {
} else if (value is SearchReply) {
buffer.putUint8(129);
writeValue(buffer, value.encode());
} else if (value is SearchRequest) {
buffer.putUint8(130);
writeValue(buffer, value.encode());
} else {
super.writeValue(buffer, value);
}
Expand All @@ -152,9 +199,12 @@ class _NonNullFlutterApiCodec extends StandardMessageCodec {
Object? readValueOfType(int type, ReadBuffer buffer) {
switch (type) {
case 128:
return SearchReply.decode(readValue(buffer)!);
return ExtraData.decode(readValue(buffer)!);

case 129:
return SearchReply.decode(readValue(buffer)!);

case 130:
return SearchRequest.decode(readValue(buffer)!);

default:
Expand Down
Loading