Skip to content

Commit 4bf667f

Browse files
authored
Merge pull request #46 from mixi-m/master
Suppress warnings in Elixir 1.17 and 1.18 + improvements
2 parents 879d9ce + 1c1c272 commit 4bf667f

File tree

10 files changed

+315
-209
lines changed

10 files changed

+315
-209
lines changed

.github/workflows/ci.yml

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: CI
2+
on: [push, pull_request]
3+
4+
jobs:
5+
build:
6+
name: Build and test
7+
runs-on: ubuntu-20.04
8+
env:
9+
MIX_ENV: test
10+
# see https://hexdocs.pm/elixir/compatibility-and-deprecations.html#between-elixir-and-erlang-otp
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
include:
15+
- elixir: 1.18.x
16+
otp: 27.x
17+
lint: true
18+
- elixir: 1.17.x
19+
otp: 27.x
20+
- elixir: 1.17.x
21+
otp: 25.x
22+
- elixir: 1.16.x
23+
otp: 26.x
24+
- elixir: 1.16.x
25+
otp: 24.x
26+
- elixir: 1.15.x
27+
otp: 26.x
28+
- elixir: 1.15.x
29+
otp: 24.x
30+
- elixir: 1.14.x
31+
otp: 25.x
32+
- elixir: 1.14.x
33+
otp: 23.x
34+
- elixir: 1.13.x
35+
otp: 24.x
36+
- elixir: 1.13.x
37+
otp: 22.x
38+
- elixir: 1.12.x
39+
otp: 24.x
40+
- elixir: 1.12.x
41+
otp: 22.x
42+
- elixir: 1.11.x
43+
otp: 23.x
44+
- elixir: 1.11.x
45+
otp: 21.x
46+
steps:
47+
- uses: actions/checkout@v4
48+
- uses: erlef/setup-beam@v1
49+
with:
50+
elixir-version: ${{ matrix.elixir }}
51+
otp-version: ${{ matrix.otp }}
52+
- run: mix deps.get
53+
- run: mix deps.compile
54+
- run: mix compile --warnings-as-errors
55+
if: ${{ matrix.lint }}
56+
- run: mix format --check-formatted
57+
if: ${{ matrix.lint }}
58+
- run: mix deps.unlock --check-unused
59+
if: ${{ matrix.lint }}
60+
- run: mix test

.travis.yml

