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

feat: TypeCheck integration #34

Open
wants to merge 8 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
2 changes: 1 addition & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
inputs: ["{mix,.formatter}.exs", "{config,lib,test,dev}/**/*.{ex,exs}"]
]
10 changes: 3 additions & 7 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,16 @@ jobs:
elixir-version:
- 1.11.4
- 1.12.3
- 1.13.3
- 1.13.4
otp-version:
- 22.3
- 23.3
- 24.0
- 24.3
include:
- elixir-version: 1.9.4
otp-version: 21.3
- elixir-version: 1.9.4
otp-version: 22.3
- elixir-version: 1.10.4
otp-version: 22.3
- elixir-version: 1.10.4
otp-version: 24.0
otp-version: 23.3
env:
MIX_ENV: test
steps:
Expand Down
18 changes: 18 additions & 0 deletions dev/mix/tasks/type_check.gen.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule Mix.Tasks.TypeCheck.Gen do
@moduledoc """
Automatically generates TypeCheck override modules from a particular
dependency and puts them in the appropriate place
"""

@shortdoc "Automatically generates TypeCheck override modules"
@requirements ["app.start"]

use Mix.Task

@impl Mix.Task
def run(_args) do
TypedEctoSchema.TypeCheckGen.generate()
IO.puts("Formatting...")
Mix.Task.run("format")
end
end
213 changes: 213 additions & 0 deletions dev/typed_ecto_schema/type_check_gen.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
defmodule TypedEctoSchema.TypeCheckGen do
def generate(prefix \\ TypedEctoSchema.Overrides, typecheck_module \\ TypedEctoSchema.TypeCheck) do
modules_to_override =
for app <- [:ecto, :decimal],
{:ok, modules} = :application.get_key(app, :modules),
module <- modules,
Code.ensure_loaded?(module),
def_types?(module),
do: {module, Module.concat(prefix, module)}

for {source, target} <- modules_to_override do
source
|> generate_override_module(target, typecheck_module)
|> write_file(target)
end

modules_to_override
|> generate_typecheck_module(typecheck_module)
|> write_file(typecheck_module)
end

def generate_typecheck_module(modules_to_override, typecheck_module) do
overrides =
modules_to_override
|> Enum.flat_map(fn {source, target} -> overrides_for(source, target) end)
|> Enum.sort()

code =
quote do
if Code.ensure_loaded?(TypeCheck) do
defmodule unquote(typecheck_module) do
@moduledoc :REPLACE_WITH_DOCS

@overrides unquote(Macro.escape(overrides))

defmacro __using__(opts) do
quote do
use TypeCheck, unquote(opts) ++ [overrides: unquote(Macro.escape(@overrides))]
end
end

def overrides do
@overrides
end
end
end
end
|> Macro.to_string()

new_doc = ~S[
"""
If you use TypeCheck, you can use this module to enable our default overrides.

You can either use this module directly

defmodule MySchema do
use TypedEctoSchema.TypeCheck
use TypedEctoSchema

typed_schema "source" do
field(:int, :integer)
end
end

Or you can use `TypedEctoSchema.TypeCheck.overrides/0` instead

defmodule MySchema do
use TypeCheck, overrides: TypedEctoSchema.TypeCheck.overrides()
use TypedEctoSchema

typed_schema "source" do
field(:int, :integer)
end
end

This is useful if you also have your own overrides you want to mix in.

Consider also creating your own `MyApp.TypeCheck` to simplify using it.
"""
]

String.replace(code, ":REPLACE_WITH_DOCS", "#{String.trim(new_doc)}\n")
end

defp overrides_for(source, target) do
{:ok, types} = Code.Typespec.fetch_types(source)

for {type, {name, _, args}} <- types, type in [:type, :opaque] do
{{source, name, length(args)}, {target, name, length(args)}}
end
end

def generate_override_module(source, target, typecheck_module) do
{:ok, types} = Code.Typespec.fetch_types(source)

to_lazify = fetch_lazy_types(types)

