Skip to content

Commit b78d7e9

Browse files
committed
toString and isNullish
1 parent cf84667 commit b78d7e9

59 files changed

Lines changed: 23608 additions & 10894 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/pigeon/CHANGELOG.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 26.4.0
2+
3+
* Overrides `toString` (or equivalent) methods on generated data classes.
4+
* [swift] Updates `isNullish` to handle double nested `Any?` values.
5+
16
## 26.3.4
27

38
* [kotlin] Updates generated error class to inherit from `RuntimeException`
@@ -915,7 +920,7 @@
915920

916921
## 3.0.3
917922

918-
* Adds ability for generators to do AST validation. This can help generators
923+
* Adds ability for generators to do AST validation. This can help generators
919924
without complete implementations to report gaps in coverage.
920925

921926
## 3.0.2
@@ -1078,11 +1083,11 @@
10781083
`dart:mirrors` doesn't support null-safe code so there were a class of
10791084
features we couldn't implement without this migration.
10801085
* **BREAKING CHANGE** - the `configurePigeon` function has been migrated to a
1081-
`@ConfigurePigeon` annotation. See `./pigeons/message.dart` for an example.
1086+
`@ConfigurePigeon` annotation. See `./pigeons/message.dart` for an example.
10821087
The annotation can be attached to anything in the file to take effect.
10831088
* **BREAKING CHANGE** - Now Pigeon files must be in one file per invocation of
1084-
Pigeon. For example, the classes your APIs use must be in the same file as
1085-
your APIs. If your Pigeon file imports another source file, it won't actually
1089+
Pigeon. For example, the classes your APIs use must be in the same file as
1090+
your APIs. If your Pigeon file imports another source file, it won't actually
10861091
import it.
10871092

10881093
## 0.2.4
@@ -1104,10 +1109,10 @@
11041109

11051110
## 0.2.0
11061111

1107-
* **BREAKING CHANGE** - Pigeon files must be null-safe now. That means the
1112+
* **BREAKING CHANGE** - Pigeon files must be null-safe now. That means the
11081113
fields inside of the classes must be declared nullable (
11091114
[non-null fields](https://github.com/flutter/flutter/issues/59118) aren't yet
1110-
supported). Migration example:
1115+
supported). Migration example:
11111116

11121117
```dart
11131118
// Version 0.1.x
@@ -1123,7 +1128,7 @@ class Foo {
11231128
}
11241129
```
11251130

1126-
* **BREAKING CHANGE** - The default output from Pigeon is now null-safe. If you
1131+
* **BREAKING CHANGE** - The default output from Pigeon is now null-safe. If you
11271132
want non-null-safe code you must provide the `--no-dart_null_safety` flag.
11281133
* The Pigeon source code is now null-safe.
11291134
* Fixed niladic non-value returning async functions in the Java generator.
@@ -1209,7 +1214,7 @@ class Foo {
12091214

12101215
## 0.1.8
12111216

1212-
* Started spawning pigeon_lib in an isolate instead of a subprocess. The
1217+
* Started spawning pigeon_lib in an isolate instead of a subprocess. The
12131218
subprocess could have lead to errors if the dart version on $PATH didn't match
12141219
the one that comes with flutter.
12151220

packages/pigeon/CONTRIBUTING.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## Description
44

55
Pigeon is a code generation tool that adds type safety to Flutter’s Platform
6-
Channels. This document serves as an overview of how it functions to help
6+
Channels. This document serves as an overview of how it functions to help
77
people who would like to contribute to the project.
88

99
## Source Index
@@ -32,15 +32,15 @@ Pigeon has 3 types of tests; you'll find them all in
3232

3333
* Unit tests - These are the fastest tests that are just typical unit tests,
3434
they may be generating code and checking it against a regular expression to
35-
see if it's correct. Example:
35+
see if it's correct. Example:
3636
[dart_generator_test.dart](./test/dart_generator_test.dart)
3737
* Compilation tests - These tests generate code, then attempt to compile that
38-
code. These are tests are much slower than unit tests, but not as slow as
39-
integration tests. These tests are typically run against the Pigeon files in
38+
code. These are tests are much slower than unit tests, but not as slow as
39+
integration tests. These tests are typically run against the Pigeon files in
4040
[pigeons](./pigeons).
4141
* Integration tests - These tests generate code, then compile the generated
42-
code, then execute the generated code. It can be thought of as unit-tests run
43-
against the generated code. Examples: [platform_tests](./platform_tests)
42+
code, then execute the generated code. It can be thought of as unit-tests run
43+
against the generated code. Examples: [platform_tests](./platform_tests)
4444

4545
For local testing, always use `test.dart` rather than `run_tests.dart`, as
4646
`run_tests.dart` is specifically a CI entrypoint. When iterating on a specific

packages/pigeon/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ to the api to allow for multiple instances to be created and operate in parallel
101101
1) Custom classes used by APIs are defined as classes with fields of the
102102
supported datatypes (see the supported Datatypes section).
103103
1) APIs should be defined as an `abstract class` with either `@HostApi()` or
104-
`@FlutterApi()` as metadata. `@HostApi()` being for procedures that are defined
104+
`@FlutterApi()` as metadata. `@HostApi()` being for procedures that are defined
105105
on the host platform and the `@FlutterApi()` for procedures that are defined in Dart.
106106
1) Method declarations on the API classes should have arguments and a return
107107
value whose types are defined in the file, are supported datatypes, or are
@@ -150,7 +150,7 @@ to the api to allow for multiple instances to be created and operate in parallel
150150
### Calling into Flutter from the host platform
151151

