-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from __future__ import annotations | ||
|
||
import json | ||
from typing import Any, Union | ||
|
||
from attrs import define, field | ||
|
||
from griptape.artifacts import BaseArtifact | ||
|
||
Json = Union[dict[str, "Json"], list["Json"], str, int, float, bool, None] | ||
|
||
|
||
@define | ||
class JsonArtifact(BaseArtifact): | ||
_obj: Any = field(metadata={"serializable": True}, alias="obj") | ||
value: dict = field(init=False, metadata={"serializable": True}) | ||
_json_str: str = field(init=False, metadata={"serializable": True}) | ||
|
||
def __attrs_post_init__(self) -> None: | ||
if self._obj is None: | ||
self._obj = {} | ||
self._json_str = json.dumps(self._obj) | ||
self.value = json.loads(self._json_str) | ||
|
||
def to_text(self) -> str: | ||
return self._json_str | ||
|
||
def __add__(self, other: BaseArtifact) -> JsonArtifact: | ||
if not isinstance(other, JsonArtifact): | ||
raise ValueError(f"Cannot add {type(self)} and {type(other)}") | ||
|
||
from griptape.utils import dict_merge | ||
|
||
self.value = dict_merge(self.value, other.value) | ||
return self |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import json | ||
|
||
import pytest | ||
|
||
from griptape.artifacts import JsonArtifact, TextArtifact | ||
|
||
|
||
class TestJsonArtifact: | ||
def test_value_type_conversion(self): | ||
assert JsonArtifact({"foo": "bar"}).value == json.loads(json.dumps({"foo": "bar"})) | ||
|
||
def test___add__(self): | ||
assert (JsonArtifact({"foo": "bar"}) + JsonArtifact({"value": "baz"})).value == JsonArtifact( | ||
{"foo": "bar", "value": "baz"} | ||
).value | ||
|
||
new = JsonArtifact({"foo": "bar"}) + JsonArtifact({"value": "baz"}) | ||
assert new.value == {"foo": "bar", "value": "baz"} | ||
|
||
assert (JsonArtifact({"foo": "bar"}) + JsonArtifact({"foo": "baz"})).value == JsonArtifact({"foo": "baz"}).value | ||
with pytest.raises(ValueError): | ||
JsonArtifact({"foo": "bar"}) + TextArtifact("invalid json") | ||
|
||
def test_to_text(self): | ||
assert JsonArtifact({"foo": "bar"}).to_text() == json.dumps({"foo": "bar"}) |