Skip to content

Commit

Permalink
Properly handle Binary fields
Browse files Browse the repository at this point in the history
  • Loading branch information
andreausu authored and darrenklein committed Aug 27, 2019
1 parent 81cc698 commit c31b253
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/ex_aws/dynamo/decoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ defmodule ExAws.Dynamo.Decoder do
def decode(%{"BOOL" => "false"}), do: false
def decode(%{"NULL" => true}), do: nil
def decode(%{"NULL" => "true"}), do: nil
def decode(%{"B" => value}), do: value
def decode(%{"B" => value}), do: Base.decode64!(value)
def decode(%{"S" => value}), do: value
def decode(%{"M" => value}), do: value |> decode
if Application.get_env(:ex_aws, :dynamodb, [])[:decode_sets] do
Expand Down
6 changes: 5 additions & 1 deletion lib/ex_aws/dynamo/encodable.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ end

defimpl ExAws.Dynamo.Encodable, for: BitString do
def encode(val, _) do
%{"S" => val}
if String.valid?(val) do
%{"S" => val}
else
%{"B" => Base.encode64(val)}
end
end
end

Expand Down
8 changes: 8 additions & 0 deletions test/lib/dynamo/decoder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,12 @@ defmodule ExAws.Dynamo.DecoderTest do
user = %Test.User{email: "foo@bar.com", name: "Bob", age: 23, admin: false}
assert user == user |> Encoder.encode() |> Decoder.decode(as: Test.User)
end

test "Decoder binary that are not strings works" do
assert :zlib.unzip(
Decoder.decode(%{
"B" => "BcGBCQAgCATAVX6ZBvlKUogP1P3pbmi9bYlFwal9DTPEDCu0s8E06DWqM3TqAw=="
})
) == "Encoder can handle binaries that are not strings"
end
end
6 changes: 6 additions & 0 deletions test/lib/dynamo/encoder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ defmodule ExAws.Dynamo.EncoderTest do
}
end

test "Encoder can handle binaries that are not strings" do
assert Encoder.encode(:zlib.zip("Encoder can handle binaries that are not strings")) == %{
"B" => "BcGBCQAgCATAVX6ZBvlKUogP1P3pbmi9bYlFwal9DTPEDCu0s8E06DWqM3TqAw=="
}
end

test "Encoder with structs works properly" do
user = %Test.User{email: "foo@bar.com", name: "Bob", age: 23, admin: false}

Expand Down

0 comments on commit c31b253

Please sign in to comment.