From 1a7190084bb39fee088618262552ea732f9eecc3 Mon Sep 17 00:00:00 2001 From: Rapougnac Date: Tue, 28 May 2024 15:30:28 +0200 Subject: [PATCH] chore: Refactor Eterl class to use Converter interface for encoding and decoding --- lib/src/eterl.dart | 27 +++++++++++---------------- test/eterl_test.dart | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/lib/src/eterl.dart b/lib/src/eterl.dart index 6241e3a..f9a0863 100644 --- a/lib/src/eterl.dart +++ b/lib/src/eterl.dart @@ -27,11 +27,12 @@ final class Eterl implements Codec> { return encoder.encode(toEncode); } - /// Returns an [EterlDecoder] that can be used to decode data from Erlang. - EterlDecoder unpacker() => _EterlDecoderImpl(); + /// Returns a converter that can be used to decode data from Erlang. + Converter, T> unpacker() => + _EterlDecoderImpl(); - /// Returns an [EterlEncoder] that can be used to encode data to Erlang. - EterlEncoder packer() => _EterlEncoderImpl(); + /// Returns a converter that can be used to encode data to Erlang. + Converter> packer() => _EterlEncoderImpl(); @override Converter> get encoder => _EterlEncoderImpl(); @@ -56,12 +57,7 @@ final class Eterl implements Codec> { } } -abstract class EterlEncoder extends Converter> {} - -abstract class EterlDecoder extends Converter, T> {} - -class _EterlDecoderImpl extends Converter, T> - implements EterlDecoder { +class _EterlDecoderImpl extends Converter, T> { @override T convert(List input) => eterl.unpack(input); @@ -71,13 +67,12 @@ class _EterlDecoderImpl extends Converter, T> } } -class _EterlEncoderImpl extends Converter> - implements EterlEncoder { +class _EterlEncoderImpl extends Converter> { @override - List convert(dynamic input) => eterl.pack(input); + List convert(Object? input) => eterl.pack(input); @override - Sink startChunkedConversion(Sink> sink) { + Sink startChunkedConversion(Sink sink) { return _EterlEncoderConversionSink(sink); } } @@ -98,11 +93,11 @@ Uint8List eterlPack(T toEncode, eterl.pack(toEncode, defaultBufferSize); class _EterlEncoderConversionSink implements ChunkedConversionSink> { - final Sink> _sink; + final Sink _sink; _EterlEncoderConversionSink(this._sink); @override - void add(List chunk) { + void add(Object? chunk) { _sink.add(eterl.pack(chunk)); } diff --git a/test/eterl_test.dart b/test/eterl_test.dart index 83985ae..bce8ae5 100644 --- a/test/eterl_test.dart +++ b/test/eterl_test.dart @@ -697,6 +697,16 @@ void main() { expect(stream.transform(eterl.unpacker()), emitsDone); }); + + test('correctly decodes', () { + final stream = Stream.fromIterable([ + [131, 97, 1], + [131, 97, 2], + [131, 97, 3], + ]); + + expect(stream.transform(eterl.unpacker()), emitsInOrder([1, 2, 3])); + }); }); }); @@ -935,5 +945,28 @@ void main() { test('Empty list', () { expect([131, 106], equals(eterl.pack([]))); }); + + group('as StreamTransformer', () { + test('correctly closes', () { + final stream = Stream.empty(); + + expect(stream.transform(eterl.packer().cast()), emitsDone); + }); + + test('correctly encodes', () { + final stream = Stream.fromIterable([1, 2, 3]); + + expect( + stream.transform(eterl.packer().cast()), + emitsInOrder( + [ + [131, 97, 1], + [131, 97, 2], + [131, 97, 3], + ], + ), + ); + }); + }); }); }