-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Justkawal/complete new approach #186
Conversation
Co-authored-by: Gabriel Okura <gabriellevicz@gmail.com>
output.pushByte(value.toInt() << 2); | ||
} else if (value < 16384) { | ||
output | ||
..pushByte((value.toInt() & 0x3F) << 2 | 1) | ||
..pushByte((value.toInt() >> 6).toUnsigned(8)); | ||
} else if (value < 1073741824) { | ||
output | ||
..pushByte((value.toInt() & 0x3F) << 2 | 2) | ||
..pushByte((value.toInt() >> 6).toUnsigned(8)) | ||
..pushByte((value.toInt() >> 14).toUnsigned(8)) | ||
..pushByte((value.toInt() >> 22).toUnsigned(8)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The value is already of type int here
output.pushByte(value << 2);
} else if (value < 16384) {
output
..pushByte((value & 0x3F) << 2 | 1)
..pushByte((value >> 6).toUnsigned(8));
} else if (value < 1073741824) {
output
..pushByte((value & 0x3F) << 2 | 2)
..pushByte((value >> 6).toUnsigned(8))
..pushByte((value >> 14).toUnsigned(8))
..pushByte((value >> 22).toUnsigned(8));
final bytesNeeded = (value.bitLength + 7) >> 3; | ||
output.pushByte(((bytesNeeded - 4) << 2).toUnsigned(8) | 3); | ||
for (var i = 0; i < bytesNeeded; i++) { | ||
output.pushByte(value.toUnsigned(8).toInt()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
output.pushByte(value.toUnsigned(8).toInt()); | |
output.pushByte(value.toUnsigned(8)); |
part of primitives; | ||
|
||
class SimpleEnumCodec<A> with Codec<A?> { | ||
final List<A?> list; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be an Map, because even simple enums can have sparse indexes, ex:
enum SomeEnum {
#[index(0)]
Value1,
#[index(10)]
Value2,
#[index(30)]
Value3,
}
} | ||
|
||
class ComplexEnumCodec<V> with Codec<MapEntry<String, V?>> { | ||
final Map<String, Codec<V?>?> map; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same case here, where do you store the enum index?
output.pushByte(index); | ||
|
||
final codec = map[value.key]; | ||
|
||
codec?.encodeTo(value.value, output); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary line break.
output.pushByte(index); | |
final codec = map[value.key]; | |
codec?.encodeTo(value.value, output); | |
output.pushByte(index); | |
final codec = map[value.key]; | |
codec?.encodeTo(value.value, output); |
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('I16 Decode Test:', () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also need tests for OutOfBounds
errors
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('I256 Decode Test:', () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also need tests for OutOfBounds
errors
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('I128 Decode Test:', () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also need tests for OutOfBounds
errors
test( | ||
'Given a 0x010100 it should be decoded to Option.some(Option.some(false))', | ||
() { | ||
final input = HexInput('0x010100'); | ||
final result = OptionCodec(OptionCodec(BoolCodec.instance)).decode(input); | ||
expect(result, Option.some(Option.some(false))); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice
group('SimpleEnumCodec Encode', () { | ||
test('should encode and decode', () { | ||
final output = HexOutput(); | ||
final codec = SimpleEnumCodec(['a', 'b', 'c']); | ||
codec.encodeTo('b', output); | ||
expect(output.toString(), '0x01'); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need tests for sparse indexes, I think in this case we can do this:
SimpleEnumCodec.sparse({
'a': 0,
'b': 10,
'c': 20,
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or the inverse (so we guarantee the index is unique):
SimpleEnumCodec.sparse({
0: 'a',
10: 'b',
20: 'c',
});
class Option extends Equatable { | ||
final dynamic value; | ||
final bool _isSome; // Workaround for Option<int?>.some(null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class Option extends Equatable { | |
final dynamic value; | |
final bool _isSome; // Workaround for Option<int?>.some(null) | |
class Option<T> extends Equatable { | |
final T value; | |
final bool _isSome; // Workaround for Option<int?>.some(null) |
#Resolves Issue:
Description
Why?
How?
forth and back
.Testing Plan
dart test