|
1 | | -from typing import Dict, Iterable, Union |
| 1 | +from typing import Iterator, Union |
2 | 2 |
|
3 | 3 | from .ArrayMap import ArrayMap |
4 | 4 | from .SnapshotValue import SnapshotValue |
@@ -53,18 +53,24 @@ def of(data: Union[bytes, str, SnapshotValue]) -> "Snapshot": |
53 | 53 | return Snapshot(data, ArrayMap.empty()) |
54 | 54 |
|
55 | 55 | @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 |
58 | 58 | facets = ArrayMap.empty() |
59 | | - for entry in entries: |
60 | | - key, value = entry["key"], entry["value"] |
| 59 | + for entry in items: |
| 60 | + (key, value) = entry |
61 | 61 | 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 |
65 | 67 | else: |
66 | 68 | 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() |
68 | 74 |
|
69 | 75 | @staticmethod |
70 | 76 | def _unix_newlines(string: str) -> str: |
|
0 commit comments