overrides =
for {type_type, {name, type_def, args}} <- types do
type_def = lazify_user_types(type_def, to_lazify)
type_code = Code.Typespec.type_to_quoted({name, type_def, args})

{:@, [context: Elixir], [{:"#{type_type}!", [context: Elixir], [type_code]}]}
end

quote do
if Code.ensure_loaded?(TypeCheck) do
defmodule unquote(target) do
@moduledoc false

use unquote(typecheck_module)

unquote_splicing(overrides)
end
end
end
end

defp fetch_lazy_types(types) do
graph = :digraph.new()

ids =
for {_, {name, type, args}} <- types do
id = {name, length(args)}
:digraph.add_vertex(graph, id)
refs = [] |> find_references(type) |> Enum.uniq()

for ref <- refs do
:digraph.add_vertex(graph, ref)
:digraph.add_edge(graph, id, ref)
end

id
end

# Heuristic: make the most connected nodes lazy first
ids = Enum.sort_by(ids, &{elem(&1, 1), -length(:digraph.edges(graph, &1))})

result =
for id <- ids, reduce: MapSet.new() do
acc ->
if :digraph.get_cycle(graph, {:t, 0}) do
:digraph.del_vertex(graph, id)
MapSet.put(acc, id)
else
acc
end
end

:digraph.delete(graph)

result
end

defp def_types?(module) do
try do
match?({:ok, [_ | _]}, Code.Typespec.fetch_types(module))
rescue
_ -> false
end
end

def find_references(refs, {type, _line, name, args}) do
refs =
case type do
:user_type -> [{name, length(args)} | refs]
_ -> refs
end

find_arg_references(refs, args)
end

def find_references(refs, _), do: refs

defp find_arg_references(refs, []), do: refs

defp find_arg_references(refs, args) when is_list(args) do
Enum.reduce(args, refs, &find_references(&2, &1))
end

defp find_arg_references(refs, _args), do: refs

defp lazify_user_types({:user_type, line, name, args}, to_lazify) do
if MapSet.member?(to_lazify, {name, length(args)}) do
{:user_type, line, :lazy, [{:user_type, line, name, lazify_args(args, to_lazify)}]}
else
{:user_type, line, name, lazify_args(args, to_lazify)}
end
end

defp lazify_user_types({type, line, name, args}, to_lazify) do
{type, line, name, lazify_args(args, to_lazify)}
end

defp lazify_user_types(other, _to_lazify), do: other

defp lazify_args(args, to_lazify) when is_list(args) do
Enum.map(args, &lazify_user_types(&1, to_lazify))
end

defp lazify_args(args, _to_lazify), do: args

defp write_file(code, module) when is_binary(code) do
path = Path.join([File.cwd!(), "lib", "#{Macro.underscore(module)}.ex"])

File.mkdir_p!(Path.dirname(path))
File.write!(path, code)
end

