Skip to content

Commit

Permalink
[PLUGIN]: Handle nullable implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
amadolid committed Nov 20, 2024
1 parent 4b86553 commit a00330e
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 30 deletions.
38 changes: 25 additions & 13 deletions jac-cloud/jac_cloud/plugin/jaseci.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@
from fastapi.responses import ORJSONResponse

from jaclang.compiler.constant import EdgeDir
from jaclang.plugin.default import JacFeatureImpl, hookimpl
from jaclang.plugin.default import (
JacCallableImplementation as _JacCallableImplementation,
JacFeatureImpl,
hookimpl,
)
from jaclang.plugin.feature import JacFeature as Jac
from jaclang.runtimelib.architype import DSFunc
from jaclang.runtimelib.architype import Architype, DSFunc

from orjson import loads

Expand All @@ -37,7 +41,6 @@
AccessLevel,
Anchor,
AnchorState,
Architype,
BaseAnchor,
EdgeAnchor,
EdgeArchitype,
Expand Down Expand Up @@ -305,6 +308,22 @@ class DefaultSpecs:
private: bool = False


class JacCallableImplementation:
"""Callable Implementations."""

@staticmethod
def get_object(id: str) -> Architype | None:
"""Get object by id."""
if not FastAPI.is_enabled():
return _JacCallableImplementation.get_object(id=id)

with suppress(ValueError):
if isinstance(architype := BaseAnchor.ref(id).architype, Architype):
return architype

return None


class JacAccessValidationPlugin:
"""Jac Access Validation Implementations."""

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

@staticmethod
@hookimpl
def get_object(id: str) -> Architype | None:
"""Get object via reference id."""
if not FastAPI.is_enabled():
return JacFeatureImpl.get_object(id=id)

with suppress(ValueError):
if isinstance(architype := BaseAnchor.ref(id).architype, Architype):
return architype

return None
def get_object_func() -> Callable[[str], Architype | None]:
"""Get object by id func."""
return JacCallableImplementation.get_object

@staticmethod
@hookimpl
Expand Down
4 changes: 2 additions & 2 deletions jac-cloud/jac_cloud/tests/simple_graph.jac
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ walker get_custom_object {
can enter1 with `root entry {
import:py from jac_cloud.core.architype {BaseAnchor}
try {
report BaseAnchor.ref(self.object_id).architype;
report &(self.object_id);
} except Exception as e {
report None;
}
Expand All @@ -763,7 +763,7 @@ walker update_custom_object {

can enter1 with `root entry {
import:py from jac_cloud.core.architype {BaseAnchor}
savable_object = BaseAnchor.ref(self.object_id).architype;
savable_object = &(self.object_id);
savable_object.parent.child.json["c"] = 3;
savable_object.parent.child.arr.append(3);
savable_object.parent.child.val = 3;
Expand Down
6 changes: 3 additions & 3 deletions jac-cloud/jac_cloud/tests/test_simple_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,19 +560,19 @@ def trigger_upload_file(self) -> None:
"single": {
"name": "simple_graph.jac",
"content_type": "application/octet-stream",
"size": 17352,
"size": 17306,
}
},
"multiple": [
{
"name": "simple_graph.jac",
"content_type": "application/octet-stream",
"size": 17352,
"size": 17306,
},
{
"name": "simple_graph.jac",
"content_type": "application/octet-stream",
"size": 17352,
"size": 17306,
},
],
"singleOptional": None,
Expand Down
25 changes: 17 additions & 8 deletions jac/jaclang/plugin/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@
logger = getLogger(__name__)


class JacCallableImplementation:
"""Callable Implementations."""

@staticmethod
def get_object(id: str) -> Architype | None:
"""Get object by id."""
if id == "root":
return Jac.get_context().root.architype
elif obj := Jac.get_context().mem.find_by_id(UUID(id)):
return obj.architype

return None


class JacAccessValidationImpl:
"""Jac Access Validation Implementations."""

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

@staticmethod
@hookimpl
def get_object(id: str) -> Architype | None:
"""Get object by id."""
if id == "root":
return Jac.get_context().root.architype
elif obj := Jac.get_context().mem.find_by_id(UUID(id)):
return obj.architype

return None
def get_object_func() -> Callable[[str], Architype | None]:
"""Get object by id func."""
return JacCallableImplementation.get_object

@staticmethod
@hookimpl
Expand Down
14 changes: 12 additions & 2 deletions jac/jaclang/plugin/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,15 @@ def create_cmd() -> None:
return plugin_manager.hook.create_cmd()


class JacCallable:
"""Jac Callable Executions."""

@staticmethod
def get_object(id: str) -> Architype | None:
"""Get object given id."""
return plugin_manager.hook.get_object_func()(id=id)


class JacFeature(
JacClassReferences,
JacAccessValidation,
Expand All @@ -246,6 +255,7 @@ class JacFeature(
JacWalker,
JacBuiltin,
JacCmd,
JacCallable,
):
"""Jac Feature."""

Expand All @@ -265,9 +275,9 @@ def reset_graph(root: Optional[Root] = None) -> int:
return plugin_manager.hook.reset_graph(root=root)

@staticmethod
def get_object(id: str) -> Architype | None:
def get_object_func() -> Callable[[str], Architype | None]:
"""Get object given id."""
return plugin_manager.hook.get_object(id=id)
return plugin_manager.hook.get_object_func()

@staticmethod
def object_ref(obj: Architype) -> str:
Expand Down
4 changes: 2 additions & 2 deletions jac/jaclang/plugin/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ def reset_graph(root: Optional[Root]) -> int:

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

@staticmethod
Expand Down

0 comments on commit a00330e

Please sign in to comment.