152152
Pigeon also supports calling in the opposite direction. The steps are similar
153-
but reversed. For more information look at the annotation `@FlutterApi()` which
153+
but reversed. For more information look at the annotation `@FlutterApi()` which
154154
denotes APIs that live in Flutter but are invoked from the host platform.
155155
[Example](./example/README.md#FlutterApi_Example).
156156

packages/pigeon/example/app/android/app/src/main/java/io/flutter/plugins/Messages.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,23 @@ public int hashCode() {
311311
return pigeonDeepHashCode(fields);
312312
}
313313

314+
@Override
315+
public String toString() {
316+
return "MessageData{"
317+
+ "name="
318+
+ name
319+
+ ", "
320+
+ "description="
321+
+ description
322+
+ ", "
323+
+ "code="
324+
+ code
325+
+ ", "
326+
+ "data="
327+
+ data
328+
+ "}";
329+
}
330+
314331
public static final class Builder {
315332

316333
private @Nullable String name;

packages/pigeon/example/app/android/app/src/main/kotlin/dev/flutter/pigeon_example_app/EventChannelMessages.g.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ data class IntEvent(val data: Long) : PlatformEvent() {
194194
result = 31 * result + EventChannelMessagesPigeonUtils.deepHash(this.data)
195195
return result
196196
}
197+
198+
override fun toString(): String {
199+
return "IntEvent(data=$data)"
200+
}
197201
}
198202

199203
/** Generated class from Pigeon that represents data sent in messages. */
@@ -227,6 +231,10 @@ data class StringEvent(val data: String) : PlatformEvent() {
227231
result = 31 * result + EventChannelMessagesPigeonUtils.deepHash(this.data)
228232
return result
229233
}
234+
235+
override fun toString(): String {
236+
return "StringEvent(data=$data)"
237+
}
230238
}
231239

232240
private open class EventChannelMessagesPigeonCodec : StandardMessageCodec() {

packages/pigeon/example/app/android/app/src/main/kotlin/dev/flutter/pigeon_example_app/Messages.g.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ data class MessageData(
252252
result = 31 * result + MessagesPigeonUtils.deepHash(this.data)
253253
return result
254254
}
255+
256+
override fun toString(): String {
257+
return "MessageData(name=$name, description=$description, code=$code, data=$data)"
258+
}
255259
}
256260

