Skip to content

Commit 7439043

Browse files
authored
Merge pull request #1 from isoos/migrate
Migration to null-safety.
2 parents 396c0e3 + 5b0109f commit 7439043

28 files changed

+339
-323
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 2.3.0-null-safety.0
4+
5+
- Migrate to null safety. (Thanks to [j4qfrost](https://github.com/j4qfrost), [#153](https://github.com/stablekernel/postgresql-dart/pull/153)).
6+
37
## 2.2.0
48

59
- Supporting Unix socket connections. (Thanks to [grillbiff](https://github.com/grillbiff),

lib/src/binary_codec.dart

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ final _hex = <String>[
2828
'f',
2929
];
3030

31-
class PostgresBinaryEncoder extends Converter<dynamic, Uint8List> {
31+
class PostgresBinaryEncoder extends Converter<dynamic, Uint8List?> {
3232
final PostgreSQLDataType _dataType;
3333

3434
const PostgresBinaryEncoder(this._dataType);
3535

3636
@override
37-
Uint8List convert(dynamic value) {
37+
Uint8List? convert(dynamic value) {
3838
if (value == null) {
3939
return null;
4040
}
@@ -169,7 +169,7 @@ class PostgresBinaryEncoder extends Converter<dynamic, Uint8List> {
169169
'Invalid type for parameter value. Expected: String Got: ${value.runtimeType}');
170170
}
171171

172-
final hexBytes = (value as String)
172+
final hexBytes = value
173173
.toLowerCase()
174174
.codeUnits
175175
.where((c) => c != _dashUnit)
@@ -199,9 +199,9 @@ class PostgresBinaryEncoder extends Converter<dynamic, Uint8List> {
199199
}
200200
return outBuffer;
201201
}
202+
default:
203+
throw PostgreSQLException('Unsupported datatype');
202204
}
203-
204-
throw PostgreSQLException('Unsupported datatype');
205205
}
206206
}
207207

@@ -211,13 +211,13 @@ class PostgresBinaryDecoder extends Converter<Uint8List, dynamic> {
211211
final int typeCode;
212212

213213
@override
214-
dynamic convert(Uint8List value) {
215-
final dataType = typeMap[typeCode];
216-
214+
dynamic convert(Uint8List? value) {
217215
if (value == null) {
218216
return null;
219217
}
220218

219+
final dataType = typeMap[typeCode];
220+
221221
final buffer =
222222
ByteData.view(value.buffer, value.offsetInBytes, value.lengthInBytes);
223223

@@ -277,16 +277,18 @@ class PostgresBinaryDecoder extends Converter<Uint8List, dynamic> {
277277

278278
return buf.toString();
279279
}
280-
}
281-
282-
// We'll try and decode this as a utf8 string and return that
283-
// for many internal types, this is valid. If it fails,
284-
// we just return the bytes and let the caller figure out what to
285-
// do with it.
286-
try {
287-
return utf8.decode(value);
288-
} catch (_) {
289-
return value;
280+
default:
281+
{
282+
// We'll try and decode this as a utf8 string and return that
283+
// for many internal types, this is valid. If it fails,
284+
// we just return the bytes and let the caller figure out what to
285+
// do with it.
286+
try {
287+
return utf8.decode(value);
288+
} catch (_) {
289+
return value;
290+
}
291+
}
290292
}
291293
}
292294

lib/src/client_messages.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ void _applyStringToBuffer(UTF8BackedString string, ByteDataWriter buffer) {
4242
}
4343

4444
class StartupMessage extends ClientMessage {
45-
final UTF8BackedString _username;
45+
final UTF8BackedString? _username;
4646
final UTF8BackedString _databaseName;
4747
final UTF8BackedString _timeZone;
4848

49-
StartupMessage(String databaseName, String timeZone, {String username})
49+
StartupMessage(String databaseName, String timeZone, {String? username})
5050
: _databaseName = UTF8BackedString(databaseName),
5151
_timeZone = UTF8BackedString(timeZone),
5252
_username = username == null ? null : UTF8BackedString(username);
@@ -58,15 +58,15 @@ class StartupMessage extends ClientMessage {
5858

5959
if (_username != null) {
6060
fixedLength += 5;
61-
variableLength += _username.utf8Length + 1;
61+
variableLength += _username!.utf8Length + 1;
6262
}
6363

6464
buffer.writeInt32(fixedLength + variableLength);
6565
buffer.writeInt32(ClientMessage.ProtocolVersion);
6666

6767
if (_username != null) {
6868
buffer.write(UTF8ByteConstants.user);
69-
_applyStringToBuffer(_username, buffer);
69+
_applyStringToBuffer(_username!, buffer);
7070
}
7171

7272
buffer.write(UTF8ByteConstants.database);
@@ -83,7 +83,7 @@ class StartupMessage extends ClientMessage {
8383
}
8484

8585
class AuthMD5Message extends ClientMessage {
86-
UTF8BackedString _hashedAuthString;
86+
UTF8BackedString? _hashedAuthString;
8787

8888
AuthMD5Message(String username, String password, List<int> saltBytes) {
8989
final passwordHash = md5.convert('$password$username'.codeUnits).toString();
@@ -96,9 +96,9 @@ class AuthMD5Message extends ClientMessage {
9696
@override
9797
void applyToBuffer(ByteDataWriter buffer) {
9898
buffer.writeUint8(ClientMessage.PasswordIdentifier);
99-
final length = 5 + _hashedAuthString.utf8Length;
99+
final length = 5 + _hashedAuthString!.utf8Length;
100100
buffer.writeUint32(length);
101-
_applyStringToBuffer(_hashedAuthString, buffer);
101+
_applyStringToBuffer(_hashedAuthString!, buffer);
102102
}
103103
}
104104

@@ -157,14 +157,14 @@ class BindMessage extends ClientMessage {
157157
final List<ParameterValue> _parameters;
158158
final UTF8BackedString _statementName;
159159
final int _typeSpecCount;
160-
int _cachedLength;
160+
int _cachedLength = -1;
161161

162162
BindMessage(this._parameters, {String statementName = ''})
163163
: _typeSpecCount = _parameters.where((p) => p.isBinary).length,
164164
_statementName = UTF8BackedString(statementName);
165165

166166
int get length {
167-
if (_cachedLength == null) {
167+
if (_cachedLength == -1) {
168168
var inputParameterElementCount = _parameters.length;
169169
if (_typeSpecCount == _parameters.length || _typeSpecCount == 0) {
170170
inputParameterElementCount = 1;
@@ -221,7 +221,7 @@ class BindMessage extends ClientMessage {
221221
buffer.writeInt32(-1);
222222
} else {
223223
buffer.writeInt32(p.length);
224-
buffer.write(p.bytes);
224+
buffer.write(p.bytes!);
225225
}
226226
});
227227

0 commit comments

Comments
 (0)