Skip to content

Commit

Permalink
change path to list in json_set and json_get in sandbox (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
CorneliaKelinske committed Jun 2, 2023
1 parent 748da66 commit 02ccb32
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
12 changes: 6 additions & 6 deletions lib/cache/redis/json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,27 @@ defmodule Cache.Redis.JSON do
)
end

defp serialize_path(nil) do
def serialize_path(nil) do
"$"
end

defp serialize_path(path_list) do
def serialize_path(path_list) do
Enum.reduce(path_list, "", &append_to_path(&2, &1))
end

defp append_to_path("", field) do
def append_to_path("", field) do
to_string(field)
end

defp append_to_path(path, field) when is_atom(field) do
def append_to_path(path, field) when is_atom(field) do
append_to_path(path, to_string(field))
end

defp append_to_path(path, index) when is_integer(index) do
def append_to_path(path, index) when is_integer(index) do
"#{path}[#{index}]"
end

defp append_to_path(path, field) when is_binary(field) do
def append_to_path(path, field) when is_binary(field) do
"#{path}.#{field}"
end

Expand Down
4 changes: 4 additions & 0 deletions lib/cache/sandbox.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ defmodule Cache.Sandbox do

use Agent

alias Cache.Redis.JSON

@behaviour Cache

@impl Cache
Expand Down Expand Up @@ -127,6 +129,7 @@ defmodule Cache.Sandbox do
end

def json_get(cache_name, key, path, _opts) do
path = JSON.serialize_path(path)
Agent.get(cache_name, fn state ->
case get_in(state, [key | String.split(path, ".")]) do
nil -> {:error, ErrorMessage.not_found("ERR Path '$.#{path}' does not exist")}
Expand All @@ -136,6 +139,7 @@ defmodule Cache.Sandbox do
end

def json_set(cache_name, key, path, value, _opts) do
path = JSON.serialize_path(path)
Agent.update(cache_name, fn state ->
put_in(state, add_defaults([key | String.split(path, ".")]), value)
end)
Expand Down
2 changes: 2 additions & 0 deletions test/cache/redis_json_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ defmodule Cache.RedisJSONTest do
test "updates a json path", %{key: key} do
assert :ok = RedisCache.json_set(key, [:some_map, :one], 4)
assert {:ok, 4} = RedisCache.json_get(key, [:some_map, :one])
assert :ok = RedisCache.json_set(key, ["some_map.one"], 5)
assert {:ok, 5} = RedisCache.json_get(key, [:some_map, :one])
end

test "returns error tuple if key does not exist" do
Expand Down
17 changes: 12 additions & 5 deletions test/cache_sandbox_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule CacheSandboxTest do

@cache_key "SomeKey"
@cache_value 1234
@cache_path "a.b"
@cache_path [:a, :b]

setup do
Cache.SandboxRegistry.start(TestCache)
Expand All @@ -28,16 +28,23 @@ defmodule CacheSandboxTest do
test "works to seperate caches between tests" do
assert {:ok, nil} = TestCache.get(@cache_key)
end
end

describe "&json_get/2" do
test "gets an item at path" do
assert :ok = TestCache.json_set(@cache_key, @cache_path, @cache_value)
assert {:ok, @cache_value} = TestCache.json_get(@cache_key, @cache_path)
assert {:ok, @cache_value} = TestCache.json_get(@cache_key, ["a.b"])
end

test "works with json commands" do
test "returns :error tuple if path not found" do
assert {:error,
%ErrorMessage{
message: "ERR Path '$.c.d' does not exist",
code: :not_found,
details: nil
}} === TestCache.json_get(@cache_key, "c.d")
assert :ok = TestCache.json_set(@cache_key, @cache_path, @cache_value)
assert {:ok, @cache_value} = TestCache.json_get(@cache_key, @cache_path)
}} === TestCache.json_get(@cache_key, ["c.d"])

end
end
end

0 comments on commit 02ccb32

Please sign in to comment.