defp write_file(code, module) do
write_file(Macro.to_string(code), module)
end
end
26 changes: 26 additions & 0 deletions lib/typed_ecto_schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,32 @@ defmodule TypedEctoSchema do
defines a default (`default: value`), since it makes no sense to have a default value for an
enforced field.
- `:opaque` - When `true` makes the generated type `t` be an opaque type.
- `:type_check` - Enable [experimental integration with
TypeCheck](#module-typecheck-integration-experimental)

## TypeCheck Integration (Experimental)

We are currently experimenting with a [TypeCheck](https://hexdocs.pm/type_check/) integration,
but because it might break, we are making it opt-in. You can either specify on the
[schema options](#module-schema-options) or globally using config:

config :typed_ecto_schema, type_check: true

What this integration enables is that by doing

defmodule InteropWithTypeCheck do
use TypedEctoSchema
use TypeCheck

typed_embedded_schema type_check: true do
field(:year, :number)
end
end

You then should be able to

iex> InteropWithTypeCheck.t()
#TypeCheck.Type< InteropWithTypeCheck.t() :: %InteropWithTypeCheck{id: binary() | nil, year: integer() | nil} >

## Type Inference

Expand Down
13 changes: 13 additions & 0 deletions lib/typed_ecto_schema/overrides/decimal.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
if Code.ensure_loaded?(TypeCheck) do
defmodule TypedEctoSchema.Overrides.Decimal do
@moduledoc false
use TypedEctoSchema.TypeCheck
@type! decimal() :: t() | integer() | String.t()
@type! t() :: %Decimal{coef: coefficient(), exp: exponent(), sign: sign()}
@type! rounding() :: :down | :half_up | :half_even | :ceiling | :floor | :half_down | :up
@type! signal() :: :invalid_operation | :division_by_zero | :rounded | :inexact
@type! sign() :: 1 | -1
@type! exponent() :: integer()
@type! coefficient() :: non_neg_integer() | :NaN | :inf
end
end
13 changes: 13 additions & 0 deletions lib/typed_ecto_schema/overrides/decimal/context.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
if Code.ensure_loaded?(TypeCheck) do
defmodule TypedEctoSchema.Overrides.Decimal.Context do
@moduledoc false
use TypedEctoSchema.TypeCheck

@type! t() :: %Decimal.Context{
flags: [Decimal.signal()],
precision: pos_integer(),
rounding: Decimal.rounding(),
traps: [Decimal.signal()]
}
end
end
8 changes: 8 additions & 0 deletions lib/typed_ecto_schema/overrides/ecto/adapter.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
if Code.ensure_loaded?(TypeCheck) do
defmodule TypedEctoSchema.Overrides.Ecto.Adapter do
@moduledoc false
use TypedEctoSchema.TypeCheck
@type! adapter_meta() :: map()
@type! t() :: module()
end
end
17 changes: 17 additions & 0 deletions lib/typed_ecto_schema/overrides/ecto/adapter/queryable.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
if Code.ensure_loaded?(TypeCheck) do
defmodule TypedEctoSchema.Overrides.Ecto.Adapter.Queryable do
@moduledoc false
use TypedEctoSchema.TypeCheck
@type! selected() :: term()
@type! options() :: Keyword.t()
@type! cached() :: term()
@type! prepared() :: term()
@type! query_cache() ::
{:nocache, prepared()}
| {:cache, cache_function :: (cached() -> :ok), prepared()}
| {:cached, update_function :: (cached() -> :ok),
reset_function :: (prepared() -> :ok), cached()}
@type! query_meta() :: %{sources: tuple(), preloads: term(), select: map()}
@type! adapter_meta() :: Ecto.Adapter.adapter_meta()
end
end
26 changes: 26 additions & 0 deletions lib/typed_ecto_schema/overrides/ecto/adapter/schema.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
if Code.ensure_loaded?(TypeCheck) do
defmodule TypedEctoSchema.Overrides.Ecto.Adapter.Schema do
@moduledoc false
use TypedEctoSchema.TypeCheck

@type! on_conflict() ::
{:raise, list(), []}
| {:nothing, list(), [atom()]}
| {[atom()], list(), [atom()]}
| {Ecto.Query.t(), list(), [atom()]}
@type! options() :: Keyword.t()
@type! placeholders() :: [term()]
@type! returning() :: [atom()]
@type! constraints() :: Keyword.t()
@type! filters() :: Keyword.t()
@type! fields() :: Keyword.t()
@type! schema_meta() :: %{
autogenerate_id: {schema_field :: atom(), source_field :: atom(), Ecto.Type.t()},
context: term(),
prefix: binary() | nil,
schema: atom(),
source: binary()
}
@type! adapter_meta() :: Ecto.Adapter.adapter_meta()
end
end
7 changes: 7 additions & 0 deletions lib/typed_ecto_schema/overrides/ecto/adapter/transaction.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
if Code.ensure_loaded?(TypeCheck) do
defmodule TypedEctoSchema.Overrides.Ecto.Adapter.Transaction do
@moduledoc false
use TypedEctoSchema.TypeCheck
@type! adapter_meta() :: Ecto.Adapter.adapter_meta()
end
end
Loading