Skip to content

Commit

Permalink
Configurable jason/transformer/flattenable in Nested
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksei Matiushkin committed Feb 7, 2024
1 parent 5656391 commit 345ab90
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ end
I suggest adding [`boundary`](https://hexdocs.pm/boundary) as a dependency since that is used in this project.

## Changelog
* `1.2.5` — `use Estructura.Nested flattenable: boolean(), jason: boolean(), transformer: boolean()`
* `1.2.3` — Several `coerce/1` and `validate/1` clauses, default coercers
* `1.2.2` — `Estructura.Flattenable`
* `1.2.1` — Generators for `:datetime` and `:date`
Expand Down
21 changes: 18 additions & 3 deletions lib/estructura/nested.ex
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,26 @@ defmodule Estructura.Nested do
struct = struct_ast(fields)
generator = generator_ast(fields)

need_jason? =
nested? or
(Module.open?(module) and
module |> Module.get_attribute(:__estructura_nested__) |> Map.get(:jason, true))

need_transformer? =
nested? or
(Module.open?(module) and
module |> Module.get_attribute(:__estructura_nested__) |> Map.get(:transformer, true))

need_flattenable? =
nested? or
(Module.open?(module) and
module |> Module.get_attribute(:__estructura_nested__) |> Map.get(:flattenable, true))

[
quote do
if unquote(nested?), do: @moduledoc(false)

if {:module, Jason} == Code.ensure_compiled(Jason) do
if unquote(need_jason?) and {:module, Jason} == Code.ensure_compiled(Jason) do
@derive Jason.Encoder

@doc "Safely parses the json, applying all the specified validations and coercions"
Expand All @@ -420,8 +435,8 @@ defmodule Estructura.Nested do
do: input |> Jason.decode!() |> unquote(module).cast!()
end

@derive Estructura.Transformer
@derive Estructura.Flattenable
if unquote(need_transformer?), do: @derive(Estructura.Transformer)
if unquote(need_flattenable?), do: @derive(Estructura.Flattenable)

defstruct unquote(Macro.escape(struct))

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Estructura.MixProject do
use Mix.Project

@app :estructura
@version "1.2.4"
@version "1.2.5"

def project do
[
Expand Down
2 changes: 1 addition & 1 deletion test/support/structs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ defmodule Estructura.User do
"""

use Estructura.Nested
use Estructura.Nested, jason: true

shape(%{
created_at: :datetime,
Expand Down

0 comments on commit 345ab90

Please sign in to comment.