Skip to content

Commit

Permalink
Inspect for Elixir >= 1.8
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksei Matiushkin committed Feb 13, 2019
1 parent e2ac6d3 commit 4dfb16e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
18 changes: 13 additions & 5 deletions lib/pyc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ defmodule Pyc do
`use Pyc` keyword argument accepts two keys at the moment:
- `definition:` the struct definition that will be passed as is to underlying `defstruct`
- `constrainst:` the list of constrainst the struct to be validated against on updates.
- `definition:` the struct definition that will be passed as is to underlying `defstruct`,
- `constrainst:` the list of constrainst the struct to be validated against on updates,
- `inspect:` since `0.2.1` we support list of fields to derive `Inspect` algebra for;
works on Elixir greater or equal to 1.8.0. When omitted, all the fields given via
`definition` parameter are used.
_Please note:_ there is no way to guarantee the validation of fields in struct in general.
Direct assignment `%MyStruct{my | foo: :bar}` and `Map.put(%MyStruct{}, :foo, :bar)`
Expand Down Expand Up @@ -56,7 +59,15 @@ defmodule Pyc do

@definition Keyword.get(unquote(opts), :definition)
if is_nil(@definition), do: raise(ArgumentError)

@fields if Keyword.keyword?(@definition), do: Keyword.keys(@definition), else: @definition

@inspect Keyword.get(unquote(opts), :inspect, @fields)
if Version.compare(System.version(), "1.7.999") == :gt,
do: @derive({Inspect, only: @inspect})

defstruct(@definition)
@after_compile {Pyc.Helpers.Hooks, :after_pyc}

@doc ~s"""
Validates the `%#{__MODULE__}{}` instance against the set of constraints
Expand All @@ -79,9 +90,6 @@ defmodule Pyc do
def validate(result), do: {:error, result}
end

@fields if Keyword.keyword?(@definition), do: Keyword.keys(@definition), else: @definition
@after_compile {Pyc.Helpers.Hooks, :after_pyc}

defmacrop __this__() do
{:%, [],
[
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Pyc.MixProject do

@app :pyc
@github "am-kantox/#{@app}"
@version "0.2.0"
@version "0.2.1"

def project do
[
Expand Down
6 changes: 6 additions & 0 deletions test/pyc_protocols_behaviours_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ defmodule Pyc.Test.ProtocolsBehaviours.Test do
assert put_in(input, [:bar, :baz], 42) == %Pyc.Test{bar: %{baz: 42}, baz: [], foo: 42}
assert put_in(input, [:foo], :bar) == {:error, %Pyc.Test{bar: %{}, baz: [], foo: :bar}}
end

if Version.compare(System.version(), "1.7.999") == :gt do
test "inspect" do
assert inspect(%Pyc.TestInspect{}) == "#Pyc.TestInspect<bar: %{key: :value}, foo: 42, ...>"
end
end
end
5 changes: 5 additions & 0 deletions test/support/test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ defmodule Pyc.TestEmptyRules do
%__MODULE__{this | foo: value, baz: [42 | baz]}
end
end

defmodule Pyc.TestInspect do
@moduledoc false
use Pyc, definition: [foo: 42, bar: %{key: :value}, baz: []], inspect: [:bar, :foo]
end

0 comments on commit 4dfb16e

Please sign in to comment.