Skip to content

Commit

Permalink
Allow None in rehydration (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebarron authored Apr 29, 2024
1 parent 513de60 commit 35a4d1c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
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"}}

0 comments on commit 35a4d1c

Please sign in to comment.