Skip to content

Commit

Permalink
protocol: Support negative floating point values in responses
Browse files Browse the repository at this point in the history
  • Loading branch information
ohsayan committed May 2, 2024
1 parent 883b2ea commit 374e77e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
16 changes: 14 additions & 2 deletions src/skytable_py/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

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

def parse_float(self, type_symbol: int) -> Union[None, Value]:
if self.__is_eof():
self.__decrement() # move back to type symbol
return None
is_negative = False
if self.__step() == ord('-'):
is_negative = True
else:
self.__decrement() # move back to float starting position since there is no '-'
whole = self.parse_next_int(stop_symbol='.')
if whole:
decimal = self.parse_next_int()
if decimal:
full_float = float(f"{whole}.{decimal}")
if is_negative:
full_float = -full_float
if type_symbol == 10:
return Value(Float32(full_float))
else:
return Value(Float64(full_float))
self.__decrement() # type symbol
if is_negative:
self.__decrement()

def parse_error_code(self) -> Union[None, ErrorCode]:
if self.__remaining() < 2:
Expand Down Expand Up @@ -196,7 +208,7 @@ def parse_row(self) -> Union[None, Row]:
return None
return Row(columns)

def parse_rows(self) -> Union[None, list[Row]]:
def parse_rows(self) -> Union[None, List[Row]]:
cursor_start = self._cursor - 1
row_count = self.parse_next_int()
rows = []
Expand Down
8 changes: 4 additions & 4 deletions src/skytable_py/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# limitations under the License.

from dataclasses import dataclass
from typing import Union
from typing import Union, List
from .exception import ClientException


Expand Down Expand Up @@ -136,7 +136,7 @@ def __eq__(self, other):


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

def __eq__(self, other):
Expand All @@ -151,7 +151,7 @@ class ErrorCode:


class Response:
def __init__(self, resp: Union[Empty, Value, Row, list[Row], ErrorCode]):
def __init__(self, resp: Union[Empty, Value, Row, List[Row], ErrorCode]):
self.data = resp

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

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

Expand Down
4 changes: 4 additions & 0 deletions tests/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ def test_response_types(self):
).value().repr.inner, 3.141592654)
self.assertEquals(Response(Value(Float64(3.141592654))
).value().repr.inner, 3.141592654)
self.assertEquals(Response(Value(Float32(-3.141592654))
).value().repr.inner, -3.141592654)
self.assertEquals(Response(Value(Float64(-3.141592654))
).value().repr.inner, -3.141592654)
# simple collections
self.assertEquals(Response(Value(b"bytes")).value().repr, b"bytes")
self.assertEquals(Response(Value("string")).value().repr, "string")
Expand Down

0 comments on commit 374e77e

Please sign in to comment.