Skip to content

Commit 72b37fc

Browse files
committed
feat: added id as output type
1 parent c9ae233 commit 72b37fc

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/eodm/cli/_serialization.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
from typing import Iterable, Protocol
22

3-
from eodm.serializers import default_serializer, json_serializer
3+
from eodm.serializers import default_serializer, id_serializer, json_serializer
44

55
from ._types import Output
66

77

88
class Mappable(Protocol):
99
def to_dict(self): ...
1010

11+
@property
12+
def id(self) -> str: ...
13+
1114

1215
def serialize(data: Iterable[Mappable], output_type: Output) -> None:
1316
match output_type:
1417
case Output.json:
1518
print(json_serializer(data))
19+
case Output.id:
20+
for d in id_serializer(data):
21+
print(d)
1622
case _:
1723
for d in default_serializer(data):
1824
print(d)

src/eodm/cli/_types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def datetime_interval(value: str) -> DatetimeInterval:
6464
class Output(str, Enum):
6565
default = "default"
6666
json = "json"
67+
id = "id"
6768

6869

6970
BBoxType = Annotated[

src/eodm/serializers.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@
55
class Mappable(Protocol):
66
def to_dict(self): ...
77

8+
@property
9+
def id(self) -> str: ...
10+
11+
12+
def id_serializer(items: Iterable[Mappable]) -> Iterable[str]:
13+
"""
14+
Serializes a list of Mappables (implementing to_dict()) to a list of ids
15+
Args:
16+
items (Iterable[Mappable]): A collection of Mappable items
17+
Returns:
18+
Iterable[str]: item as a string
19+
Yields:
20+
Iterator[Iterable[str]]: Collection of ids
21+
"""
22+
23+
for item in items:
24+
yield item.id
25+
826

927
def default_serializer(items: Iterable[Mappable]) -> Iterable[str]:
1028
"""Serializes a list of Mappables (implementing to_dict()) to json strings

0 commit comments

Comments
 (0)