Skip to content

Commit 6f67a8a

Browse files
committed
[PLUGIN]: Handle nullable implementation
1 parent 4b86553 commit 6f67a8a

File tree

6 files changed

+50
-29
lines changed

6 files changed

+50
-29
lines changed

jac-cloud/jac_cloud/plugin/jaseci.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@
2323
from fastapi.responses import ORJSONResponse
2424

2525
from jaclang.compiler.constant import EdgeDir
26-
from jaclang.plugin.default import JacFeatureImpl, hookimpl
26+
from jaclang.plugin.default import (
27+
JacCallableImplementation as _JacCallableImplementation,
28+
JacFeatureImpl,
29+
hookimpl,
30+
)
2731
from jaclang.plugin.feature import JacFeature as Jac
28-
from jaclang.runtimelib.architype import DSFunc
32+
from jaclang.runtimelib.architype import Architype, DSFunc
2933

3034
from orjson import loads
3135

@@ -37,7 +41,6 @@
3741
AccessLevel,
3842
Anchor,
3943
AnchorState,
40-
Architype,
4144
BaseAnchor,
4245
EdgeAnchor,
4346
EdgeArchitype,
@@ -305,6 +308,22 @@ class DefaultSpecs:
305308
private: bool = False
306309

307310

311+
class JacCallableImplementation:
312+
"""Callable Implementations."""
313+
314+
@staticmethod
315+
def get_object(id: str) -> Architype | None:
316+
"""Get object by id."""
317+
if not FastAPI.is_enabled():
318+
return _JacCallableImplementation.get_object(id=id)
319+
320+
with suppress(ValueError):
321+
if isinstance(architype := BaseAnchor.ref(id).architype, Architype):
322+
return architype
323+
324+
return None
325+
326+
308327
class JacAccessValidationPlugin:
309328
"""Jac Access Validation Implementations."""
310329

@@ -729,16 +748,9 @@ def builder(source: NodeAnchor, target: NodeAnchor) -> EdgeArchitype:
729748

730749
@staticmethod
731750
@hookimpl
732-
def get_object(id: str) -> Architype | None:
733-
"""Get object via reference id."""
734-
if not FastAPI.is_enabled():
735-
return JacFeatureImpl.get_object(id=id)
736-
737-
with suppress(ValueError):
738-
if isinstance(architype := BaseAnchor.ref(id).architype, Architype):
739-
return architype
740-
741-
return None
751+
def get_object_func() -> Callable[[str], Architype | None]:
752+
"""Get object by id func."""
753+
return JacCallableImplementation.get_object
742754

743755
@staticmethod
744756
@hookimpl

jac-cloud/jac_cloud/tests/simple_graph.jac

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ walker get_custom_object {
751751
can enter1 with `root entry {
752752
import:py from jac_cloud.core.architype {BaseAnchor}
753753
try {
754-
report BaseAnchor.ref(self.object_id).architype;
754+
report &(self.object_id);
755755
} except Exception as e {
756756
report None;
757757
}
@@ -763,7 +763,7 @@ walker update_custom_object {
763763

764764
can enter1 with `root entry {
765765
import:py from jac_cloud.core.architype {BaseAnchor}
766-
savable_object = BaseAnchor.ref(self.object_id).architype;
766+
savable_object = &(self.object_id);
767767
savable_object.parent.child.json["c"] = 3;
768768
savable_object.parent.child.arr.append(3);
769769
savable_object.parent.child.val = 3;

jac-cloud/jac_cloud/tests/test_simple_graph.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,19 +560,19 @@ def trigger_upload_file(self) -> None:
560560
"single": {
561561
"name": "simple_graph.jac",
562562
"content_type": "application/octet-stream",
563-
"size": 17352,
563+
"size": 17306,
564564
}
565565
},
566566
"multiple": [
567567
{
568568
"name": "simple_graph.jac",
569569
"content_type": "application/octet-stream",
570-
"size": 17352,
570+
"size": 17306,
571571
},
572572
{
573573
"name": "simple_graph.jac",
574574
"content_type": "application/octet-stream",
575-
"size": 17352,
575+
"size": 17306,
576576
},
577577
],
578578
"singleOptional": None,

jac/jaclang/plugin/default.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@
5151
logger = getLogger(__name__)
5252

5353

54+
class JacCallableImplementation:
55+
"""Callable Implementations."""
56+
57+
@staticmethod
58+
def get_object(id: str) -> Architype | None:
59+
"""Get object by id."""
60+
if id == "root":
61+
return Jac.get_context().root.architype
62+
elif obj := Jac.get_context().mem.find_by_id(UUID(id)):
63+
return obj.architype
64+
65+
return None
66+
67+
5468
class JacAccessValidationImpl:
5569
"""Jac Access Validation Implementations."""
5670

@@ -599,14 +613,9 @@ def reset_graph(root: Optional[Root] = None) -> int:
599613

600614
@staticmethod
601615
@hookimpl
602-
def get_object(id: str) -> Architype | None:
603-
"""Get object by id."""
604-
if id == "root":
605-
return Jac.get_context().root.architype
606-
elif obj := Jac.get_context().mem.find_by_id(UUID(id)):
607-
return obj.architype
608-
609-
return None
616+
def get_object_func() -> Callable[[str], Architype | None]:
617+
"""Get object by id func."""
618+
return JacCallableImplementation.get_object
610619

611620
@staticmethod
612621
@hookimpl

jac/jaclang/plugin/feature.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def reset_graph(root: Optional[Root] = None) -> int:
267267
@staticmethod
268268
def get_object(id: str) -> Architype | None:
269269
"""Get object given id."""
270-
return plugin_manager.hook.get_object(id=id)
270+
return plugin_manager.hook.get_object_func()(id=id)
271271

272272
@staticmethod
273273
def object_ref(obj: Architype) -> str:

jac/jaclang/plugin/spec.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,8 @@ def reset_graph(root: Optional[Root]) -> int:
258258

259259
@staticmethod
260260
@hookspec(firstresult=True)
261-
def get_object(id: str) -> Architype | None:
262-
"""Get object by id."""
261+
def get_object_func() -> Callable[[str], Architype | None]:
262+
"""Get object by id func."""
263263
raise NotImplementedError
264264

265265
@staticmethod

0 commit comments

Comments
 (0)