Skip to content

Commit

Permalink
📎 [🐜] fix for numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksei Matiushkin committed Mar 13, 2024
1 parent 376bdae commit e5da445
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
9 changes: 9 additions & 0 deletions lib/estructura/aston.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ defmodule Estructura.Aston do
def coerce_content(nil), do: {:ok, nil}
def coerce_content(true), do: {:ok, true}
def coerce_content(false), do: {:ok, false}
def coerce_content(number) when is_number(number), do: {:ok, number}
def coerce_content(text) when is_binary(text), do: {:ok, text}
def coerce_content(value), do: value |> List.wrap() |> do_coerce_content({[], []})

Expand All @@ -87,6 +88,9 @@ defmodule Estructura.Aston do
defp do_coerce_content([head | rest], {good, bad}) when head in [true, false, nil],
do: do_coerce_content(rest, {[head | good], bad})

defp do_coerce_content([head | rest], {good, bad}) when is_number(head),
do: do_coerce_content(rest, {[head | good], bad})

defp do_coerce_content([head | rest], {good, bad}) when is_binary(head),
do: do_coerce_content(rest, {[head | good], bad})

Expand Down Expand Up @@ -117,6 +121,7 @@ defmodule Estructura.Aston do
def validate_content(nil), do: {:ok, nil}
def validate_content(true), do: {:ok, true}
def validate_content(false), do: {:ok, false}
def validate_content(number) when is_number(number), do: {:ok, number}
def validate_content(text) when is_binary(text), do: {:ok, text}
def validate_content(value) when is_list(value), do: {:ok, value}

Expand All @@ -139,6 +144,9 @@ defmodule Estructura.Aston do
def coerce(bool_node, _key_prefix) when bool_node in [true, false],
do: {:ok, bool_node}

def coerce(number_node, _key_prefix) when is_number(number_node),
do: {:ok, number_node}

def coerce(text_node, _key_prefix) when is_binary(text_node),
do: {:ok, text_node}

Expand Down Expand Up @@ -213,6 +221,7 @@ defmodule Estructura.Aston do
defp ast_content(nil), do: ""
defp ast_content(true), do: "true"
defp ast_content(false), do: "false"
defp ast_content(number) when is_number(number), do: "#{number}"
defp ast_content(text_node) when is_binary(text_node), do: text_node
defp ast_content(list) when is_list(list), do: Enum.map(list, &ast_content/1)

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.3.1"
@version "1.3.2"

def project do
[
Expand Down
12 changes: 9 additions & 3 deletions test/estructura/aston_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,22 @@ defmodule Estructura.Aston.Test do
name: "Baz.Baz2",
attributes: %{},
content: [
%Aston{name: "Deep1", attributes: %{}, content: []},
%Aston{name: "Deep2", attributes: %{}, content: []}
%Aston{name: "Deep1", attributes: %{}, content: [true, 3.14]},
%Aston{name: "Deep2", attributes: %{}, content: ["string", nil]}
]
}
]
}} =
Aston.coerce(%{
name: "Bar",
attributes: {:foo, :bar},
content: %{name: ["Baz", "Baz2"], content: [%{name: "Deep1"}, %{name: "Deep2"}]}
content: %{
name: ["Baz", "Baz2"],
content: [
%{content: [true, 3.14], name: "Deep1"},
%{content: ["string", nil], name: "Deep2"}
]
}
})
end

Expand Down

0 comments on commit e5da445

Please sign in to comment.