From 4c5dba43e6c84d35cca47a84675badd2f87ee1c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Istv=C3=A1n=20So=C3=B3s?= Date: Tue, 27 Aug 2024 00:40:36 +0200 Subject: [PATCH] TypeCodec[Context] in separate file. (#351) --- lib/src/messages/client_messages.dart | 2 +- lib/src/types/binary_codec.dart | 2 +- lib/src/types/generic_type.dart | 39 +------------ lib/src/types/text_codec.dart | 3 +- lib/src/types/text_search.dart | 5 +- lib/src/types/type_codec.dart | 80 +++++++++++++++++++++++++++ lib/src/types/type_registry.dart | 45 +-------------- lib/src/v3/connection.dart | 4 +- 8 files changed, 90 insertions(+), 90 deletions(-) create mode 100644 lib/src/types/type_codec.dart diff --git a/lib/src/messages/client_messages.dart b/lib/src/messages/client_messages.dart index 247f06a..27843a1 100644 --- a/lib/src/messages/client_messages.dart +++ b/lib/src/messages/client_messages.dart @@ -2,12 +2,12 @@ import 'dart:convert'; import 'dart:typed_data'; import 'package:charcode/ascii.dart'; -import 'package:postgres/src/types/generic_type.dart'; import '../buffer.dart'; import '../replication.dart'; import '../time_converters.dart'; import '../types.dart'; +import '../types/type_codec.dart'; import 'shared_messages.dart'; abstract class ClientMessageFormat { diff --git a/lib/src/types/binary_codec.dart b/lib/src/types/binary_codec.dart index 53d479a..4fea975 100644 --- a/lib/src/types/binary_codec.dart +++ b/lib/src/types/binary_codec.dart @@ -2,12 +2,12 @@ import 'dart:convert'; import 'dart:typed_data'; import 'package:buffer/buffer.dart'; -import 'package:postgres/src/types/generic_type.dart'; import '../buffer.dart'; import '../types.dart'; import 'geo_types.dart'; import 'range_types.dart'; +import 'type_codec.dart'; import 'type_registry.dart'; final _bool0 = Uint8List(1)..[0] = 0; diff --git a/lib/src/types/generic_type.dart b/lib/src/types/generic_type.dart index d26e27e..ab3f618 100644 --- a/lib/src/types/generic_type.dart +++ b/lib/src/types/generic_type.dart @@ -1,44 +1,7 @@ -import 'dart:convert'; -import 'dart:typed_data'; - -import 'package:postgres/src/buffer.dart'; - import '../types.dart'; import 'binary_codec.dart'; import 'text_codec.dart'; -import 'type_registry.dart'; - -class EncodedValue { - final Uint8List? bytes; - final bool isBinary; - - EncodedValue({ - required this.bytes, - required this.isBinary, - }); -} - -class TypeCodecContext { - final Encoding encoding; - final TypeRegistry typeRegistry; - - TypeCodecContext({ - required this.encoding, - required this.typeRegistry, - }); - - PgByteDataReader newPgByteDataReader([Uint8List? bytes]) { - final reader = PgByteDataReader(encoding: encoding); - if (bytes != null) { - reader.add(bytes); - } - return reader; - } - - PgByteDataWriter newPgByteDataWriter() { - return PgByteDataWriter(encoding: encoding); - } -} +import 'type_codec.dart'; class UnknownType extends Type { const UnknownType(super.oid); diff --git a/lib/src/types/text_codec.dart b/lib/src/types/text_codec.dart index 778be11..e96c7ef 100644 --- a/lib/src/types/text_codec.dart +++ b/lib/src/types/text_codec.dart @@ -1,11 +1,10 @@ import 'dart:convert'; import 'dart:typed_data'; -import 'package:postgres/src/types/generic_type.dart'; - import '../exceptions.dart'; import '../types.dart'; import 'geo_types.dart'; +import 'type_codec.dart'; import 'type_registry.dart'; class PostgresTextEncoder { diff --git a/lib/src/types/text_search.dart b/lib/src/types/text_search.dart index bc6dd17..bfe8557 100644 --- a/lib/src/types/text_search.dart +++ b/lib/src/types/text_search.dart @@ -1,8 +1,7 @@ -import 'package:postgres/src/exceptions.dart'; - import '../buffer.dart'; +import '../exceptions.dart'; import '../types.dart'; -import 'generic_type.dart'; +import 'type_codec.dart'; import 'type_registry.dart'; /// The `tsvector` data type stores a list of [TsWord]s, lexemes with diff --git a/lib/src/types/type_codec.dart b/lib/src/types/type_codec.dart new file mode 100644 index 0000000..95d425c --- /dev/null +++ b/lib/src/types/type_codec.dart @@ -0,0 +1,80 @@ +import 'dart:async'; +import 'dart:convert'; +import 'dart:typed_data'; + +import '../buffer.dart'; +import 'type_registry.dart'; + +/// Encodes the [input] value and returns an [EncodedValue] object. +/// +/// May return `null` if the codec is not able to encode the [input]. +typedef TypeEncoderFn = FutureOr Function( + TypeCodecContext context, Object? input); + +/// Encoder and decoder for a given type (OID). +abstract class TypeCodec { + /// Whether the `null` value is handled as a special case by this codec. + /// + /// By default Dart `null` values are encoded as SQL `NULL` values, and + /// [TypeCodec] will not recieve the `null` value on its [encode] method. + /// + /// When the flag is set (`true`) the [TypeCodec.encode] will recieve `null` + /// as `input` value. + final bool encodesNull; + + /// Whether the SQL `NULL` value is handled as a special case by this codec. + /// + /// By default SQL `NULL` values are decoded as Dart `null` values, and + /// [TypeCodec] will not recieve the `null` value on its [decode] method. + /// + /// When the flag is set (`true`) the [TypeCodec.decode] will recieve `null` + /// as `input` value ([EncodedValue.bytes] will be `null`). + final bool decodesNull; + + TypeCodec({ + this.encodesNull = false, + this.decodesNull = false, + }); + + /// Encodes the [input] value and returns an [EncodedValue] object. + /// + /// May return `null` if the codec is not able to encode the [input]. + FutureOr encode(TypeCodecContext context, Object? input); + + /// Decodes the [input] value and returns a Dart value object. + /// + /// May return [UndecodedBytes] if the codec is not able to decode the [input]. + FutureOr decode(TypeCodecContext context, EncodedValue input); +} + +class TypeCodecContext { + final Encoding encoding; + final TypeRegistry typeRegistry; + + TypeCodecContext({ + required this.encoding, + required this.typeRegistry, + }); + + PgByteDataReader newPgByteDataReader([Uint8List? bytes]) { + final reader = PgByteDataReader(encoding: encoding); + if (bytes != null) { + reader.add(bytes); + } + return reader; + } + + PgByteDataWriter newPgByteDataWriter() { + return PgByteDataWriter(encoding: encoding); + } +} + +class EncodedValue { + final Uint8List? bytes; + final bool isBinary; + + EncodedValue({ + required this.bytes, + required this.isBinary, + }); +} diff --git a/lib/src/types/type_registry.dart b/lib/src/types/type_registry.dart index 29a9f31..f553ee1 100644 --- a/lib/src/types/type_registry.dart +++ b/lib/src/types/type_registry.dart @@ -2,12 +2,13 @@ import 'dart:async'; import 'dart:typed_data'; import 'package:buffer/buffer.dart'; -import 'package:postgres/src/types/text_search.dart'; import '../exceptions.dart'; import '../types.dart'; import 'generic_type.dart'; import 'text_codec.dart'; +import 'text_search.dart'; +import 'type_codec.dart'; /// See: https://github.com/postgres/postgres/blob/master/src/include/catalog/pg_type.dat class TypeOid { @@ -240,48 +241,6 @@ final _builtInTypeNames = { '_varchar': Type.varCharArray, }; -/// Encoder and decoder for a given type (OID). -abstract class TypeCodec { - /// Whether the `null` value is handled as a special case by this codec. - /// - /// By default Dart `null` values are encoded as SQL `NULL` values, and - /// [TypeCodec] will not recieve the `null` value on its [encode] method. - /// - /// When the flag is set (`true`) the [TypeCodec.encode] will recieve `null` - /// as `input` value. - final bool encodesNull; - - /// Whether the SQL `NULL` value is handled as a special case by this codec. - /// - /// By default SQL `NULL` values are decoded as Dart `null` values, and - /// [TypeCodec] will not recieve the `null` value on its [decode] method. - /// - /// When the flag is set (`true`) the [TypeCodec.decode] will recieve `null` - /// as `input` value ([EncodedValue.bytes] will be `null`). - final bool decodesNull; - - TypeCodec({ - this.encodesNull = false, - this.decodesNull = false, - }); - - /// Encodes the [input] value and returns an [EncodedValue] object. - /// - /// May return `null` if the codec is not able to encode the [input]. - FutureOr encode(TypeCodecContext context, Object? input); - - /// Decodes the [input] value and returns a Dart value object. - /// - /// May return [UndecodedBytes] if the codec is not able to decode the [input]. - FutureOr decode(TypeCodecContext context, EncodedValue input); -} - -/// Encodes the [input] value and returns an [EncodedValue] object. -/// -/// May return `null` if the codec is not able to encode the [input]. -typedef TypeEncoderFn = FutureOr Function( - TypeCodecContext context, Object? input); - class TypeRegistry { final _byTypeOid = {}; final _bySubstitutionName = {}; diff --git a/lib/src/v3/connection.dart b/lib/src/v3/connection.dart index 4ad7e3b..9e5de0d 100644 --- a/lib/src/v3/connection.dart +++ b/lib/src/v3/connection.dart @@ -7,12 +7,12 @@ import 'package:async/async.dart' as async; import 'package:charcode/ascii.dart'; import 'package:meta/meta.dart'; import 'package:pool/pool.dart' as pool; -import 'package:postgres/src/types/generic_type.dart'; -import 'package:postgres/src/types/type_registry.dart'; import 'package:stream_channel/stream_channel.dart'; import '../../postgres.dart'; import '../auth/auth.dart'; +import '../types/type_codec.dart'; +import '../types/type_registry.dart'; import 'protocol.dart'; import 'query_description.dart'; import 'resolved_settings.dart';