257261
private open class MessagesPigeonCodec : StandardMessageCodec() {

packages/pigeon/example/app/ios/Runner/EventChannelMessages.g.swift

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,31 @@ import Foundation
1414
#error("Unsupported platform.")
1515
#endif
1616

17-
private func isNullish(_ value: Any?) -> Bool {
18-
return value is NSNull || value == nil
19-
}
17+
#if DEBUG
18+
func isNullish(_ value: Any?) -> Bool {
19+
guard let innerValue = value else {
20+
return true
21+
}
22+
23+
if case Optional<Any>.some(Optional<Any>.none) = value {
24+
return true
25+
}
26+
27+
return innerValue is NSNull
28+
}
29+
#else
30+
private func isNullish(_ value: Any?) -> Bool {
31+
guard let innerValue = value else {
32+
return true
33+
}
34+
35+
if case Optional<Any>.some(Optional<Any>.none) = value {
36+
return true
37+
}
38+
39+
return innerValue is NSNull
40+
}
41+
#endif
2042

2143
private func nilOrValue<T>(_ value: Any?) -> T? {
2244
if value is NSNull { return nil }
@@ -139,7 +161,7 @@ protocol PlatformEvent {
139161
}
140162

141163
/// Generated class from Pigeon that represents data sent in messages.
142-
struct IntEvent: PlatformEvent {
164+
struct IntEvent: PlatformEvent, CustomStringConvertible {
143165
var data: Int64
144166

145167
// swift-format-ignore: AlwaysUseLowerCamelCase
@@ -166,10 +188,14 @@ struct IntEvent: PlatformEvent {
166188
hasher.combine("IntEvent")
167189
deepHashEventChannelMessages(value: data, hasher: &hasher)
168190
}
191+
192+
public var description: String {
193+
return "IntEvent(data: \(data))"
194+
}
169195
}
170196

171197
/// Generated class from Pigeon that represents data sent in messages.
172-
struct StringEvent: PlatformEvent {
198+
struct StringEvent: PlatformEvent, CustomStringConvertible {
173199
var data: String
174200

175201
// swift-format-ignore: AlwaysUseLowerCamelCase
@@ -196,6 +222,10 @@ struct StringEvent: PlatformEvent {
196222
hasher.combine("StringEvent")
197223
deepHashEventChannelMessages(value: data, hasher: &hasher)
198224
}
225+
226+
public var description: String {
227+
return "StringEvent(data: \(data))"
228+
}
199229
}
200230

201231
private class EventChannelMessagesPigeonCodecReader: FlutterStandardReader {

packages/pigeon/example/app/ios/Runner/Messages.g.swift

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,31 @@ private func createConnectionError(withChannelName channelName: String) -> Pigeo
6464
details: "")
6565
}
6666

67-
private func isNullish(_ value: Any?) -> Bool {
68-
return value is NSNull || value == nil
69-
}
67+
#if DEBUG
68+
func isNullish(_ value: Any?) -> Bool {
69+
guard let innerValue = value else {
70+
return true
71+
}
72+
73+
if case Optional<Any>.some(Optional<Any>.none) = value {
74+
return true
75+
}
76+
77+
return innerValue is NSNull
78+
}
79+
#else
80+
private func isNullish(_ value: Any?) -> Bool {
81+
guard let innerValue = value else {
82+
return true
83+
}
84+
85+
if case Optional<Any>.some(Optional<Any>.none) = value {
86+
return true
87+
}
88+
89+
return innerValue is NSNull
90+
}
91+
#endif
7092

7193
private func nilOrValue<T>(_ value: Any?) -> T? {
7294
if value is NSNull { return nil }
@@ -188,7 +210,7 @@ enum Code: Int {
188210
}
189211

190212
/// Generated class from Pigeon that represents data sent in messages.
191-
struct MessageData: Hashable {
213+
struct MessageData: Hashable, CustomStringConvertible {
192214
var name: String? = nil
193215
var description: String? = nil
194216
var code: Code
@@ -232,6 +254,10 @@ struct MessageData: Hashable {
232254
deepHashMessages(value: code, hasher: &hasher)
233255
deepHashMessages(value: data, hasher: &hasher)
234256
}
257+
258+
public var description: String {
259+
return "MessageData(name: \(name), description: \(description), code: \(code), data: \(data))"
260+
}
235261
}
236262

237263
private class MessagesPigeonCodecReader: FlutterStandardReader {
@@ -349,6 +375,7 @@ class ExampleHostApiSetup {
349375
}
350376
}
351377
}
378+
352379
/// Generated protocol from Pigeon that represents Flutter messages that can be called from Swift.
353380
protocol MessageFlutterApiProtocol {
354381
func flutterMethod(

packages/pigeon/example/app/lib/src/event_channel_messages.g.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ class IntEvent extends PlatformEvent {
110110
@override
111111
// ignore: avoid_equals_and_hash_code_on_mutable_classes
112112
int get hashCode => _deepHash(<Object?>[runtimeType, ..._toList()]);
113+
114+
@override
115+
String toString() {
116+
return 'IntEvent(data: $data)';
117+
}
113118
}
114119

115120
class StringEvent extends PlatformEvent {
@@ -145,6 +150,11 @@ class StringEvent extends PlatformEvent {
145150
@override
146151
// ignore: avoid_equals_and_hash_code_on_mutable_classes
147152
int get hashCode => _deepHash(<Object?>[runtimeType, ..._toList()]);
153+
154+
@override
155+
String toString() {
156+
return 'StringEvent(data: $data)';
157+
}
148158
}
149159

150160
class _PigeonCodec extends StandardMessageCodec {

packages/pigeon/example/app/lib/src/messages.g.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ class MessageData {
168168
@override
169169
// ignore: avoid_equals_and_hash_code_on_mutable_classes
170170
int get hashCode => _deepHash(<Object?>[runtimeType, ..._toList()]);
171+
172+
@override
173+
String toString() {
174+
return 'MessageData(name: $name, description: $description, code: $code, data: $data)';
175+
}
171176
}
172177

173178
class _PigeonCodec extends StandardMessageCodec {
@@ -203,8 +208,8 @@ class _PigeonCodec extends StandardMessageCodec {
203208
}
204209

205210
class ExampleHostApi {
206-
/// Constructor for [ExampleHostApi]. The [binaryMessenger] named argument is
207-
/// available for dependency injection. If it is left null, the default
211+
/// Constructor for [ExampleHostApi]. The [binaryMessenger] named argument is
212+
/// available for dependency injection. If it is left null, the default
208213
/// BinaryMessenger will be used which routes to the host platform.
209214
ExampleHostApi({
210215
BinaryMessenger? binaryMessenger,

0 commit comments

Comments
 (0)