Skip to content

Commit

Permalink
Handle nested objects containing dates, closes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Nov 29, 2019
1 parent 48088e4 commit 0d58154
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
11 changes: 10 additions & 1 deletion tests/test_cli.py
@@ -1,20 +1,29 @@
from click.testing import CliRunner
from yaml_to_sqlite import cli
import sqlite_utils
import json


TEST_YAML = """
- name: datasette-cluster-map
url: https://github.com/simonw/datasette-cluster-map
- name: datasette-vega
url: https://github.com/simonw/datasette-vega
nested_with_date:
- title: Hello
date: 2010-01-01
"""
EXPECTED = [
{
"name": "datasette-cluster-map",
"url": "https://github.com/simonw/datasette-cluster-map",
"nested_with_date": None,
},
{
"name": "datasette-vega",
"url": "https://github.com/simonw/datasette-vega",
"nested_with_date": json.dumps([{"title": "Hello", "date": "2010-01-01"}]),
},
{"name": "datasette-vega", "url": "https://github.com/simonw/datasette-vega"},
]


Expand Down
7 changes: 6 additions & 1 deletion yaml_to_sqlite/cli.py
@@ -1,6 +1,7 @@
import click
import yaml
import sqlite_utils
import json


@click.command()
Expand All @@ -14,4 +15,8 @@
def cli(db_path, table, yaml_file, pk):
"Covert YAML files to SQLite"
db = sqlite_utils.Database(db_path)
db[table].upsert_all(yaml.safe_load(yaml_file), pk=pk)
docs = yaml.safe_load(yaml_file)
# We round-trip the docs to JSON to ensure anything unexpected
# like date objects is converted to valid JSON values
docs = json.loads(json.dumps(docs, default=str))
db[table].upsert_all(docs, pk=pk)

0 comments on commit 0d58154

Please sign in to comment.