Skip to content
This repository has been archived by the owner on Jan 2, 2023. It is now read-only.

Fixing encoding of decimals ending in .0 or 0.0 #115

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/cql/protocol/cql_byte_buffer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
10 changes: 10 additions & 0 deletions spec/cql/protocol/cql_byte_buffer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down