From 1ff8b11df95e20b31eaa45f8bbce5543dd30f4ff Mon Sep 17 00:00:00 2001 From: ahisbrook Date: Thu, 3 Jul 2014 13:38:48 -0400 Subject: [PATCH 1/3] Fixing encoding of decimals ending in .0 or 0.0 --- lib/cql/protocol/cql_byte_buffer.rb | 8 +++++--- spec/cql/protocol/cql_byte_buffer_spec.rb | 10 ++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/cql/protocol/cql_byte_buffer.rb b/lib/cql/protocol/cql_byte_buffer.rb index f4ef3da..6f031be 100644 --- a/lib/cql/protocol/cql_byte_buffer.rb +++ b/lib/cql/protocol/cql_byte_buffer.rb @@ -2,7 +2,6 @@ require 'bigdecimal' - module Cql module Protocol class CqlByteBuffer < Ione::ByteBuffer @@ -264,9 +263,12 @@ def append_varint(n) end def append_decimal(n) - sign, number_string, _, size = n.split + str = n.to_s('F') + size = str.index('.') + number_string = str.gsub('.', '') + 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 diff --git a/spec/cql/protocol/cql_byte_buffer_spec.rb b/spec/cql/protocol/cql_byte_buffer_spec.rb index bce9868..d5444f5 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 'appends 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')) From f283e6a723b86009d2968e74c7580403ac6ede71 Mon Sep 17 00:00:00 2001 From: ahisbrook Date: Thu, 3 Jul 2014 14:07:58 -0400 Subject: [PATCH 2/3] Fixing encoding of decimals ending in .0 or 0.0 - removed string literals --- lib/cql/protocol/cql_byte_buffer.rb | 8 +++++--- spec/cql/protocol/cql_byte_buffer_spec.rb | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/cql/protocol/cql_byte_buffer.rb b/lib/cql/protocol/cql_byte_buffer.rb index 6f031be..8b9ed68 100644 --- a/lib/cql/protocol/cql_byte_buffer.rb +++ b/lib/cql/protocol/cql_byte_buffer.rb @@ -263,9 +263,9 @@ def append_varint(n) end def append_decimal(n) - str = n.to_s('F') - size = str.index('.') - number_string = str.gsub('.', '') + 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(num) @@ -286,6 +286,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 d5444f5..00c4f3b 100644 --- a/spec/cql/protocol/cql_byte_buffer_spec.rb +++ b/spec/cql/protocol/cql_byte_buffer_spec.rb @@ -858,7 +858,7 @@ module Protocol buffer.should eql_bytes("\x00\x00\x00\x01\tz\xE4b\xD4\xC4") end - it 'appends a BigDecimal ending with 00.0' do + 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 From df9366b36ac45beb36b1e839cefdf54fa70a39a0 Mon Sep 17 00:00:00 2001 From: ahisbrook Date: Thu, 3 Jul 2014 14:09:32 -0400 Subject: [PATCH 3/3] Fixing encoding of decimals ending in .0 or 0.0 --- lib/cql/protocol/cql_byte_buffer.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/cql/protocol/cql_byte_buffer.rb b/lib/cql/protocol/cql_byte_buffer.rb index 8b9ed68..cbf63bd 100644 --- a/lib/cql/protocol/cql_byte_buffer.rb +++ b/lib/cql/protocol/cql_byte_buffer.rb @@ -2,6 +2,7 @@ require 'bigdecimal' + module Cql module Protocol class CqlByteBuffer < Ione::ByteBuffer