Skip to content

Commit 075072a

Browse files
authored
fix: don't set a false value param to nil on new/1 (#4)
* fix: don't set a `false`-value param to `nil` on `new/1` * chore: bump version to 0.4.1
1 parent 8cddff3 commit 075072a

6 files changed

Lines changed: 71 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## v0.4.1
4+
5+
- Set `false` parameter correctly when given a Map of params. Was previously
6+
evaluating to `nil`.
7+
38
## v0.4.0
49

510
- Default typespecs and documentation for modules using Commandex.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Add commandex as a `mix.exs` dependency:
1616
```elixir
1717
def deps do
1818
[
19-
{:commandex, "~> 0.4.0"}
19+
{:commandex, "~> 0.4.1"}
2020
]
2121
end
2222
```

lib/commandex.ex

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ defmodule Commandex do
363363
end
364364

365365
def parse_params(%{params: p} = struct, %{} = params) do
366-
params = for {key, _} <- p, into: %{}, do: {key, get_param(params, key) || p[key]}
366+
params = for {key, _} <- p, into: %{}, do: {key, get_param(params, key, p[key])}
367367
%{struct | params: params}
368368
end
369369

@@ -413,10 +413,13 @@ defmodule Commandex do
413413
Module.put_attribute(mod, :pipelines, name)
414414
end
415415

416-
defp get_param(params, key) do
416+
defp get_param(params, key, default) do
417417
case Map.get(params, key) do
418-
nil -> Map.get(params, to_string(key))
419-
val -> val
418+
nil ->
419+
Map.get(params, to_string(key), default)
420+
421+
val ->
422+
val
420423
end
421424
end
422425
end

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Commandex.MixProject do
22
use Mix.Project
33

4-
@version "0.4.0"
4+
@version "0.4.1"
55

66
def project do
77
[

test/commandex_test.exs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ defmodule CommandexTest do
33
doctest Commandex
44
alias Commandex.RegisterUser
55

6+
@email "example@example.com"
7+
@password "test1234"
8+
@agree_tos false
9+
610
describe "struct assembly" do
711
test "sets :params map" do
8-
for key <- [:email, :password] do
12+
for key <- [:email, :password, :agree_tos] do
913
assert Map.has_key?(%RegisterUser{}.params, key)
1014
end
1115
end
@@ -19,5 +23,45 @@ defmodule CommandexTest do
1923
assert Map.has_key?(%RegisterUser{}.data, key)
2024
end
2125
end
26+
27+
test "handles atom-key map params correctly" do
28+
params = %{
29+
email: @email,
30+
password: @password,
31+
agree_tos: @agree_tos
32+
}
33+
34+
command = RegisterUser.new(params)
35+
assert_params(command)
36+
end
37+
38+
test "handles string-key map params correctly" do
39+
params = %{
40+
email: @email,
41+
password: @password,
42+
agree_tos: @agree_tos
43+
}
44+
45+
command = RegisterUser.new(params)
46+
assert_params(command)
47+
end
48+
49+
test "handles keyword list params correctly" do
50+
params = [
51+
email: @email,
52+
password: @password,
53+
agree_tos: @agree_tos
54+
]
55+
56+
command = RegisterUser.new(params)
57+
assert_params(command)
58+
end
59+
end
60+
61+
defp assert_params(command) do
62+
assert command.params.email == @email
63+
assert command.params.password == @password
64+
# Don't use refute here because nil fails the test.
65+
assert command.params.agree_tos == @agree_tos
2266
end
2367
end

test/support/register_user.ex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,27 @@ defmodule Commandex.RegisterUser do
88
command do
99
param :email, default: "test@test.com"
1010
param :password
11+
param :agree_tos
1112

1213
data :user
1314
data :auth
1415

16+
pipeline :verify_tos
1517
pipeline :create_user
1618
pipeline :record_auth_attempt
1719
pipeline &IO.inspect/1
1820
end
1921

22+
def verify_tos(command, %{agree_tos: true} = _params, _data) do
23+
command
24+
end
25+
26+
def verify_tos(command, %{agree_tos: false} = _params, _data) do
27+
command
28+
|> put_error(:tos, :not_accepted)
29+
|> halt()
30+
end
31+
2032
def create_user(command, %{password: nil} = _params, _data) do
2133
command
2234
|> put_error(:password, :not_given)

0 commit comments

Comments
 (0)