Skip to content

Commit

Permalink
fix(datastore): fix parsing type list to tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
jialeicui committed May 15, 2023
1 parent 9569686 commit 0dd0978
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
5 changes: 4 additions & 1 deletion client/starwhale/api/_impl/data_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,10 @@ def decode_schema_from_type_encoded_value(value: Dict[str, Any]) -> "SwType":
# TODO: support more than one item types
if isinstance(v, (list, tuple)) and len(v) != 0:
element_type = SwType.decode_schema_from_type_encoded_value(v[0])
return SwTupleType(element_type)
if type_name == "LIST":
return SwListType(element_type)
else:
return SwTupleType(element_type)
if type_name == "MAP":
# {
# "type": "MAP",
Expand Down
26 changes: 26 additions & 0 deletions client/tests/sdk/test_data_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
STRING,
SwType,
SwMapType,
SwListType,
SwTupleType,
ColumnSchema,
TableWriterException,
)
Expand Down Expand Up @@ -2385,6 +2387,30 @@ def test_decode_schema_from_type_encoded_values():
decoded = schema.decode_from_type_encoded_value(value)
assert decoded == {1: "foobar"}

# list
value = {
"type": "LIST",
"value": [
{"type": "STRING", "value": "foobar"},
],
}
schema = SwType.decode_schema_from_type_encoded_value(value)
assert schema == SwListType(STRING)
decoded = schema.decode_from_type_encoded_value(value)
assert decoded == ["foobar"]

# tuple
value = {
"type": "TUPLE",
"value": [
{"type": "STRING", "value": "foobar"},
],
}
schema = SwType.decode_schema_from_type_encoded_value(value)
assert schema == SwTupleType(STRING)
decoded = schema.decode_from_type_encoded_value(value)
assert decoded == ("foobar",)


if __name__ == "__main__":
unittest.main()

0 comments on commit 0dd0978

Please sign in to comment.