Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress warnings in Elixir 1.17 and 1.18 + improvements #46

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: CI
on: [push, pull_request]

jobs:
build:
name: Build and test
runs-on: ubuntu-20.04
env:
MIX_ENV: test
# see https://hexdocs.pm/elixir/compatibility-and-deprecations.html#between-elixir-and-erlang-otp
strategy:
fail-fast: false
matrix:
include:
- elixir: 1.18.x
otp: 27.x
lint: true
- elixir: 1.17.x
otp: 27.x
- elixir: 1.17.x
otp: 25.x
- elixir: 1.16.x
otp: 26.x
- elixir: 1.16.x
otp: 24.x
- elixir: 1.15.x
otp: 26.x
- elixir: 1.15.x
otp: 24.x
- elixir: 1.14.x
otp: 25.x
- elixir: 1.14.x
otp: 23.x
- elixir: 1.13.x
otp: 24.x
- elixir: 1.13.x
otp: 22.x
- elixir: 1.12.x
otp: 24.x
- elixir: 1.12.x
otp: 22.x
- elixir: 1.11.x
otp: 23.x
- elixir: 1.11.x
otp: 21.x
steps:
- uses: actions/checkout@v4
- uses: erlef/setup-beam@v1
with:
elixir-version: ${{ matrix.elixir }}
otp-version: ${{ matrix.otp }}
- run: mix deps.get
- run: mix deps.compile
- run: mix compile --warnings-as-errors
if: ${{ matrix.lint }}
- run: mix format --check-formatted
if: ${{ matrix.lint }}
- run: mix deps.unlock --check-unused
if: ${{ matrix.lint }}
- run: mix test
12 changes: 0 additions & 12 deletions .travis.yml

This file was deleted.

1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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

[![Build Status](https://travis-ci.org/vic/params.svg?branch=master)](https://travis-ci.org/vic/params)
[![Hex Version](https://img.shields.io/hexpm/v/params.svg)](https://hex.pm/packages/params)
[![Hex Docs](https://img.shields.io/badge/hex-docs-lightgreen.svg)](https://hexdocs.pm/params/)
[![Total Download](https://img.shields.io/hexpm/dt/params.svg)](https://hex.pm/packages/params)
Expand Down
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config
import Config

# This configuration is loaded before any dependency and is restricted
# to this project. If another project depends on this project, this
Expand Down
52 changes: 31 additions & 21 deletions lib/params.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ defmodule Params do
Recursively traverses and transforms embedded changesets and skips keys that
was not part of params given to changeset
"""
@spec to_map(Changeset.t) :: map
@spec to_map(Changeset.t()) :: map
def to_map(%Changeset{data: %{__struct__: module}} = ch) do
ecto_defaults = module |> plain_defaults_defined_by_ecto_schema
params_defaults = module |> schema |> defaults
Expand Down Expand Up @@ -72,13 +72,14 @@ defmodule Params do
data.login # => "foo"
```
"""
@spec data(Changeset.t) :: struct
@spec data(Changeset.t()) :: struct
def data(%Changeset{data: data = %{__struct__: module}} = ch) do
default_embeds = default_embeds_from_schema(module)

default = Enum.reduce(default_embeds, data, fn {k, v}, m ->
Map.put(m, k, Map.get(m, k) || v)
end)
default =
Enum.reduce(default_embeds, data, fn {k, v}, m ->
Map.put(m, k, Map.get(m, k) || v)
end)

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

case schema(module) do
nil -> %{}
nil ->
%{}

schema ->
schema
|> Stream.filter(is_embed_default)
|> Stream.map(default_embed)
|> Enum.into(struct(module) |> Map.from_struct)
|> Enum.into(struct(module) |> Map.from_struct())
end
end

Expand Down Expand Up @@ -143,7 +146,7 @@ defmodule Params do
changeset
|> Changeset.cast(params, required ++ optional)
|> Changeset.validate_required(required)
|> cast_relations(required_relations, [required: true])
|> cast_relations(required_relations, required: true)
|> cast_relations(optional_relations, [])
end

Expand All @@ -158,22 +161,23 @@ defmodule Params do
end

defp change(%{__struct__: _} = model) do
model |> Changeset.change
model |> Changeset.change()
end

defp change(module) when is_atom(module) do
module |> struct |> Changeset.change
module |> struct |> Changeset.change()
end

defp relation_partition(module, names) do
types = module.__changeset__
types = module.__changeset__()

names
|> Stream.map(fn x -> String.to_atom("#{x}") end)
|> Enum.reduce({[], []}, fn name, {fields, relations} ->
case Map.get(types, name) do
{type, _} when type in @relations ->
{fields, [{name, type} | relations]}

_ ->
{[name | fields], relations}
end
Expand All @@ -194,33 +198,39 @@ defmodule Params do
defp deep_merge_conflict(_k, %{} = m1, %{} = m2) do
deep_merge(m1, m2)
end

defp deep_merge_conflict(_k, _v1, v2), do: v2

defp defaults(params), do: defaults(params, %{}, [])
defp defaults(params, acc, path)
defp defaults([], acc, _path), do: acc
defp defaults(nil, _acc, _path), do: %{}

defp defaults([opts | rest], acc, path) when is_list(opts) do
defaults([Enum.into(opts, %{}) | rest], acc, path)
end

defp defaults([%{name: name, embeds: embeds} | rest], acc, path) do
acc = defaults(embeds, acc, [name | path])
defaults(rest, acc, path)
end

defp defaults([%{name: name, default: value} | rest], acc, path) do
funs = [name | path]
|> Enum.reverse
|> Enum.map(fn nested_name ->
fn :get_and_update, data, next ->
with {nil, inner_data} <- next.(data[nested_name] || %{}),
data = Map.put(data, nested_name, inner_data),
do: {nil, data}
end
end)
funs =
[name | path]
|> Enum.reverse()
|> Enum.map(fn nested_name ->
fn :get_and_update, data, next ->
with {nil, inner_data} <- next.(data[nested_name] || %{}),
data = Map.put(data, nested_name, inner_data),
do: {nil, data}
end
end)

acc = put_in(acc, funs, value)
defaults(rest, acc, path)
end

defp defaults([%{} | rest], acc, path) do
defaults(rest, acc, path)
end
Expand All @@ -238,7 +248,7 @@ defmodule Params do
defp plain_defaults_defined_by_ecto_schema(module) do
module
|> struct
|> Map.from_struct
|> Map.from_struct()
|> Map.delete(:__meta__)
|> Enum.reject(fn {_, v} -> is_nil(v) end)
|> Enum.into(%{})
Expand Down
7 changes: 3 additions & 4 deletions lib/params/behaviour.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
defmodule Params.Behaviour do
@moduledoc false

@callback from(map, Keyword.t) :: Ecto.Changeset.t
@callback data(map, Keyword.t) :: {:ok, struct} | {:error, Ecto.Changeset.t}
@callback changeset(Ecto.Changeset.t, map) :: Ecto.Changeset.t

@callback from(map, Keyword.t()) :: Ecto.Changeset.t()
@callback data(map, Keyword.t()) :: {:ok, struct} | {:error, Ecto.Changeset.t()}
@callback changeset(Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
end
Loading