Skip to content

Commit

Permalink
add enforce_keys option
Browse files Browse the repository at this point in the history
  • Loading branch information
artemeff committed Apr 22, 2020
1 parent c49be2a commit e07ec8e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Changelog

## v2.1.10

* Enhancements
* Add `enforce_keys` option to not enforce keys when building struct

## v2.1.9

* Enhancements
Expand Down
8 changes: 7 additions & 1 deletion lib/construct.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ defmodule Construct do

quote do
@behaviour Construct
@construct_opts unquote(opts)

unquote(pre_ast)

Expand Down Expand Up @@ -275,7 +276,12 @@ defmodule Construct do
|> Enum.reverse()

quote do
@enforce_keys unquote(enforce_fields)
enforce_keys = Keyword.get(@construct_opts, :enforce_keys, true)

if enforce_keys do
@enforce_keys unquote(enforce_fields)
end

defstruct unquote(Macro.escape(fields))
end
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Construct.Mixfile do
def project do
[
app: :construct,
version: "2.1.9",
version: "2.1.10",
elixir: "~> 1.5",
deps: deps(),
elixirc_paths: elixirc_paths(Mix.env),
Expand Down
10 changes: 10 additions & 0 deletions test/integration/struct_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ defmodule Construct.Integration.StructTest do
end
end

defmodule Test5 do
use Construct, enforce_keys: false

structure do
field :a, :string
end
end

test "struct should be equal with make" do
assert struct!(Test0) == Test0.make!()
end
Expand Down Expand Up @@ -96,6 +104,8 @@ defmodule Construct.Integration.StructTest do
end)

assert {:ok, %Test4{a: %Time{}}} = Test4.make()

assert %Test5{a: nil} = struct!(Test5)
end

test "cycle deps" do
Expand Down

0 comments on commit e07ec8e

Please sign in to comment.