-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathscope_resolver.py
More file actions
42 lines (32 loc) · 1.61 KB
/
scope_resolver.py
File metadata and controls
42 lines (32 loc) · 1.61 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
"""Federation resolution over a :class:`SemanticStore` (★④).
Implements :class:`~lang2sql.core.ports.semantic_scope.ScopeResolverPort`. The
store holds raw per-scope definitions; this resolver walks an identity's
``scope_chain()`` (narrow→wide) and merges them so the most specific definition
of each ``name`` wins. ``define``/``entries_at`` delegate straight to the store.
"""
from __future__ import annotations
from ..core.identity import Identity, Scope
from ..semantic.layer import SemanticLayer
from ..semantic.scoped_layer import merge_scoped
from ..semantic.store import SemanticStore
from ..semantic.types import SemanticEntry
class ScopeResolver:
"""Resolve effective semantic layers, backed by a :class:`SemanticStore`."""
def __init__(self, store: SemanticStore | None = None) -> None:
self._store = store if store is not None else SemanticStore()
@property
def store(self) -> SemanticStore:
return self._store
async def effective_layer(self, identity: Identity) -> SemanticLayer:
"""Merge ``identity``'s scope chain narrow→wide; most specific wins."""
scoped = [
(scope, self._store.entries_at(scope))
for scope in identity.scope_chain()
]
return merge_scoped(scoped)
async def define(self, scope: Scope, entry: SemanticEntry) -> None:
"""Persist one definition at an explicit scope."""
self._store.add(scope, entry)
async def entries_at(self, scope: Scope) -> list[SemanticEntry]:
"""Definitions stored exactly at ``scope`` (no inheritance)."""
return self._store.entries_at(scope)