Skip to content

Commit

Permalink
fix empty set as a single object property decoding (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsidnev authored Jun 17, 2023
1 parent 0a7dcf9 commit bb2e416
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- crash after updating `db_connection` to `2.5`.
- decoding a single propery for `EdgeDB.Object` that equals to an empty set.
- not catching an `EdgeDB.Error` exception during parameters encoding,
which caused throwing an exception for non-`!` functions.

Expand Down
13 changes: 12 additions & 1 deletion lib/edgedb/protocol/codecs/object.ex
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,20 @@ defimpl EdgeDB.Protocol.Codec, for: EdgeDB.Protocol.Codecs.Object do
element.name
end

single_property? =
element.cardinality in [:at_most_one, :one] and
(link_property?(element) or not link?(element))

value =
if single_property? do
nil
else
@empty_set
end

field = %EdgeDB.Object.Field{
name: name,
value: @empty_set,
value: value,
is_link: link?(element),
is_link_property: link_property?(element),
is_implicit: implicit?(element)
Expand Down
32 changes: 21 additions & 11 deletions test/edgedb/protocol/codecs/object_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -232,17 +232,27 @@ defmodule Tests.EdgeDB.Protocol.Codecs.ObjectTest do
end)
end

test "decoding object with property equals to empty set", %{client: client} do
rollback(client, fn client ->
object =
EdgeDB.query_required_single!(client, """
select {
a := <str>{}
}
limit 1
""")
test "decoding object with a single property equals to empty set", %{client: client} do
object =
EdgeDB.query_required_single!(client, """
select {
a := <str>{}
}
limit 1
""")

assert Enum.empty?(object[:a])
end)
assert is_nil(object[:a])
end

test "decoding object with a multi property equals to empty set", %{client: client} do
object =
EdgeDB.query_required_single!(client, """
select {
multi a := <str>{}
}
limit 1
""")

assert Enum.empty?(object[:a])
end
end

0 comments on commit bb2e416

Please sign in to comment.