From 228cddb8c7edd9a3f7598b81ed1cf5d534a7fcae Mon Sep 17 00:00:00 2001 From: ahisbrook Date: Thu, 3 Jul 2014 13:38:48 -0400 Subject: [PATCH] Fixing encoding of decimals ending in .0 or 0.0 --- lib/cql/protocol/cql_byte_buffer.rb | 9 +++++++-- spec/cql/protocol/cql_byte_buffer_spec.rb | 10 ++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/cql/protocol/cql_byte_buffer.rb b/lib/cql/protocol/cql_byte_buffer.rb index f4ef3da..cbf63bd 100644 --- a/lib/cql/protocol/cql_byte_buffer.rb +++ b/lib/cql/protocol/cql_byte_buffer.rb @@ -264,9 +264,12 @@ def append_varint(n) end def append_decimal(n) - sign, number_string, _, size = n.split + str = n.to_s(FLOAT_STRING_FORMAT) + size = str.index(DECIMAL_POINT) + number_string = str.gsub(DECIMAL_POINT, NO_CHAR) + num = number_string.to_i - raw = self.class.new.append_varint(sign * num) + raw = self.class.new.append_varint(num) append_int(number_string.length - size) append(raw) end @@ -284,6 +287,8 @@ def append_float(n) MINUS = '-'.freeze ZERO = '0'.freeze DECIMAL_POINT = '.'.freeze + FLOAT_STRING_FORMAT = 'F'.freeze + NO_CHAR = ''.freeze end end end \ No newline at end of file diff --git a/spec/cql/protocol/cql_byte_buffer_spec.rb b/spec/cql/protocol/cql_byte_buffer_spec.rb index bce9868..00c4f3b 100644 --- a/spec/cql/protocol/cql_byte_buffer_spec.rb +++ b/spec/cql/protocol/cql_byte_buffer_spec.rb @@ -853,6 +853,16 @@ module Protocol buffer.should eql_bytes("\x00\x00\x00\x01\x00") end + it 'encodes a BigDecimal ending in .0' do + buffer.append_decimal(BigDecimal.new('1042342234234.0')) + buffer.should eql_bytes("\x00\x00\x00\x01\tz\xE4b\xD4\xC4") + end + + it 'encodes a BigDecimal ending with 00.0' do + buffer.append_decimal(BigDecimal.new('12000.0')) + buffer.should eql_bytes("\x00\x00\x00\x01\x01\xD4\xC0") + end + it 'appends to the buffer' do buffer << "\x99" buffer.append_decimal(BigDecimal.new('1042342234234.123423435647768234'))