This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@spec delta_simple(path(), term(), term()) :: [delta()] | |
defp delta_simple(_path, a, b) when a == b, do: [] | |
defp delta_simple(path, a, b) do | |
cond do | |
empty?(a) -> [{path, {:add, b}}] | |
empty?(b) -> [{path, {:delete, a}}] | |
true -> [{path, {:update, a, b}}] | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@empty [false, "", nil, [], 0, 0.0] | |
@spec empty?(any()) :: boolean() | |
def empty?(value) when value in @empty, do: true | |
def empty?(value) when is_list(value), do: Enum.all?(value, &empty?/1) | |
def empty?(value) when is_tuple(value), do: value |> Tuple.to_list() |> empty?() | |
def empty?(value) when is_struct(value), do: value |> Map.from_struct() |> empty?() | |
def empty?(value) when is_map(value), do: value |> Map.values() |> empty?() | |
def empty?(_), do: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@key :__audit_trail__ | |
@enabled? Application.compile_env(:audit, :enabled?, false) | |
@type file_t :: binary() | |
@type line_t :: non_neg_integer() | |
@type trail_t :: {struct(), file_t(), line_t()} | |
@type change_t :: {struct(), trail_t()} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@spec audit_fun(struct(), Macro.Env.t()) :: struct() | |
def audit_fun(r, e) do | |
r |> struct([{@key, payload(r, e)}]) | |
end | |
@spec payload(struct(), Macro.Env) :: trail_t() | |
def payload(r, e), do: {r, e.file, e.line} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmacro audit(record) do | |
quote generated: true do | |
unquote(__MODULE__).audit_fun(unquote(record), __ENV__) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@spec changelist(term) :: [change_t()] | |
defp changelist(r = %_{__audit_trail__: audit_trail}) do | |
if audit_trail, do: [{r, audit_trail} | changelist(record(audit_trail))], else: [] | |
end | |
defp changelist(_), do: [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def to_string(r) do | |
r | |
|> changelist() | |
|> Enum.map_join("\n=====\n", &stringify_change/1) | |
end | |
@spec changelist(term) :: [change_t()] | |
defp changelist(r = %_{__audit_trail__: audit_trail}) do | |
if audit_trail, do: [{r, audit_trail} | changelist(record(audit_trail))], else: [] | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Audit.FileCache do | |
@moduledoc """ | |
Simple File cache Agent | |
""" | |
use Agent | |
def start_link(_args) do | |
Agent.start_link(fn -> %{} end, name: __MODULE__) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Audit.Github do | |
@moduledoc """ | |
Helper functions for generating Github URLs from filename and line number | |
""" | |
@type line_number_t :: binary | non_neg_integer() | |
@type filename_t :: binary() | |
@type git_t :: binary() | nil | |
@spec git([binary()]) :: git_t() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> nil |> Tree.add(2) |> Tree.add(1) |> Tree.add(3) |> Audit.to_string() |> IO.puts | |
github url: https://github.com/rugyoga/tree_ex/tree/main/lib/tree.ex#L15 | |
local path: lib/tree.ex:15 | |
code: item > i -> %Tree{audit(tree) | right: add(r, item)} | |
diff: [{[:right], {:add, %Tree{item: 3, left: nil, right: nil}}}] | |
===== | |
github url: https://github.com/rugyoga/tree_ex/tree/main/lib/tree.ex#L14 | |
local path: lib/tree.ex:14 | |
code: item < i -> %Tree{audit(tree) | left: add(l, item)} | |
diff: [{[:left], {:add, %Tree{item: 1, left: nil, right: nil}}}] |