-
Notifications
You must be signed in to change notification settings - Fork 792
Expand file tree
/
Copy pathflat_target_app.py
More file actions
60 lines (45 loc) · 1.58 KB
/
flat_target_app.py
File metadata and controls
60 lines (45 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""Test app with flat/leaf target states only (no child providers)."""
from __future__ import annotations
import pathlib
from typing import Any, Collection
import cocoindex as coco
_HERE = pathlib.Path(__file__).resolve().parent
DB_PATH = _HERE / "cocoindex.db"
env = coco.Environment(coco.Settings.from_env(db_path=DB_PATH))
class _FlatStore:
def __init__(self) -> None:
self.data: dict[str, Any] = {}
def _sink(
self,
context_provider: coco.ContextProvider,
actions: Collection[tuple[str, Any | coco.NonExistenceType]],
/,
) -> None:
for key, value in actions:
if coco.is_non_existence(value):
self.data.pop(key, None)
else:
self.data[key] = value
def reconcile(
self,
key: coco.StableKey,
desired_state: Any | coco.NonExistenceType,
prev_possible_records: Collection[Any],
prev_may_be_missing: bool,
) -> (
coco.TargetReconcileOutput[tuple[str, Any | coco.NonExistenceType], Any] | None
):
assert isinstance(key, str)
return coco.TargetReconcileOutput(
action=(key, desired_state),
sink=coco.TargetActionSink.from_fn(self._sink),
tracking_record=desired_state,
)
_flat_store = _FlatStore()
_provider = coco.register_root_target_states_provider(
"test_cli/flat_preview", _flat_store
)
@coco.fn
def build() -> None:
coco.declare_target_state(_provider.target_state("x", 42))
app = coco.App(coco.AppConfig(name="FlatPreviewApp", environment=env), build)