Skip to content

Commit c65c316

Browse files
authored
Merge pull request #121 from wjwitek/feature/derive-except
Add :except to derive options
2 parents 59dc659 + 8e8218e commit c65c316

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

lib/ex_aws/dynamo/encodable.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ defimpl ExAws.Dynamo.Encodable, for: Any do
3737

3838
def deriving(module, _struct, options) do
3939
extractor =
40-
if only = options[:only] do
41-
quote(do: Map.take(struct, unquote(only)))
42-
else
43-
quote(do: :maps.remove(:__struct__, struct))
40+
cond do
41+
only = options[:only] -> quote(do: Map.take(struct, unquote(only)))
42+
except = options[:except] -> quote(do: Map.drop(struct, [:__struct__] ++ unquote(except)))
43+
true -> quote(do: :maps.remove(:__struct__, struct))
4444
end
4545

4646
quote do

test/lib/dynamo/encoder_test.exs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,16 @@ defmodule ExAws.Dynamo.EncoderTest do
8686
assert Encoder.encode(nil) == %{"NULL" => true}
8787
assert Encoder.encode(%{"key" => nil}) == %{"M" => %{"key" => %{"NULL" => true}}}
8888
end
89+
90+
test "encoder skips struct fields that are marked as excepted" do
91+
user_except =
92+
%Test.ExceptUser{email: "[email protected]", name: "Bob", age: 23, secret: "secret information"}
93+
|> Encoder.encode_root()
94+
95+
assert %{
96+
"age" => %{"M" => %{"N" => %{"S" => "23"}}},
97+
"email" => %{"M" => %{"S" => %{"S" => "[email protected]"}}},
98+
"name" => %{"M" => %{"S" => %{"S" => "Bob"}}}
99+
} == Encoder.encode_root(user_except)
100+
end
89101
end

test/support/mock_models.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@ defmodule Test.Nested do
1717
@derive {ExAws.Dynamo.Encodable, only: [:items]}
1818
defstruct items: [], secret: nil
1919
end
20+
21+
defmodule Test.ExceptUser do
22+
@moduledoc false
23+
@derive {ExAws.Dynamo.Encodable, except: [:secret]}
24+
defstruct [:email, :name, :age, :secret]
25+
end

0 commit comments

Comments
 (0)