Skip to content

Commit 374e77e

Browse files
committed
protocol: Support negative floating point values in responses
1 parent 883b2ea commit 374e77e

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

src/skytable_py/protocol.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
from typing import Union
16+
from typing import Union, List
1717
from .exception import ProtocolException
1818
from .response import Value, UInt8, UInt16, UInt32, UInt64, SInt8, SInt16, SInt32, SInt64, Float32, Float64, Empty, \
1919
ErrorCode, Row, Response
@@ -145,16 +145,28 @@ def parse_sint(self, type_symbol: int) -> Union[None, Value]:
145145
self.__decrement() # move back to starting position of this integer
146146

147147
def parse_float(self, type_symbol: int) -> Union[None, Value]:
148+
if self.__is_eof():
149+
self.__decrement() # move back to type symbol
150+
return None
151+
is_negative = False
152+
if self.__step() == ord('-'):
153+
is_negative = True
154+
else:
155+
self.__decrement() # move back to float starting position since there is no '-'
148156
whole = self.parse_next_int(stop_symbol='.')
149157
if whole:
150158
decimal = self.parse_next_int()
151159
if decimal:
152160
full_float = float(f"{whole}.{decimal}")
161+
if is_negative:
162+
full_float = -full_float
153163
if type_symbol == 10:
154164
return Value(Float32(full_float))
155165
else:
156166
return Value(Float64(full_float))
157167
self.__decrement() # type symbol
168+
if is_negative:
169+
self.__decrement()
158170

159171
def parse_error_code(self) -> Union[None, ErrorCode]:
160172
if self.__remaining() < 2:
@@ -196,7 +208,7 @@ def parse_row(self) -> Union[None, Row]:
196208
return None
197209
return Row(columns)
198210

199-
def parse_rows(self) -> Union[None, list[Row]]:
211+
def parse_rows(self) -> Union[None, List[Row]]:
200212
cursor_start = self._cursor - 1
201213
row_count = self.parse_next_int()
202214
rows = []

src/skytable_py/response.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# limitations under the License.
1515

1616
from dataclasses import dataclass
17-
from typing import Union
17+
from typing import Union, List
1818
from .exception import ClientException
1919

2020

@@ -136,7 +136,7 @@ def __eq__(self, other):
136136

137137

138138
class Row:
139-
def __init__(self, values: list[Value]) -> None:
139+
def __init__(self, values: List[Value]) -> None:
140140
self.columns = values
141141

142142
def __eq__(self, other):
@@ -151,7 +151,7 @@ class ErrorCode:
151151

152152

153153
class Response:
154-
def __init__(self, resp: Union[Empty, Value, Row, list[Row], ErrorCode]):
154+
def __init__(self, resp: Union[Empty, Value, Row, List[Row], ErrorCode]):
155155
self.data = resp
156156

157157
def is_empty(self) -> bool:
@@ -165,7 +165,7 @@ def row(self) -> Union[None, Row]:
165165
if isinstance(self.data, Row):
166166
return self.data
167167

168-
def rows(self) -> Union[None, list[Row]]:
168+
def rows(self) -> Union[None, List[Row]]:
169169
if isinstance(self.data, list):
170170
return self.data
171171

tests/test_response.py

+4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ def test_response_types(self):
5959
).value().repr.inner, 3.141592654)
6060
self.assertEquals(Response(Value(Float64(3.141592654))
6161
).value().repr.inner, 3.141592654)
62+
self.assertEquals(Response(Value(Float32(-3.141592654))
63+
).value().repr.inner, -3.141592654)
64+
self.assertEquals(Response(Value(Float64(-3.141592654))
65+
).value().repr.inner, -3.141592654)
6266
# simple collections
6367
self.assertEquals(Response(Value(b"bytes")).value().repr, b"bytes")
6468
self.assertEquals(Response(Value("string")).value().repr, "string")

0 commit comments

Comments
 (0)