/
tyyppi.ex
416 lines (350 loc) · 12 KB
/
tyyppi.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
defmodule Tyyppi do
@moduledoc """
The main interface to `Tyyppi` library. Usually, functions and macros
presented is this module are enough to work with `Tyyppi`.
"""
use Boundary, exports: [Function, Matchers, Stats]
require Logger
alias Tyyppi.{Matchers, Stats, T}
import Tyyppi.T, only: [normalize_params: 1, param_names: 1, parse_definition: 1]
@doc false
defguard is_params(params) when is_list(params) or is_atom(params)
@doc """
Sigil to simplify specification of `Tyyppi.T.t(term())` type, it’s literally the wrapper for `Tyyppi.parse/1`.
## Examples
iex> import Tyyppi
iex> ~t[integer()]
%Tyyppi.T{
definition: {:type, 0, :integer, []},
module: nil,
name: nil,
params: [],
quoted: {:integer, [], []},
source: nil,
type: :built_in
}
...> ~t[atom]
%Tyyppi.T{
definition: {:type, 0, :atom, []},
module: nil,
name: nil,
params: [],
quoted: {:atom, [], []},
source: nil,
type: :built_in
}
"""
defmacro sigil_t({:<<>>, _meta, [string]}, []) when is_binary(string) do
if Version.compare(System.version(), "1.12.0") == :lt do
quote bind_quoted: [string: string] do
string
|> :elixir_interpolation.unescape_chars()
|> Code.string_to_quoted!()
|> Tyyppi.parse_quoted()
end
else
quote bind_quoted: [string: string] do
string
|> :elixir_interpolation.unescape_string()
|> Code.string_to_quoted!()
|> Tyyppi.parse_quoted()
end
end
end
defmacro sigil_t({:<<>>, meta, pieces}, []) do
tokens =
case :elixir_interpolation.unescape_tokens(pieces) do
{:ok, unescaped_tokens} -> unescaped_tokens
{:error, reason} -> raise ArgumentError, to_string(reason)
{:error, reason, _} -> raise ArgumentError, to_string(reason)
end
quote do
unquote({:<<>>, meta, tokens})
|> Code.string_to_quoted!()
|> Tyyppi.parse_quoted()
end
end
@doc """
Parses the type as by spec and returns its `Tyyppi.T` representation.
_Example:_
iex> require Tyyppi
...> parsed = Tyyppi.parse(GenServer.on_start())
...> with %Tyyppi.T{definition: {:type, _, :union, [type | _]}} <- parsed, do: type
{:type, 0, :tuple, [{:atom, 0, :ok}, {:type, 704, :pid, []}]}
...> parsed.module
GenServer
...> parsed.name
:on_start
...> parsed.params
[]
...> parsed.quoted
{{:., [], [GenServer, :on_start]}, [], []}
...> parsed.type
:type
"""
defmacro parse({:|, _, [_, _]} = type) do
quote bind_quoted: [union: Macro.escape(type)] do
union
|> T.union()
|> T.parse_definition()
|> Stats.type()
end
end
defmacro parse([{:->, _, [args, result]}]) do
type =
case args do
[{:..., _, _}] -> {:type, 0, :any}
args -> {:type, 0, :product, Enum.map(args, &parse_definition/1)}
end
result = parse_definition(result)
quote bind_quoted: [type: Macro.escape(type), result: Macro.escape(result)] do
Stats.type({:type, 0, :fun, [type, result]})
end
end
defmacro parse({{:., _, [module, fun]}, _, params}) when is_params(params) do
params = params |> normalize_params() |> length()
quote bind_quoted: [module: module, fun: fun, params: params] do
Stats.type({module, fun, params})
end
end
defmacro parse({{:., _, [{:__aliases__, _, aliases}, fun]}, _, params})
when is_params(params) do
params = params |> normalize_params() |> length()
quote bind_quoted: [aliases: aliases, fun: fun, params: params] do
Stats.type({Module.concat(aliases), fun, params})
end
end
defmacro parse({:%{}, _meta, fields} = quoted) when is_list(fields),
do: do_parse_map(quoted, __CALLER__)
defmacro parse({:%, _meta, [struct, {:%{}, meta, fields}]}),
do: do_parse_map({:%{}, meta, [{:__struct__, struct} | fields]}, __CALLER__)
defmacro parse({_, _} = tuple), do: do_lookup(tuple)
defmacro parse({:{}, _, content} = tuple) when is_list(content), do: do_lookup(tuple)
defmacro parse({fun, _, params}) when is_atom(fun) and is_params(params) do
quote bind_quoted: [fun: fun, params: param_names(params)] do
Stats.type({:type, 0, fun, params})
end
end
defmacro parse(any) do
Logger.debug("[🚰 T.parse/1]: " <> inspect(any))
do_lookup(any)
end
defp do_parse_map({:%{}, _meta, fields} = quoted, caller) when is_list(fields) do
fields =
fields
|> Enum.map(fn
{{:optional, _, [name]}, type} ->
{:type, 0, :map_field_assoc, Enum.map([name, type], &parse_quoted(&1).definition)}
{{:required, _, [name]}, type} ->
{:type, 0, :map_field_exact, Enum.map([name, type], &parse_quoted(&1).definition)}
{name, type} ->
{:type, 0, :map_field_exact, Enum.map([name, type], &parse_quoted(&1).definition)}
end)
|> Macro.escape()
file = caller.file
quoted = Macro.escape(quoted, prune_metadata: true)
quote location: :keep do
%Tyyppi.T{
definition: {:type, 0, :map, unquote(fields)},
module: nil,
name: nil,
params: [],
quoted: unquote(quoted),
source: unquote(file),
type: :type
}
end
end
defp do_lookup(any) do
quote bind_quoted: [any: Macro.escape(any)] do
any
|> T.parse_definition()
|> Stats.type()
end
end
@doc """
Returns `true` if the `term` passed as the second parameter is of type `type`.
The first parameter is expected to be a `type` as in specs, e. g. `atom()` or
`GenServer.on_start()`.
_Examples:_
iex> require Tyyppi
...> Tyyppi.of?(atom(), :ok)
true
...> Tyyppi.of?(atom(), 42)
false
...> Tyyppi.of?(GenServer.on_start(), {:error, {:already_started, self()}})
true
...> Tyyppi.of?(GenServer.on_start(), :foo)
false
"""
defmacro of?(type, term) do
quote do
%Tyyppi.T{module: module, definition: definition} = Tyyppi.parse(unquote(type))
Matchers.of?(module, definition, unquote(term))
end
end
@spec of_type?(Tyyppi.T.t(wrapped), any()) :: boolean() when wrapped: term()
@doc """
Returns `true` if the `term` passed as the second parameter is of type `type`.
The first parameter is expected to be of type `Tyyppi.T.t(term())`.
_Examples:_
iex> require Tyyppi
...> type = Tyyppi.parse(atom())
%Tyyppi.T{
definition: {:type, 0, :atom, []},
module: nil,
name: nil,
params: [],
quoted: {:atom, [], []},
source: nil,
type: :built_in
}
...> Tyyppi.of_type?(type, :ok)
true
...> Tyyppi.of_type?(type, 42)
false
...> type = Tyyppi.parse(GenServer.on_start())
...> Tyyppi.of_type?(type, {:error, {:already_started, self()}})
true
...> Tyyppi.of_type?(type, :foo)
false
"""
if Application.get_env(:tyyppi, :strict, false) do
def of_type?(%T{module: module, definition: definition}, term),
do: Matchers.of?(module, definition, term)
def of_type?(nil, term) do
Logger.debug("[🚰 Tyyppi.of_type?/2]: " <> inspect(term))
false
end
else
def of_type?(_, _), do: true
end
@doc """
**Experimental:** applies the **local** function given as an argument
in the form `&Module.fun/arity` or **anonymous** function with arguments.
Validates the arguments given and the result produced by the call.
Only named types are supported at the moment.
If the number of arguments does not fit the arity of the type, returns
`{:error, {:arity, n}}` where `n` is the number of arguments passed.
If arguments did not pass the validation, returns `{:error, {:args, [arg1, arg2, ...]}}`
where `argN` are the arguments passed.
If both arity and types of arguments are ok, _evaluates_ the function and checks the
result against the type. Returns `{:ok, result}` _or_ `{:error, {:result, result}}`
if the validation did not pass.
_Example:_
```elixir
require Tyyppi
Tyyppi.apply(MyModule.callback(), &MyModule.on_info/1, 2)
#⇒ {:ok, [foo_squared: 4]}
Tyyppi.apply(MyModule.callback(), &MyModule.on_info/1, :ok)
#⇒ {:error, {:args, :ok}}
Tyyppi.apply(MyModule.callback(), &MyModule.on_info/1, [])
#⇒ {:error, {:arity, 0}}
```
"""
defmacro apply(type, fun, args) do
quote do
%Tyyppi.T{module: module, definition: definition} = Tyyppi.parse(unquote(type))
Tyyppi.Function.apply(module, definition, unquote(fun), unquote(args))
end
end
@doc """
**Experimental:** applies the **external** function given as an argument
in the form `&Module.fun/arity` or **anonymous** function with arguments.
Validates the arguments given and the result produced by the call.
_Examples:_
iex> require Tyyppi
...> Tyyppi.apply((atom() -> binary()),
...> fn a -> to_string(a) end, [:foo])
{:ok, "foo"}
...> result = Tyyppi.apply((atom() -> binary()),
...> fn -> "foo" end, [:foo])
...> match?({:error, {:fun, _}}, result)
true
...> Tyyppi.apply((atom() -> binary()),
...> fn _ -> 42 end, ["foo"])
{:error, {:args, ["foo"]}}
...> Tyyppi.apply((atom() -> binary()),
...> fn _ -> 42 end, [:foo])
{:error, {:result, 42}}
"""
defmacro apply(fun, args) do
quote do
with %{module: module, name: fun, arity: arity} <-
Map.new(Elixir.Function.info(unquote(fun))),
{:ok, specs} <- Code.Typespec.fetch_specs(module),
{{fun, arity}, [spec]} <- Enum.find(specs, &match?({{^fun, ^arity}, _}, &1)),
do: Tyyppi.Function.apply(module, spec, unquote(fun), unquote(args)),
else: (result -> {:error, {:no_spec, result}})
end
end
@doc false
defdelegate parse_quoted(type), to: Tyyppi.T
@doc false
defdelegate void_validation(value), to: Tyyppi.Value.Validations, as: :any
@doc false
defdelegate void_coercion(value), to: Tyyppi.Value.Coercions, as: :any
@doc false
defmacro coproduct(types), do: {:|, [], types}
@doc false
defp setup_ast(import?) do
[
if(import?,
do: quote(generated: true, do: import(Tyyppi)),
else: quote(generated: true, do: require(Tyyppi))
),
quote generated: true, location: :keep do
import Kernel, except: [defstruct: 1]
import Tyyppi.Struct, only: [defstruct: 1]
alias Tyyppi.Value
end
]
end
@doc false
defp can_struct_guard? do
String.to_integer(System.otp_release()) > 22 and
Version.compare(System.version(), "1.11.0") != :lt
end
@doc false
defp maybe_struct_guard(struct) do
name = struct |> Module.split() |> List.last() |> Macro.underscore()
name = :"is_#{name}"
if can_struct_guard?() do
quote generated: true, location: :keep do
@doc "Helper guard to match instances of struct #{inspect(unquote(struct))}"
@doc since: "0.9.0", guard: true
defguard unquote(name)(value)
when is_map(value) and value.__struct__ == unquote(struct)
end
else
quote generated: true, location: :keep do
@doc """
Stub guard to match instances of struct #{inspect(unquote(struct))}.
Upgrade to 11.0/23 to make it work.
"""
@doc since: "0.9.0", guard: true
defguard unquote(name)(value) when is_map(value)
end
end
end
@doc false
defmacro formulae_guard, do: maybe_struct_guard(Formulae)
@doc false
defmacro __using__(opts \\ []) do
import? = Keyword.get(opts, :import, false)
guards =
case __CALLER__.context_modules do
[] -> []
[_some | _] -> [maybe_struct_guard(Tyyppi.Value)]
end
guards ++ setup_ast(import?)
end
@doc false
@spec can_flatten?(type :: module()) :: boolean()
def can_flatten?(type) do
{:flatten, 2} in Keyword.take(type.__info__(:functions), [:flatten])
end
@doc false
@spec any :: Tyyppi.T.t(term())
def any, do: parse(any())
end