mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-27 12:24:11 +02:00
fix: allow recipes.jsonsplit to pass through NULL values
Table.convert with jsonsplit raised OperationalError on NULL cells because None.split was called. Match parsedate and return None unchanged.
This commit is contained in:
parent
a947dc6739
commit
4f4d823f1c
2 changed files with 6 additions and 2 deletions
|
|
@ -68,9 +68,11 @@ def parsedatetime(
|
||||||
|
|
||||||
|
|
||||||
def jsonsplit(
|
def jsonsplit(
|
||||||
value: str, delimiter: str = ",", type: Callable[[str], object] = str
|
value: Optional[str], delimiter: str = ",", type: Callable[[str], object] = str
|
||||||
) -> str:
|
) -> Optional[str]:
|
||||||
"""
|
"""
|
||||||
Convert a string like a,b,c into a JSON array ["a", "b", "c"]
|
Convert a string like a,b,c into a JSON array ["a", "b", "c"]
|
||||||
"""
|
"""
|
||||||
|
if value is None:
|
||||||
|
return value
|
||||||
return json.dumps([type(s.strip()) for s in value.split(delimiter)])
|
return json.dumps([type(s.strip()) for s in value.split(delimiter)])
|
||||||
|
|
|
||||||
|
|
@ -101,6 +101,7 @@ def test_jsonsplit(fresh_db, delimiter):
|
||||||
[
|
[
|
||||||
{"id": 1, "tags": (delimiter or ",").join(["foo", "bar"])},
|
{"id": 1, "tags": (delimiter or ",").join(["foo", "bar"])},
|
||||||
{"id": 2, "tags": (delimiter or ",").join(["bar", "baz"])},
|
{"id": 2, "tags": (delimiter or ",").join(["bar", "baz"])},
|
||||||
|
{"id": 3, "tags": None},
|
||||||
],
|
],
|
||||||
pk="id",
|
pk="id",
|
||||||
)
|
)
|
||||||
|
|
@ -116,6 +117,7 @@ def test_jsonsplit(fresh_db, delimiter):
|
||||||
assert list(fresh_db["example"].rows) == [
|
assert list(fresh_db["example"].rows) == [
|
||||||
{"id": 1, "tags": '["foo", "bar"]'},
|
{"id": 1, "tags": '["foo", "bar"]'},
|
||||||
{"id": 2, "tags": '["bar", "baz"]'},
|
{"id": 2, "tags": '["bar", "baz"]'},
|
||||||
|
{"id": 3, "tags": None},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue