Skip to content

Commit aeff758

Browse files
- THRIFT-5712 - Added Dart 3 Compatibility
1 parent a6ee3c0 commit aeff758

36 files changed

+433
-340
lines changed

compiler/cpp/src/thrift/generate/t_dart_generator.cc

Lines changed: 113 additions & 55 deletions
Large diffs are not rendered by default.

lib/dart/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
# under the License.
1818
#
1919

20+
# Dart build command
21+
DARTPUB = dart pub
22+
2023
all-local:
2124
$(DARTPUB) get
2225

lib/dart/lib/src/browser/t_web_socket.dart

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
/// KIND, either express or implied. See the License for the
1515
/// specific language governing permissions and limitations
1616
/// under the License.
17+
///
18+
/// @author: Dipl. Ing. (FH) Martin Haimberger <[email protected]>
1719
1820
library thrift.src.browser;
1921

@@ -49,19 +51,19 @@ class TWebSocket implements TSocket {
4951
: _onStateController = StreamController.broadcast(),
5052
_onErrorController = StreamController.broadcast(),
5153
_onMessageController = StreamController.broadcast() {
52-
if (url == null || !url.hasAuthority || !url.hasPort) {
54+
if (!url.hasAuthority || !url.hasPort) {
5355
throw ArgumentError('Invalid url');
5456
}
5557
}
5658

57-
WebSocket _socket;
59+
WebSocket? _socket;
5860

5961
@override
60-
bool get isOpen => _socket != null && _socket.readyState == WebSocket.OPEN;
62+
bool get isOpen => _socket != null && _socket!.readyState == WebSocket.OPEN;
6163

6264
@override
6365
bool get isClosed =>
64-
_socket == null || _socket.readyState == WebSocket.CLOSED;
66+
_socket == null ||_socket!.readyState == WebSocket.CLOSED;
6567

6668
@override
6769
Future open() {
@@ -71,19 +73,19 @@ class TWebSocket implements TSocket {
7173
}
7274

7375
_socket = WebSocket(url.toString());
74-
_socket.onError.listen(_onError);
75-
_socket.onOpen.listen(_onOpen);
76-
_socket.onClose.listen(_onClose);
77-
_socket.onMessage.listen(_onMessage);
76+
_socket!.onError.listen(_onError);
77+
_socket!.onOpen.listen(_onOpen);
78+
_socket!.onClose.listen(_onClose);
79+
_socket!.onMessage.listen(_onMessage);
7880

79-
return _socket.onOpen.first;
81+
return _socket!.onOpen.first;
8082
}
8183

8284
@override
8385
Future close() {
8486
if (_socket != null) {
85-
_socket.close();
86-
return _socket.onClose.first;
87+
_socket!.close();
88+
return _socket!.onClose.first;
8789
} else {
8890
return Future.value();
8991
}
@@ -98,7 +100,7 @@ class TWebSocket implements TSocket {
98100
void _sendRequests() {
99101
while (isOpen && _requests.isNotEmpty) {
100102
Uint8List data = _requests.removeAt(0);
101-
_socket.sendString(base64.encode(data));
103+
_socket!.sendString(base64.encode(data));
102104
}
103105
}
104106

lib/dart/lib/src/console/t_tcp_socket.dart

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
/// KIND, either express or implied. See the License for the
1515
/// specific language governing permissions and limitations
1616
/// under the License.
17+
///
18+
/// @author: Dipl. Ing. (FH) Martin Haimberger <[email protected]>
1719
1820
library thrift.src.console.t_tcp_socket;
1921

@@ -37,7 +39,7 @@ class TTcpSocket implements TSocket {
3739
@override
3840
Stream<Uint8List> get onMessage => _onMessageController.stream;
3941

40-
TTcpSocket(Socket socket)
42+
TTcpSocket(Socket? socket)
4143
: _onStateController = StreamController.broadcast(),
4244
_onErrorController = StreamController.broadcast(),
4345
_onMessageController = StreamController.broadcast() {
@@ -46,10 +48,10 @@ class TTcpSocket implements TSocket {
4648
}
4749

4850
_socket = socket;
49-
_socket.listen(_onMessage, onError: _onError, onDone: close);
51+
_socket!.listen(_onMessage, onError: _onError, onDone: close);
5052
}
5153

52-
Socket _socket;
54+
Socket? _socket;
5355

5456
@override
5557
bool get isOpen => _socket != null;
@@ -65,7 +67,7 @@ class TTcpSocket implements TSocket {
6567
@override
6668
Future close() async {
6769
if (_socket != null) {
68-
await _socket.close();
70+
await _socket!.close();
6971
_socket = null;
7072
}
7173

@@ -74,7 +76,9 @@ class TTcpSocket implements TSocket {
7476

7577
@override
7678
void send(Uint8List data) {
77-
_socket.add(data);
79+
if (_socket != null) {
80+
_socket!.add(data);
81+
}
7882
}
7983

8084
void _onMessage(List<int> message) {

lib/dart/lib/src/console/t_web_socket.dart

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
/// KIND, either express or implied. See the License for the
1515
/// specific language governing permissions and limitations
1616
/// under the License.
17+
///
18+
/// @author: Dipl. Ing. (FH) Martin Haimberger <[email protected]>
1719
1820
library thrift.src.console.t_web_socket;
1921

@@ -38,7 +40,7 @@ class TWebSocket implements TSocket {
3840
@override
3941
Stream<Uint8List> get onMessage => _onMessageController.stream;
4042

41-
TWebSocket(WebSocket socket)
43+
TWebSocket(WebSocket? socket)
4244
: _onStateController = StreamController.broadcast(),
4345
_onErrorController = StreamController.broadcast(),
4446
_onMessageController = StreamController.broadcast() {
@@ -47,10 +49,12 @@ class TWebSocket implements TSocket {
4749
}
4850

4951
_socket = socket;
50-
_socket.listen(_onMessage, onError: _onError, onDone: close);
52+
_socket!.listen((dynamic message) => _onMessage(message as String),
53+
onError: (dynamic error) => _onError(error),
54+
onDone: () => close(),);
5155
}
5256

53-
WebSocket _socket;
57+
WebSocket? _socket;
5458

5559
@override
5660
bool get isOpen => _socket != null;
@@ -66,7 +70,7 @@ class TWebSocket implements TSocket {
6670
@override
6771
Future close() async {
6872
if (_socket != null) {
69-
await _socket.close();
73+
await _socket!.close();
7074
_socket = null;
7175
}
7276

@@ -75,7 +79,9 @@ class TWebSocket implements TSocket {
7579

7680
@override
7781
void send(Uint8List data) {
78-
_socket.add(base64.encode(data));
82+
if (_socket != null) {
83+
_socket!.add(base64.encode(data));
84+
}
7985
}
8086

8187
void _onMessage(String message) {

lib/dart/lib/src/protocol/t_binary_protocol.dart

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
/// KIND, either express or implied. See the License for the
1515
/// specific language governing permissions and limitations
1616
/// under the License.
17+
///
18+
/// @author: Dipl. Ing. (FH) Martin Haimberger <[email protected]>
1719
1820
part of thrift;
1921

@@ -114,15 +116,13 @@ class TBinaryProtocol extends TProtocol {
114116

115117
@override
116118
void writeBool(bool b) {
117-
if (b == null) b = false;
118119
writeByte(b ? 1 : 0);
119120
}
120121

121122
final ByteData _byteOut = ByteData(1);
122123

123124
@override
124125
void writeByte(int byte) {
125-
if (byte == null) byte = 0;
126126
_byteOut.setUint8(0, byte);
127127
transport.write(_byteOut.buffer.asUint8List(), 0, 1);
128128
}
@@ -131,7 +131,6 @@ class TBinaryProtocol extends TProtocol {
131131

132132
@override
133133
void writeI16(int i16) {
134-
if (i16 == null) i16 = 0;
135134
_i16Out.setInt16(0, i16);
136135
transport.write(_i16Out.buffer.asUint8List(), 0, 2);
137136
}
@@ -140,7 +139,6 @@ class TBinaryProtocol extends TProtocol {
140139

141140
@override
142141
void writeI32(int i32) {
143-
if (i32 == null) i32 = 0;
144142
_i32Out.setInt32(0, i32);
145143
transport.write(_i32Out.buffer.asUint8List(), 0, 4);
146144
}
@@ -149,7 +147,6 @@ class TBinaryProtocol extends TProtocol {
149147

150148
@override
151149
void writeI64(int i64) {
152-
if (i64 == null) i64 = 0;
153150
var i = Int64(i64);
154151
var bts = i.toBytes();
155152
for (var j = 0; j < 8; j++) {
@@ -160,7 +157,7 @@ class TBinaryProtocol extends TProtocol {
160157

161158
@override
162159
void writeString(String s) {
163-
var bytes = s != null ? _utf8Codec.encode(s) : Uint8List.fromList([]);
160+
var bytes = _utf8Codec.encode(s);
164161
writeI32(bytes.length);
165162
transport.write(bytes, 0, bytes.length);
166163
}
@@ -169,7 +166,6 @@ class TBinaryProtocol extends TProtocol {
169166

170167
@override
171168
void writeDouble(double d) {
172-
if (d == null) d = 0.0;
173169
_doubleOut.setFloat64(0, d);
174170
transport.write(_doubleOut.buffer.asUint8List(), 0, 8);
175171
}

lib/dart/lib/src/protocol/t_compact_protocol.dart

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
/// KIND, either express or implied. See the License for the
1515
/// specific language governing permissions and limitations
1616
/// under the License.
17+
///
18+
/// @author: Dipl. Ing. (FH) Martin Haimberger <[email protected]>
1719
1820
part of thrift;
1921

@@ -54,7 +56,8 @@ class TCompactProtocol extends TProtocol {
5456
static const int TYPE_MAP = 0x0B;
5557
static const int TYPE_STRUCT = 0x0C;
5658

57-
static final List<int> _typeMap = List.unmodifiable(List(16)
59+
static final List<int> _typeMap = List<int>.unmodifiable(
60+
List<int>.filled(16, 0)
5861
..[TType.STOP] = TType.STOP
5962
..[TType.BOOL] = TYPE_BOOLEAN_TRUE
6063
..[TType.BYTE] = TYPE_BYTE
@@ -66,16 +69,17 @@ class TCompactProtocol extends TProtocol {
6669
..[TType.LIST] = TYPE_LIST
6770
..[TType.SET] = TYPE_SET
6871
..[TType.MAP] = TYPE_MAP
69-
..[TType.STRUCT] = TYPE_STRUCT);
72+
..[TType.STRUCT] = TYPE_STRUCT,
73+
);
7074

7175
static const Utf8Codec _utf8Codec = Utf8Codec();
7276

7377
// Pretend this is a stack
7478
DoubleLinkedQueue<int> _lastField = DoubleLinkedQueue<int>();
7579
int _lastFieldId = 0;
7680

77-
TField _booleanField;
78-
bool _boolValue;
81+
TField? _booleanField;
82+
bool? _boolValue;
7983

8084
final Uint8List tempList = Uint8List(10);
8185
final ByteData tempBD = ByteData(10);
@@ -169,10 +173,9 @@ class TCompactProtocol extends TProtocol {
169173

170174
@override
171175
void writeBool(bool b) {
172-
if (b == null) b = false;
173176
if (_booleanField != null) {
174177
_writeFieldBegin(
175-
_booleanField, b ? TYPE_BOOLEAN_TRUE : TYPE_BOOLEAN_FALSE);
178+
_booleanField!, b ? TYPE_BOOLEAN_TRUE : TYPE_BOOLEAN_FALSE);
176179
_booleanField = null;
177180
} else {
178181
writeByte(b ? TYPE_BOOLEAN_TRUE : TYPE_BOOLEAN_FALSE);
@@ -181,40 +184,34 @@ class TCompactProtocol extends TProtocol {
181184

182185
@override
183186
void writeByte(int b) {
184-
if (b == null) b = 0;
185187
tempList[0] = b;
186188
transport.write(tempList, 0, 1);
187189
}
188190

189191
@override
190192
void writeI16(int i16) {
191-
if (i16 == null) i16 = 0;
192193
_writeVarInt32(_int32ToZigZag(Int32(i16)));
193194
}
194195

195196
@override
196197
void writeI32(int i32) {
197-
if (i32 == null) i32 = 0;
198198
_writeVarInt32(_int32ToZigZag(Int32(i32)));
199199
}
200200

201201
@override
202202
void writeI64(int i64) {
203-
if (i64 == null) i64 = 0;
204203
_writeVarInt64(_int64ToZigZag(Int64(i64)));
205204
}
206205

207206
@override
208207
void writeDouble(double d) {
209-
if (d == null) d = 0.0;
210208
tempBD.setFloat64(0, d, Endian.little);
211209
transport.write(tempBD.buffer.asUint8List(), 0, 8);
212210
}
213211

214212
@override
215213
void writeString(String str) {
216-
Uint8List bytes =
217-
str != null ? _utf8Codec.encode(str) : Uint8List.fromList([]);
214+
Uint8List bytes = _utf8Codec.encode(str);
218215
writeBinary(bytes);
219216
}
220217

@@ -374,7 +371,7 @@ class TCompactProtocol extends TProtocol {
374371
@override
375372
bool readBool() {
376373
if (_boolValue != null) {
377-
bool result = _boolValue;
374+
bool result = _boolValue!;
378375
_boolValue = null;
379376
return result;
380377
}

0 commit comments

Comments
 (0)