Skip to content

Commit f72974a

Browse files
authored
Merge pull request #5 from danschultzer/formatting
Enforce elixir formatting
2 parents c37d331 + fbe37c7 commit f72974a

12 files changed

+312
-236
lines changed

.github/workflows/ci.yml

+16-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@ on:
77
workflow_call:
88

99
jobs:
10+
lint:
11+
runs-on: ubuntu-latest
12+
name: Linter
13+
env:
14+
MIX_ENV: test
15+
steps:
16+
- uses: actions/checkout@v3
17+
- uses: erlef/setup-beam@v1
18+
with:
19+
otp-version: 26.0
20+
elixir-version: 1.15
21+
- run: mix deps.get
22+
- run: mix compile --warnings-as-errors
23+
- run: mix credo --strict --ignore design.alias
24+
- run: mix format --check-formatted
25+
1026
test:
1127
services:
1228
postgres:
@@ -40,5 +56,4 @@ jobs:
4056
elixir-version: ${{matrix.version.elixir}}
4157
- run: mix deps.get mix compile --warnings-as-errors
4258
- run: mix test
43-
- run: mix credo
4459

lib/idempotency_plug.ex

+43-31
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,28 @@ defmodule IdempotencyPlug do
44
There's no Idempotency-Key request headers.
55
"""
66

7-
defexception [
8-
message: "No idempotency key found. You need to set the `Idempotency-Key` header for all POST requests: 'Idempotency-Key: KEY'",
9-
plug_status: :bad_request
10-
]
7+
defexception message:
8+
"No idempotency key found. You need to set the `Idempotency-Key` header for all POST requests: 'Idempotency-Key: KEY'",
9+
plug_status: :bad_request
1110
end
1211

1312
defmodule MultipleHeadersError do
1413
@moduledoc """
1514
There are multiple Idempotency-Key request headers.
1615
"""
1716

18-
defexception [
19-
message: "Only one `Idempotency-Key` header can be sent",
20-
plug_status: :bad_request
21-
]
17+
defexception message: "Only one `Idempotency-Key` header can be sent",
18+
plug_status: :bad_request
2219
end
2320

2421
defmodule ConcurrentRequestError do
2522
@moduledoc """
2623
There's another request currently being processed for this ID.
2724
"""
2825

29-
defexception [
30-
message: "A request with the same `Idempotency-Key` is currently being processed",
31-
plug_status: :conflict
32-
]
26+
defexception message:
27+
"A request with the same `Idempotency-Key` is currently being processed",
28+
plug_status: :conflict
3329
end
3430

3531
defmodule RequestPayloadFingerprintMismatchError do
@@ -51,18 +47,20 @@ defmodule IdempotencyPlug do
5147

5248
defexception [
5349
:reason,
54-
message: "The original request was interrupted and can't be recovered as it's in an unknown state",
50+
message:
51+
"The original request was interrupted and can't be recovered as it's in an unknown state",
5552
plug_status: :internal_server_error
5653
]
5754
end
5855

59-
defimpl Plug.Exception, for: [
60-
NoHeadersError,
61-
MultipleHeadersError,
62-
ConcurrentRequestError,
63-
RequestPayloadFingerprintMismatchError,
64-
HaltedResponseError
65-
] do
56+
defimpl Plug.Exception,
57+
for: [
58+
NoHeadersError,
59+
MultipleHeadersError,
60+
ConcurrentRequestError,
61+
RequestPayloadFingerprintMismatchError,
62+
HaltedResponseError
63+
] do
6664
def status(%{plug_status: status}), do: Plug.Conn.Status.code(status)
6765
def actions(_), do: []
6866
end
@@ -173,7 +171,7 @@ defmodule IdempotencyPlug do
173171

174172
other ->
175173
raise ArgumentError,
176-
"option :tracker must be one of PID or Atom, got: #{inspect(other)}"
174+
"option :tracker must be one of PID or Atom, got: #{inspect(other)}"
177175
end
178176

179177
opts
@@ -208,7 +206,7 @@ defmodule IdempotencyPlug do
208206
other ->
209207
# credo:disable-for-next-line Credo.Check.Warning.RaiseInsideRescue
210208
raise ArgumentError,
211-
"option :with should be one of :exception or MFA, got: #{inspect(other)}"
209+
"option :with should be one of :exception or MFA, got: #{inspect(other)}"
212210
end
213211
end
214212

@@ -254,9 +252,14 @@ defmodule IdempotencyPlug do
254252

255253
processed_key =
256254
case Keyword.get(opts, :idempotency_key, {__MODULE__, :idempotency_key}) do
257-
{mod, fun} -> apply(mod, fun, [conn, key])
258-
{mod, fun, args} -> apply(mod, fun, [conn, key | args])
259-
other -> raise ArgumentError, "option :idempotency_key must be a MFA, got: #{inspect(other)}"
255+
{mod, fun} ->
256+
apply(mod, fun, [conn, key])
257+
258+
{mod, fun, args} ->
259+
apply(mod, fun, [conn, key | args])
260+
261+
other ->
262+
raise ArgumentError, "option :idempotency_key must be a MFA, got: #{inspect(other)}"
260263
end
261264

262265
hash(:idempotency_key, processed_key, opts)
@@ -275,9 +278,15 @@ defmodule IdempotencyPlug do
275278
defp hash_request_payload(conn, opts) do
276279
payload =
277280
case Keyword.get(opts, :request_payload, {__MODULE__, :request_payload}) do
278-
{mod, fun} -> apply(mod, fun, [conn])
279-
{mod, fun, args} -> apply(mod, fun, [conn | args])
280-
other -> raise ArgumentError, "option :request_payload must be a MFA tuple, got: #{inspect(other)}"
281+
{mod, fun} ->
282+
apply(mod, fun, [conn])
283+
284+
{mod, fun, args} ->
285+
apply(mod, fun, [conn | args])
286+
287+
other ->
288+
raise ArgumentError,
289+
"option :request_payload must be a MFA tuple, got: #{inspect(other)}"
281290
end
282291

283292
hash(:request_payload, payload, opts)
@@ -308,7 +317,7 @@ defmodule IdempotencyPlug do
308317
Conn.register_before_send(conn, fn conn ->
309318
case RequestTracker.put_response(tracker, key, conn_to_response(conn)) do
310319
{:ok, expires} -> put_expires_header(conn, expires)
311-
{:error, error} -> raise "failed to put response in cache store, got: #{inspect error}"
320+
{:error, error} -> raise "failed to put response in cache store, got: #{inspect(error)}"
312321
end
313322
end)
314323
end
@@ -338,8 +347,11 @@ defmodule IdempotencyPlug do
338347
mod
339348
|> apply(fun, [conn, error | args])
340349
|> case do
341-
%Conn{halted: true} = conn -> conn
342-
other -> raise ArgumentError, "option :with MUST return a halted conn, got: #{inspect(other)}"
350+
%Conn{halted: true} = conn ->
351+
conn
352+
353+
other ->
354+
raise ArgumentError, "option :with MUST return a halted conn, got: #{inspect(other)}"
343355
end
344356
end
345357
end

lib/idempotency_plug/request_tracker.ex

+8-8
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ defmodule IdempotencyPlug.RequestTracker do
8787
fingerprint differs from what was stored, an error is returned.
8888
"""
8989
@spec track(atom() | pid(), binary(), binary()) ::
90-
{:error, term()} |
91-
{:init, binary(), DateTime.t()} |
92-
{:mismatch, {:fingerprint, binary()}, DateTime.t()} |
93-
{:processing, {atom(), pid()}, DateTime.t()} |
94-
{:cache, {:ok, any()}, DateTime.t()} |
95-
{:cache, {:halted, term()}, DateTime.t()}
90+
{:error, term()}
91+
| {:init, binary(), DateTime.t()}
92+
| {:mismatch, {:fingerprint, binary()}, DateTime.t()}
93+
| {:processing, {atom(), pid()}, DateTime.t()}
94+
| {:cache, {:ok, any()}, DateTime.t()}
95+
| {:cache, {:halted, term()}, DateTime.t()}
9696
def track(name_or_pid, request_id, fingerprint) do
9797
GenServer.call(name_or_pid, {:track, request_id, fingerprint})
9898
end
@@ -162,7 +162,7 @@ defmodule IdempotencyPlug.RequestTracker do
162162

