Skip to content

Commit 2752d51

Browse files
committed
Replace of_entries with of_items, which matches up with items.
1 parent fcd3958 commit 2752d51

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

python/selfie-lib/selfie_lib/Snapshot.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Dict, Iterable, Union
1+
from typing import Iterator, Union
22

33
from .ArrayMap import ArrayMap
44
from .SnapshotValue import SnapshotValue
@@ -53,18 +53,24 @@ def of(data: Union[bytes, str, SnapshotValue]) -> "Snapshot":
5353
return Snapshot(data, ArrayMap.empty())
5454

5555
@staticmethod
56-
def of_entries(entries: Iterable[Dict[str, SnapshotValue]]) -> "Snapshot":
57-
root = None
56+
def of_items(items: Iterator[tuple[str, SnapshotValue]]) -> "Snapshot":
57+
subject = None
5858
facets = ArrayMap.empty()
59-
for entry in entries:
60-
key, value = entry["key"], entry["value"]
59+
for entry in items:
60+
(key, value) = entry
6161
if key == "":
62-
if root is not None:
63-
raise ValueError("Duplicate root snapshot detected")
64-
root = value
62+
if subject is not None:
63+
raise ValueError(
64+
"Duplicate root snapshot value.\n first: ${subject}\n second: ${value}"
65+
)
66+
subject = value
6567
else:
6668
facets = facets.plus(key, value)
67-
return Snapshot(root if root else SnapshotValue.of(""), facets)
69+
return Snapshot(subject if subject else SnapshotValue.of(""), facets)
70+
71+
def items(self) -> Iterator[tuple[str, SnapshotValue]]:
72+
yield ("", self._subject)
73+
yield from self._facet_data.items()
6874

6975
@staticmethod
7076
def _unix_newlines(string: str) -> str:
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from selfie_lib import Snapshot
2+
3+
4+
def test_items():
5+
undertest = (
6+
Snapshot.of("subject").plus_facet("key1", "value1").plus_facet("key2", "value2")
7+
)
8+
roundtrip = Snapshot.of_items(undertest.items())
9+
assert roundtrip == undertest

0 commit comments

Comments
 (0)