diff --git a/packages/agent_dart_base/lib/agent/utils/leb128.dart b/packages/agent_dart_base/lib/agent/utils/leb128.dart index b0183146..5f86dc85 100644 --- a/packages/agent_dart_base/lib/agent/utils/leb128.dart +++ b/packages/agent_dart_base/lib/agent/utils/leb128.dart @@ -22,12 +22,7 @@ List safeRead(BufferPipe pipe, int ref) { /// nearest integer. /// @param value The number to encode. Uint8List lebEncode(dynamic value) { - BigInt bn = switch (value) { - BigInt() => value, - num() => BigInt.from(value), - String() => BigInt.parse(value), - _ => throw ArgumentError('Invalid big number: $value', 'lebEncode'), - }; + BigInt bn = value is BigInt ? value : BigInt.from(value); if (bn < BigInt.zero) { throw StateError('Cannot leb-encode negative values.'); } @@ -63,13 +58,9 @@ BigInt lebDecode(BufferPipe pipe) { /// Encode a number (or bigint) into a Buffer, with support for negative numbers. /// The number will be floored to the nearest integer. /// @param value The number to encode. -Uint8List slebEncode(dynamic value) { - BigInt bn = switch (value) { - BigInt() => value, - num() => BigInt.from(value), - String() => BigInt.parse(value), - _ => throw ArgumentError('Invalid big number: $value', 'slebEncode'), - }; +Uint8List slebEncode(Comparable value) { + BigInt bn = value is BigInt ? value : BigInt.from(value as num); + final isNeg = bn < BigInt.zero; if (isNeg) { bn = -bn - BigInt.one; diff --git a/packages/agent_dart_base/lib/candid/idl.dart b/packages/agent_dart_base/lib/candid/idl.dart index 31c80a41..5238ee20 100644 --- a/packages/agent_dart_base/lib/candid/idl.dart +++ b/packages/agent_dart_base/lib/candid/idl.dart @@ -533,9 +533,7 @@ class NatClass extends PrimitiveType { @override bool covariant(x) { - return (x is BigInt && x >= BigInt.zero) || - (x is int && x >= 0) || - (x is String && BigInt.parse(x) >= BigInt.zero); + return (x is BigInt && x >= BigInt.zero) || (x is int && x >= 0); } @override diff --git a/packages/agent_dart_base/test/agent/utils/leb128.dart b/packages/agent_dart_base/test/agent/utils/leb128.dart index 831e949a..072a97aa 100644 --- a/packages/agent_dart_base/test/agent/utils/leb128.dart +++ b/packages/agent_dart_base/test/agent/utils/leb128.dart @@ -24,7 +24,6 @@ void leb128Test() { lebEncode(BigInt.from(60000000000000000)).toHex(), '808098f4e9b5ca6a', ); - expect(lebEncode('1').toHex(), '01'); expect(lebDecode(BufferPipe(Uint8List.fromList([0]))), BigInt.zero); expect(lebDecode(BufferPipe(Uint8List.fromList([1]))), BigInt.one); @@ -60,7 +59,6 @@ void leb128Test() { slebEncode(BigInt.parse('60000000000000000')).toHex(), '808098f4e9b5caea00', ); - expect(slebEncode('1').toHex(), '01'); expect( slebDecode(BufferPipe(Uint8List.fromList([0x7f]))),