-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TypeCodec[Context] in separate file. (#351)
- Loading branch information
Showing
8 changed files
with
90 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters