Skip to content
Open
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
6 changes: 4 additions & 2 deletions sqlite_utils/recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ def parsedatetime(


def jsonsplit(
value: str, delimiter: str = ",", type: Callable[[str], object] = str
) -> str:
value: Optional[str], delimiter: str = ",", type: Callable[[str], object] = str
) -> Optional[str]:
"""
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)])
2 changes: 2 additions & 0 deletions tests/test_recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def test_jsonsplit(fresh_db, delimiter):
[
{"id": 1, "tags": (delimiter or ",").join(["foo", "bar"])},
{"id": 2, "tags": (delimiter or ",").join(["bar", "baz"])},
{"id": 3, "tags": None},
],
pk="id",
)
Expand All @@ -116,6 +117,7 @@ def fn(value):
assert list(fresh_db["example"].rows) == [
{"id": 1, "tags": '["foo", "bar"]'},
{"id": 2, "tags": '["bar", "baz"]'},
{"id": 3, "tags": None},
]


Expand Down
Loading