Skip to content

Commit

Permalink
isEnum, isClass, fix swift casting, default values
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrinneal committed Nov 11, 2023
1 parent 17bd92e commit bff1449
Show file tree
Hide file tree
Showing 34 changed files with 2,059 additions and 1,744 deletions.
7 changes: 7 additions & 0 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 13.2.0

* Adds named parameters to api methods.
* Adds optional parameters to api methods.
* Adds default values for class constructors and api methods.
* Adds `isEnum` and `isClass` to `TypeDeclaration`s.

## 13.1.0

* [swift] Fixes Flutter Api void return error handling.
Expand Down
8 changes: 4 additions & 4 deletions packages/pigeon/example/app/lib/src/messages.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class ExampleHostApi {
}
}

Future<int> add(int arg_a, int arg_b) async {
Future<int> add(int a, int b) async {
const String channelName =
'dev.flutter.pigeon.pigeon_example_package.ExampleHostApi.add';
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
Expand All @@ -139,7 +139,7 @@ class ExampleHostApi {
binaryMessenger: _binaryMessenger,
);
final List<Object?>? replyList =
await channel.send(<Object?>[arg_a, arg_b]) as List<Object?>?;
await channel.send(<Object?>[a, b]) as List<Object?>?;
if (replyList == null) {
throw _createConnectionError(channelName);
} else if (replyList.length > 1) {
Expand All @@ -158,7 +158,7 @@ class ExampleHostApi {
}
}

Future<bool> sendMessage(MessageData arg_message) async {
Future<bool> sendMessage(MessageData message) async {
const String channelName =
'dev.flutter.pigeon.pigeon_example_package.ExampleHostApi.sendMessage';
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
Expand All @@ -167,7 +167,7 @@ class ExampleHostApi {
binaryMessenger: _binaryMessenger,
);
final List<Object?>? replyList =
await channel.send(<Object?>[arg_message]) as List<Object?>?;
await channel.send(<Object?>[message]) as List<Object?>?;
if (replyList == null) {
throw _createConnectionError(channelName);
} else if (replyList.length > 1) {
Expand Down
83 changes: 75 additions & 8 deletions packages/pigeon/lib/ast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// ignore_for_file: avoid_equals_and_hash_code_on_mutable_classes

import 'package:collection/collection.dart' show ListEquality;
import 'package:meta/meta.dart';
import 'pigeon_lib.dart';

typedef _ListEquals = bool Function(List<Object?>, List<Object?>);
Expand Down Expand Up @@ -44,7 +45,7 @@ class Method extends Node {
TypeDeclaration returnType;

/// The arguments passed into the [Method].
List<NamedType> arguments;
List<Parameter> arguments;

/// Whether the receiver of this method is expected to return synchronously or not.
bool isAsynchronous;
Expand Down Expand Up @@ -115,19 +116,24 @@ class Api extends Node {
}

/// A specific instance of a type.
@immutable
class TypeDeclaration {
/// Constructor for [TypeDeclaration].
const TypeDeclaration({
TypeDeclaration({
required this.baseName,
required this.isNullable,
this.isEnum = false,
this.associatedEnum,
this.isClass = false,
this.associatedClass,
this.typeArguments = const <TypeDeclaration>[],
});

/// Void constructor.
const TypeDeclaration.voidDeclaration()
TypeDeclaration.voidDeclaration()
: baseName = 'void',
isNullable = false,
isEnum = false,
isClass = false,
typeArguments = const <TypeDeclaration>[];

/// The base name of the [TypeDeclaration] (ex 'Foo' to 'Foo<Bar>?').
Expand All @@ -142,6 +148,18 @@ class TypeDeclaration {
/// True if the type is nullable.
final bool isNullable;

/// Whether the [TypeDeclaration] represents an [Enum].
bool isEnum;

/// Associated [Enum], if any.
Enum? associatedEnum;

/// Whether the [TypeDeclaration] represents a [Class].
bool isClass;

/// Associated [Class], if any.
Class? associatedClass;

@override
int get hashCode {
// This has to be implemented because TypeDeclaration is used as a Key to a
Expand All @@ -163,15 +181,19 @@ class TypeDeclaration {
return other is TypeDeclaration &&
baseName == other.baseName &&
isNullable == other.isNullable &&
_listEquals(typeArguments, other.typeArguments);
_listEquals(typeArguments, other.typeArguments) &&
isEnum == other.isEnum &&
isClass == other.isClass &&
associatedClass == other.associatedClass &&
associatedEnum == other.associatedEnum;
}
}

@override
String toString() {
final String typeArgumentsStr =
typeArguments.isEmpty ? '' : 'typeArguments:$typeArguments';
return '(TypeDeclaration baseName:$baseName isNullable:$isNullable$typeArgumentsStr)';
return '(TypeDeclaration baseName:$baseName isNullable:$isNullable$typeArgumentsStr isEnum:$isEnum isClass:$isClass)';
}
}

Expand All @@ -182,6 +204,7 @@ class NamedType extends Node {
required this.name,
required this.type,
this.offset,
this.defaultValue,
this.documentationComments = const <String>[],
});

Expand All @@ -194,6 +217,9 @@ class NamedType extends Node {
/// The offset in the source file where the [NamedType] appears.
int? offset;

/// Stringified version of the default value of types that have default values.
String? defaultValue;

/// List of documentation comments, separated by line.
///
/// Lines should not include the comment marker itself, but should include any
Expand All @@ -203,7 +229,48 @@ class NamedType extends Node {

@override
String toString() {
return '(NamedType name:$name type:$type documentationComments:$documentationComments)';
return '(NamedType name:$name type:$type defaultValue:$defaultValue documentationComments:$documentationComments)';
}
}

/// Represents a [Method]'s parameter that has a type and a name.
class Parameter extends NamedType {
/// Parametric constructor for [Parameter].
Parameter({
required super.name,
required super.type,
super.offset,
super.defaultValue,
this.isNamed = true,
this.isOptional = false,
this.isPositional = true,
this.isRequired = true,
super.documentationComments,
});

/// Return `true` if this parameter is a named parameter.
///
/// Defaults to `true`.
bool isNamed;

/// Return `true` if this parameter is an optional parameter.
///
/// Defaults to `false`.
bool isOptional;

/// Return `true` if this parameter is a positional parameter.
///
/// Defaults to `true`.
bool isPositional;

/// Return `true` if this parameter is a required parameter.
///
/// Defaults to `true`.
bool isRequired;

@override
String toString() {
return '(Parameter name:$name type:$type isNamed:$isNamed isOptional:$isOptional isPositional:$isPositional isRequired:$isRequired documentationComments:$documentationComments)';
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/lib/ast_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import 'ast.dart';
import 'generator_tools.dart';

/// Writes the AST represention of [root] to [sink].
/// Writes the AST representation of [root] to [sink].
void generateAst(Root root, StringSink sink) {
final Indent indent = Indent(sink);
final String output = root.toString();
Expand Down
Loading

0 comments on commit bff1449

Please sign in to comment.