163163
def handle_call({:put_response, request_id, response}, _from, state) do
164164
{store, store_opts} = fetch_store(state.options)
165-
{_finished, state} = pop_monitored(state, &elem(&1, 0) == request_id)
165+
{_finished, state} = pop_monitored(state, &(elem(&1, 0) == request_id))
166166
data = {:ok, response}
167167
expires_at = expires_at(state.options)
168168

@@ -189,7 +189,7 @@ defmodule IdempotencyPlug.RequestTracker do
189189
@impl true
190190
def handle_info({:DOWN, _ref, :process, pid, reason}, state) do
191191
{store, store_opts} = fetch_store(state.options)
192-
{finished, state} = pop_monitored(state, &elem(&1, 1) == pid)
192+
{finished, state} = pop_monitored(state, &(elem(&1, 1) == pid))
193193
data = {:halted, reason}
194194
expires_at = expires_at(state.options)
195195

lib/idempotency_plug/store.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ defmodule IdempotencyPlug.Store do
3131

3232
@callback setup(options()) :: :ok | {:error, term()}
3333
@callback lookup(request_id(), options()) :: {data(), fingerprint(), expires_at()} | :not_found
34-
@callback insert(request_id(), data(), fingerprint(), expires_at(), options()) :: :ok | {:error, term()}
34+
@callback insert(request_id(), data(), fingerprint(), expires_at(), options()) ::
35+
:ok | {:error, term()}
3536
@callback update(request_id(), data(), expires_at(), options()) :: :ok | {:error, term()}
3637
@callback prune(options()) :: :ok
3738
end

0 commit comments

Comments
 (0)