Skip to content

Commit

Permalink
chore: Refactor Eterl class to use Converter interface for encoding a…
Browse files Browse the repository at this point in the history
…nd decoding
  • Loading branch information
Lexedia committed May 28, 2024
1 parent bfda03d commit 1a71900
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
27 changes: 11 additions & 16 deletions lib/src/eterl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ final class Eterl implements Codec<Object?, List<int>> {
return encoder.encode(toEncode);
}

/// Returns an [EterlDecoder] that can be used to decode data from Erlang.
EterlDecoder<T> unpacker<T extends Object?>() => _EterlDecoderImpl<T>();
/// Returns a converter that can be used to decode data from Erlang.
Converter<List<int>, T> unpacker<T extends Object?>() =>
_EterlDecoderImpl<T>();

/// 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<Object?, List<int>> packer() => _EterlEncoderImpl();

@override
Converter<Object?, List<int>> get encoder => _EterlEncoderImpl();
Expand All @@ -56,12 +57,7 @@ final class Eterl implements Codec<Object?, List<int>> {
}
}

abstract class EterlEncoder extends Converter<Object?, List<int>> {}

abstract class EterlDecoder<T> extends Converter<List<int>, T> {}

class _EterlDecoderImpl<T> extends Converter<List<int>, T>
implements EterlDecoder<T> {
class _EterlDecoderImpl<T> extends Converter<List<int>, T> {
@override
T convert(List<int> input) => eterl.unpack(input);

Expand All @@ -71,13 +67,12 @@ class _EterlDecoderImpl<T> extends Converter<List<int>, T>
}
}

class _EterlEncoderImpl extends Converter<dynamic, List<int>>
implements EterlEncoder {
class _EterlEncoderImpl extends Converter<Object?, List<int>> {
@override
List<int> convert(dynamic input) => eterl.pack(input);
List<int> convert(Object? input) => eterl.pack(input);

@override
Sink<dynamic> startChunkedConversion(Sink<List<int>> sink) {
Sink<Object?> startChunkedConversion(Sink<Object?> sink) {
return _EterlEncoderConversionSink(sink);
}
}
Expand All @@ -98,11 +93,11 @@ Uint8List eterlPack<T extends Object?>(T toEncode,
eterl.pack(toEncode, defaultBufferSize);

class _EterlEncoderConversionSink implements ChunkedConversionSink<List<int>> {
final Sink<List<int>> _sink;
final Sink<Object?> _sink;
_EterlEncoderConversionSink(this._sink);

@override
void add(List<int> chunk) {
void add(Object? chunk) {
_sink.add(eterl.pack(chunk));
}

Expand Down
33 changes: 33 additions & 0 deletions test/eterl_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
});
});
});

Expand Down Expand Up @@ -935,5 +945,28 @@ void main() {
test('Empty list', () {
expect([131, 106], equals(eterl.pack([])));
});

group('as StreamTransformer', () {
test('correctly closes', () {
final stream = Stream<Object>.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],
],
),
);
});
});
});
}

0 comments on commit 1a71900

Please sign in to comment.