Skip to content

Commit cc4b704

Browse files
authored
Upgrade sources: Dart 2.2 format, pedantic + other strict linter rules. (#81)
Upgrade sources: Dart 2.2 format, pedantic + other strict linter rules.
1 parent 54ff7be commit cc4b704

30 files changed

+1887
-1798
lines changed

analysis_options.yaml

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,61 @@
33
# The commented part below is just for inspiration. Read the guide here:
44
# https://github.com/dart-lang/sdk/tree/master/pkg/analyzer#configuring-the-analyzer
55

6+
include: package:pedantic/analysis_options.yaml
7+
68
analyzer:
7-
# strong-mode:
8-
# implicit-casts: false
9-
# excludes:
10-
# - path/to/excluded/files/**
9+
errors:
10+
override_on_non_overriding_method: error
11+
unused_element: error
12+
unused_import: error
13+
unused_local_variable: error
14+
dead_code: error
15+
strong-mode:
16+
implicit-casts: false
1117

1218
linter:
1319
rules:
1420
# see catalogue here: http://dart-lang.github.io/linter/lints/
15-
- camel_case_types
16-
- hash_and_equals
17-
- iterable_contains_unrelated_type
18-
- list_remove_unrelated_type
19-
- unawaited_futures
20-
- unrelated_type_equality_checks
21-
- valid_regexps
21+
- annotate_overrides
22+
- avoid_unused_constructor_parameters
23+
- await_only_futures
24+
- camel_case_types
25+
- cancel_subscriptions
26+
- directives_ordering
27+
# - empty_catches
28+
- empty_statements
29+
- hash_and_equals
30+
- iterable_contains_unrelated_type
31+
- list_remove_unrelated_type
32+
- no_adjacent_strings_in_list
33+
- no_duplicate_case_values
34+
- non_constant_identifier_names
35+
- only_throw_errors
36+
- overridden_fields
37+
- prefer_collection_literals
38+
- prefer_conditional_assignment
39+
- prefer_contains
40+
- prefer_final_fields
41+
- prefer_final_locals
42+
- prefer_initializing_formals
43+
- prefer_interpolation_to_compose_strings
44+
- prefer_is_empty
45+
- prefer_is_not_empty
46+
- prefer_single_quotes
47+
- prefer_typing_uninitialized_variables
48+
- recursive_getters
49+
- slash_for_doc_comments
50+
- test_types_in_equals
51+
- throw_in_finally
52+
- type_init_formals
53+
- unawaited_futures
54+
- unnecessary_brace_in_string_interps
55+
- unnecessary_getters_setters
56+
- unnecessary_lambdas
57+
- unnecessary_new
58+
- unnecessary_null_aware_assignments
59+
- unnecessary_statements
60+
- unnecessary_this
61+
- unrelated_type_equality_checks
62+
- use_rethrow_when_possible
63+
- valid_regexps

lib/postgres.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
library postgres;
22

33
export 'src/connection.dart';
4-
export 'src/types.dart';
5-
export 'src/substituter.dart';
64
export 'src/execution_context.dart';
5+
export 'src/substituter.dart';
6+
export 'src/types.dart';

lib/src/binary_codec.dart

Lines changed: 85 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import 'dart:convert';
2-
32
import 'dart:typed_data';
43

5-
import 'package:postgres/postgres.dart';
6-
import 'package:postgres/src/types.dart';
4+
import 'package:buffer/buffer.dart';
5+
6+
import '../postgres.dart';
7+
import 'types.dart';
78

89
class PostgresBinaryEncoder extends Converter<dynamic, Uint8List> {
910
const PostgresBinaryEncoder(this.dataType);
@@ -19,125 +20,114 @@ class PostgresBinaryEncoder extends Converter<dynamic, Uint8List> {
1920
switch (dataType) {
2021
case PostgreSQLDataType.boolean:
2122
{
22-
if (value is! bool) {
23-
throw new FormatException(
24-
"Invalid type for parameter value. Expected: bool Got: ${value.runtimeType}");
23+
if (value is bool) {
24+
final bd = ByteData(1);
25+
bd.setUint8(0, value ? 1 : 0);
26+
return bd.buffer.asUint8List();
2527
}
26-
27-
var bd = new ByteData(1);
28-
bd.setUint8(0, value ? 1 : 0);
29-
return bd.buffer.asUint8List();
28+
throw FormatException(
29+
'Invalid type for parameter value. Expected: bool Got: ${value.runtimeType}');
3030
}
3131
case PostgreSQLDataType.bigSerial:
3232
case PostgreSQLDataType.bigInteger:
3333
{
34-
if (value is! int) {
35-
throw new FormatException(
36-
"Invalid type for parameter value. Expected: int Got: ${value.runtimeType}");
34+
if (value is int) {
35+
final bd = ByteData(8);
36+
bd.setInt64(0, value);
37+
return bd.buffer.asUint8List();
3738
}
38-
39-
var bd = new ByteData(8);
40-
bd.setInt64(0, value);
41-
return bd.buffer.asUint8List();
39+
throw FormatException(
40+
'Invalid type for parameter value. Expected: int Got: ${value.runtimeType}');
4241
}
4342
case PostgreSQLDataType.serial:
4443
case PostgreSQLDataType.integer:
4544
{
46-
if (value is! int) {
47-
throw new FormatException(
48-
"Invalid type for parameter value. Expected: int Got: ${value.runtimeType}");
45+
if (value is int) {
46+
final bd = ByteData(4);
47+
bd.setInt32(0, value);
48+
return bd.buffer.asUint8List();
4949
}
50-
51-
var bd = new ByteData(4);
52-
bd.setInt32(0, value);
53-
return bd.buffer.asUint8List();
50+
throw FormatException(
51+
'Invalid type for parameter value. Expected: int Got: ${value.runtimeType}');
5452
}
5553
case PostgreSQLDataType.smallInteger:
5654
{
57-
if (value is! int) {
58-
throw new FormatException(
59-
"Invalid type for parameter value. Expected: int Got: ${value.runtimeType}");
55+
if (value is int) {
56+
final bd = ByteData(2);
57+
bd.setInt16(0, value);
58+
return bd.buffer.asUint8List();
6059
}
61-
62-
var bd = new ByteData(2);
63-
bd.setInt16(0, value);
64-
return bd.buffer.asUint8List();
60+
throw FormatException(
61+
'Invalid type for parameter value. Expected: int Got: ${value.runtimeType}');
6562
}
6663
case PostgreSQLDataType.name:
6764
case PostgreSQLDataType.text:
6865
{
69-
if (value is! String) {
70-
throw new FormatException(
71-
"Invalid type for parameter value. Expected: String Got: ${value.runtimeType}");
66+
if (value is String) {
67+
return castBytes(utf8.encode(value));
7268
}
73-
74-
return utf8.encode(value);
69+
throw FormatException(
70+
'Invalid type for parameter value. Expected: String Got: ${value.runtimeType}');
7571
}
7672
case PostgreSQLDataType.real:
7773
{
78-
if (value is! double) {
79-
throw new FormatException(
80-
"Invalid type for parameter value. Expected: double Got: ${value.runtimeType}");
74+
if (value is double) {
75+
final bd = ByteData(4);
76+
bd.setFloat32(0, value);
77+
return bd.buffer.asUint8List();
8178
}
82-
83-
var bd = new ByteData(4);
84-
bd.setFloat32(0, value);
85-
return bd.buffer.asUint8List();
79+
throw FormatException(
80+
'Invalid type for parameter value. Expected: double Got: ${value.runtimeType}');
8681
}
8782
case PostgreSQLDataType.double:
8883
{
89-
if (value is! double) {
90-
throw new FormatException(
91-
"Invalid type for parameter value. Expected: double Got: ${value.runtimeType}");
84+
if (value is double) {
85+
final bd = ByteData(8);
86+
bd.setFloat64(0, value);
87+
return bd.buffer.asUint8List();
9288
}
93-
94-
var bd = new ByteData(8);
95-
bd.setFloat64(0, value);
96-
return bd.buffer.asUint8List();
89+
throw FormatException(
90+
'Invalid type for parameter value. Expected: double Got: ${value.runtimeType}');
9791
}
9892
case PostgreSQLDataType.date:
9993
{
100-
if (value is! DateTime) {
101-
throw new FormatException(
102-
"Invalid type for parameter value. Expected: DateTime Got: ${value.runtimeType}");
94+
if (value is DateTime) {
95+
final bd = ByteData(4);
96+
bd.setInt32(0, value.toUtc().difference(DateTime.utc(2000)).inDays);
97+
return bd.buffer.asUint8List();
10398
}
104-
105-
var bd = new ByteData(4);
106-
bd.setInt32(
107-
0, value.toUtc().difference(new DateTime.utc(2000)).inDays);
108-
return bd.buffer.asUint8List();
99+
throw FormatException(
100+
'Invalid type for parameter value. Expected: DateTime Got: ${value.runtimeType}');
109101
}
110102

111103
case PostgreSQLDataType.timestampWithoutTimezone:
112104
{
113-
if (value is! DateTime) {
114-
throw new FormatException(
115-
"Invalid type for parameter value. Expected: DateTime Got: ${value.runtimeType}");
105+
if (value is DateTime) {
106+
final bd = ByteData(8);
107+
final diff = value.toUtc().difference(DateTime.utc(2000));
108+
bd.setInt64(0, diff.inMicroseconds);
109+
return bd.buffer.asUint8List();
116110
}
117-
118-
var bd = new ByteData(8);
119-
var diff = value.toUtc().difference(new DateTime.utc(2000));
120-
bd.setInt64(0, diff.inMicroseconds);
121-
return bd.buffer.asUint8List();
111+
throw FormatException(
112+
'Invalid type for parameter value. Expected: DateTime Got: ${value.runtimeType}');
122113
}
123114

124115
case PostgreSQLDataType.timestampWithTimezone:
125116
{
126-
if (value is! DateTime) {
127-
throw new FormatException(
128-
"Invalid type for parameter value. Expected: DateTime Got: ${value.runtimeType}");
117+
if (value is DateTime) {
118+
final bd = ByteData(8);
119+
bd.setInt64(
120+
0, value.toUtc().difference(DateTime.utc(2000)).inMicroseconds);
121+
return bd.buffer.asUint8List();
129122
}
130-
131-
var bd = new ByteData(8);
132-
bd.setInt64(0,
133-
value.toUtc().difference(new DateTime.utc(2000)).inMicroseconds);
134-
return bd.buffer.asUint8List();
123+
throw FormatException(
124+
'Invalid type for parameter value. Expected: DateTime Got: ${value.runtimeType}');
135125
}
136126

137127
case PostgreSQLDataType.json:
138128
{
139-
var jsonBytes = utf8.encode(json.encode(value));
140-
final outBuffer = new Uint8List(jsonBytes.length + 1);
129+
final jsonBytes = utf8.encode(json.encode(value));
130+
final outBuffer = Uint8List(jsonBytes.length + 1);
141131
outBuffer[0] = 1;
142132
for (var i = 0; i < jsonBytes.length; i++) {
143133
outBuffer[i + 1] = jsonBytes[i];
@@ -148,28 +138,28 @@ class PostgresBinaryEncoder extends Converter<dynamic, Uint8List> {
148138

149139
case PostgreSQLDataType.byteArray:
150140
{
151-
if (value is! List) {
152-
throw new FormatException(
153-
"Invalid type for parameter value. Expected: List<int> Got: ${value.runtimeType}");
141+
if (value is List<int>) {
142+
return castBytes(value);
154143
}
155-
return new Uint8List.fromList(value);
144+
throw FormatException(
145+
'Invalid type for parameter value. Expected: List<int> Got: ${value.runtimeType}');
156146
}
157147

158148
case PostgreSQLDataType.uuid:
159149
{
160150
if (value is! String) {
161-
throw new FormatException(
162-
"Invalid type for parameter value. Expected: String Got: ${value.runtimeType}");
151+
throw FormatException(
152+
'Invalid type for parameter value. Expected: String Got: ${value.runtimeType}');
163153
}
164154

165-
final dashUnit = "-".codeUnits.first;
155+
final dashUnit = '-'.codeUnits.first;
166156
final hexBytes = (value as String)
167157
.toLowerCase()
168158
.codeUnits
169159
.where((c) => c != dashUnit)
170160
.toList();
171161
if (hexBytes.length != 32) {
172-
throw new FormatException(
162+
throw FormatException(
173163
"Invalid UUID string. There must be exactly 32 hexadecimal (0-9 and a-f) characters and any number of '-' characters.");
174164
}
175165

@@ -180,11 +170,11 @@ class PostgresBinaryEncoder extends Converter<dynamic, Uint8List> {
180170
return charCode - 87;
181171
}
182172

183-
throw new FormatException(
184-
"Invalid UUID string. Contains non-hexadecimal character (0-9 and a-f).");
173+
throw FormatException(
174+
'Invalid UUID string. Contains non-hexadecimal character (0-9 and a-f).');
185175
};
186176

187-
final outBuffer = new Uint8List(16);
177+
final outBuffer = Uint8List(16);
188178
for (var i = 0; i < hexBytes.length; i += 2) {
189179
final upperByte = byteConvert(hexBytes[i]);
190180
final lowerByte = byteConvert(hexBytes[i + 1]);
@@ -195,7 +185,7 @@ class PostgresBinaryEncoder extends Converter<dynamic, Uint8List> {
195185
}
196186
}
197187

198-
throw new PostgreSQLException("Unsupported datatype");
188+
throw PostgreSQLException('Unsupported datatype');
199189
}
200190
}
201191

@@ -212,8 +202,8 @@ class PostgresBinaryDecoder extends Converter<Uint8List, dynamic> {
212202
return null;
213203
}
214204

215-
final buffer = new ByteData.view(
216-
value.buffer, value.offsetInBytes, value.lengthInBytes);
205+
final buffer =
206+
ByteData.view(value.buffer, value.offsetInBytes, value.lengthInBytes);
217207

218208
switch (dataType) {
219209
case PostgreSQLDataType.name:
@@ -236,12 +226,11 @@ class PostgresBinaryDecoder extends Converter<Uint8List, dynamic> {
236226
return buffer.getFloat64(0);
237227
case PostgreSQLDataType.timestampWithoutTimezone:
238228
case PostgreSQLDataType.timestampWithTimezone:
239-
return new DateTime.utc(2000)
240-
.add(new Duration(microseconds: buffer.getInt64(0)));
229+
return DateTime.utc(2000)
230+
.add(Duration(microseconds: buffer.getInt64(0)));
241231

242232
case PostgreSQLDataType.date:
243-
return new DateTime.utc(2000)
244-
.add(new Duration(days: buffer.getInt32(0)));
233+
return DateTime.utc(2000).add(Duration(days: buffer.getInt32(0)));
245234

246235
case PostgreSQLDataType.json:
247236
{
@@ -257,7 +246,7 @@ class PostgresBinaryDecoder extends Converter<Uint8List, dynamic> {
257246

258247
case PostgreSQLDataType.uuid:
259248
{
260-
final codeDash = "-".codeUnitAt(0);
249+
final codeDash = '-'.codeUnitAt(0);
261250

262251
final cipher = [
263252
'0',
@@ -281,7 +270,7 @@ class PostgresBinaryDecoder extends Converter<Uint8List, dynamic> {
281270
return cipher[value];
282271
};
283272

284-
final buf = new StringBuffer();
273+
final buf = StringBuffer();
285274
for (var i = 0; i < buffer.lengthInBytes; i++) {
286275
final byteValue = buffer.getUint8(i);
287276
final upperByteValue = byteValue ~/ 16;

0 commit comments

Comments
 (0)