Skip to content

Commit

Permalink
TypeCodec[Context] in separate file. (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos authored Aug 26, 2024
1 parent 8423a01 commit 4c5dba4
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 90 deletions.
2 changes: 1 addition & 1 deletion lib/src/messages/client_messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/types/binary_codec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
39 changes: 1 addition & 38 deletions lib/src/types/generic_type.dart
Original file line number Diff line number Diff line change
@@ -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<Object> {
const UnknownType(super.oid);
Expand Down
3 changes: 1 addition & 2 deletions lib/src/types/text_codec.dart
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions lib/src/types/text_search.dart
Original file line number Diff line number Diff line change
@@ -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
Expand Down
80 changes: 80 additions & 0 deletions lib/src/types/type_codec.dart
Original file line number Diff line number Diff line change
@@ -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<EncodedValue?> 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<EncodedValue?> 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<Object?> 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,
});
}
45 changes: 2 additions & 43 deletions lib/src/types/type_registry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -240,48 +241,6 @@ final _builtInTypeNames = <String, Type>{
'_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<EncodedValue?> 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<Object?> 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<EncodedValue?> Function(
TypeCodecContext context, Object? input);

class TypeRegistry {
final _byTypeOid = <int, Type>{};
final _bySubstitutionName = <String, Type>{};
Expand Down
4 changes: 2 additions & 2 deletions lib/src/v3/connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 4c5dba4

Please sign in to comment.