Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow None in rehydration #263

Merged
merged 1 commit into from
Apr 29, 2024
Merged
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: 5 additions & 1 deletion src/pypgstac/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ fn hydrate_any<'a>(base: &PyAny, item: &'a PyAny) -> PyResult<&'a PyAny> {
if let Ok(item) = item.downcast::<PyDict>() {
if let Ok(base) = base.downcast::<PyDict>() {
hydrate_dict(base, item).map(|item| item.into())
} else if base.is_none() {
Ok(item)
} else {
Err(anyhow!("type mismatch").into())
}
} else if let Ok(item) = item.downcast::<PyList>() {
if let Ok(base) = base.downcast::<PyList>() {
hydrate_list(base, item).map(|item| item.into())
} else if base.is_none() {
Ok(item)
} else {
Err(anyhow!("type mismatch").into())
}
Expand Down Expand Up @@ -73,7 +77,7 @@ fn hydrate_dict<'a>(base: &PyDict, item: &'a PyDict) -> PyResult<&'a PyDict> {
.map(|s| s == MAGIC_MARKER)
.unwrap_or(false)
{
item.del_item(&key)?;
item.del_item(key)?;
} else {
item.set_item(key, hydrate(base_value, item_value)?)?;
}
Expand Down
11 changes: 10 additions & 1 deletion src/pypgstac/tests/hydration/test_hydrate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test Hydration."""

import json
from copy import deepcopy
from pathlib import Path
Expand Down Expand Up @@ -35,7 +36,9 @@

class TestHydrate:
def hydrate(
self, base_item: Dict[str, Any], item: Dict[str, Any],
self,
base_item: Dict[str, Any],
item: Dict[str, Any],
) -> Dict[str, Any]:
hpy = hydration.hydrate_py(deepcopy(base_item), deepcopy(item))
hrs = hydration.hydrate(deepcopy(base_item), deepcopy(item))
Expand Down Expand Up @@ -234,3 +237,9 @@ def test_top_level_base_keys_marked(self) -> None:
hydrated = self.hydrate(base_item, dehydrated)

assert hydrated == {"included": "value", "unique": "value"}

def test_base_none(self) -> None:
base_item = {"value": None}
dehydrated = {"value": {"a": "b"}}
hydrated = self.hydrate(base_item, dehydrated)
assert hydrated == {"value": {"a": "b"}}
Loading