-12
This file was deleted.

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
[Looking for maintainer](https://github.com/vic/params/issues/new?title=Becoming%20a%20maintainer)
44

5-
[![Build Status](https://travis-ci.org/vic/params.svg?branch=master)](https://travis-ci.org/vic/params)
65
[![Hex Version](https://img.shields.io/hexpm/v/params.svg)](https://hex.pm/packages/params)
76
[![Hex Docs](https://img.shields.io/badge/hex-docs-lightgreen.svg)](https://hexdocs.pm/params/)
87
[![Total Download](https://img.shields.io/hexpm/dt/params.svg)](https://hex.pm/packages/params)

config/config.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This file is responsible for configuring your application
22
# and its dependencies with the aid of the Mix.Config module.
3-
use Mix.Config
3+
import Config
44

55
# This configuration is loaded before any dependency and is restricted
66
# to this project. If another project depends on this project, this

lib/params.ex

+31-21
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ defmodule Params do
4141
Recursively traverses and transforms embedded changesets and skips keys that
4242
was not part of params given to changeset
4343
"""
44-
@spec to_map(Changeset.t) :: map
44+
@spec to_map(Changeset.t()) :: map
4545
def to_map(%Changeset{data: %{__struct__: module}} = ch) do
4646
ecto_defaults = module |> plain_defaults_defined_by_ecto_schema
4747
params_defaults = module |> schema |> defaults
@@ -72,13 +72,14 @@ defmodule Params do
7272
data.login # => "foo"
7373
```
7474
"""
75-
@spec data(Changeset.t) :: struct
75+
@spec data(Changeset.t()) :: struct
7676
def data(%Changeset{data: data = %{__struct__: module}} = ch) do
7777
default_embeds = default_embeds_from_schema(module)
7878

79-
default = Enum.reduce(default_embeds, data, fn {k, v}, m ->
80-
Map.put(m, k, Map.get(m, k) || v)
81-
end)
79+
default =
80+
Enum.reduce(default_embeds, data, fn {k, v}, m ->
81+
Map.put(m, k, Map.get(m, k) || v)
82+
end)
8283

8384
Enum.reduce(ch.changes, default, fn {k, v}, m ->
8485
case v do
@@ -103,12 +104,14 @@ defmodule Params do
103104
end
104105

105106
case schema(module) do
106-
nil -> %{}
107+
nil ->
108+
%{}
109+
107110
schema ->
108111
schema
109112
|> Stream.filter(is_embed_default)
110113
|> Stream.map(default_embed)
111-
|> Enum.into(struct(module) |> Map.from_struct)
114+
|> Enum.into(struct(module) |> Map.from_struct())
112115
end
113116
end
114117

@@ -143,7 +146,7 @@ defmodule Params do
143146
changeset
144147
|> Changeset.cast(params, required ++ optional)
145148
|> Changeset.validate_required(required)
146-
|> cast_relations(required_relations, [required: true])
149+
|> cast_relations(required_relations, required: true)
147150
|> cast_relations(optional_relations, [])
148151
end
149152

@@ -158,22 +161,23 @@ defmodule Params do
158161
end
159162

160163
defp change(%{__struct__: _} = model) do
161-
model |> Changeset.change
164+
model |> Changeset.change()
162165
end
163166

164167
defp change(module) when is_atom(module) do
165-
module |> struct |> Changeset.change
168+
module |> struct |> Changeset.change()
166169
end
167170

168171
defp relation_partition(module, names) do
169-
types = module.__changeset__
172+
types = module.__changeset__()
170173

171174
names
172175
|> Stream.map(fn x -> String.to_atom("#{x}") end)
173176
|> Enum.reduce({[], []}, fn name, {fields, relations} ->
174177
case Map.get(types, name) do
175178
{type, _} when type in @relations ->
176179
{fields, [{name, type} | relations]}
180+
177181
_ ->
178182
{[name | fields], relations}
179183
end
@@ -194,33 +198,39 @@ defmodule Params do
194198
defp deep_merge_conflict(_k, %{} = m1, %{} = m2) do
195199
deep_merge(m1, m2)
196200
end
201+
197202
defp deep_merge_conflict(_k, _v1, v2), do: v2
198203

199204
defp defaults(params), do: defaults(params, %{}, [])
200205
defp defaults(params, acc, path)
201206
defp defaults([], acc, _path), do: acc
202207
defp defaults(nil, _acc, _path), do: %{}
208+
203209
defp defaults([opts | rest], acc, path) when is_list(opts) do
204210
defaults([Enum.into(opts, %{}) | rest], acc, path)
205211
end
212+
206213
defp defaults([%{name: name, embeds: embeds} | rest], acc, path) do
207214
acc = defaults(embeds, acc, [name | path])
208215
defaults(rest, acc, path)
209216
end
217+
210218
defp defaults([%{name: name, default: value} | rest], acc, path) do
211-
funs = [name | path]
212-
|> Enum.reverse
213-
|> Enum.map(fn nested_name ->
214-
fn :get_and_update, data, next ->
215-
with {nil, inner_data} <- next.(data[nested_name] || %{}),
216-
data = Map.put(data, nested_name, inner_data),
217-
do: {nil, data}
218-
end
219-
end)
219+
funs =
220+
[name | path]
221+
|> Enum.reverse()
222+
|> Enum.map(fn nested_name ->
223+
fn :get_and_update, data, next ->
224+
with {nil, inner_data} <- next.(data[nested_name] || %{}),
225+
data = Map.put(data, nested_name, inner_data),
226+
do: {nil, data}
227+
end
228+
end)
220229

221230
acc = put_in(acc, funs, value)
222231
defaults(rest, acc, path)
223232
end
233+
224234
defp defaults([%{} | rest], acc, path) do
225235
defaults(rest, acc, path)
226236
end
@@ -238,7 +248,7 @@ defmodule Params do
238248
defp plain_defaults_defined_by_ecto_schema(module) do
239249
module
240250
|> struct
241-
|> Map.from_struct
251+
|> Map.from_struct()
242252
|> Map.delete(:__meta__)
243253
|> Enum.reject(fn {_, v} -> is_nil(v) end)
244254
|> Enum.into(%{})

lib/params/behaviour.ex

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
defmodule Params.Behaviour do
22
@moduledoc false
33

4-
@callback from(map, Keyword.t) :: Ecto.Changeset.t
5-
@callback data(map, Keyword.t) :: {:ok, struct} | {:error, Ecto.Changeset.t}
6-
@callback changeset(Ecto.Changeset.t, map) :: Ecto.Changeset.t
7-
4+
@callback from(map, Keyword.t()) :: Ecto.Changeset.t()
5+
@callback data(map, Keyword.t()) :: {:ok, struct} | {:error, Ecto.Changeset.t()}
6+
@callback changeset(Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
87
end

0 commit comments

Comments
 (0)