Skip to content

Commit

Permalink
Estructura.Tree generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksei Matiushkin committed Feb 6, 2024
1 parent 21e4c14 commit 7d0275e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
29 changes: 27 additions & 2 deletions lib/estructura/tree.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ defmodule Estructura.Tree do
@moduledoc """
The implementation of `Estructura` ready to work with tree AST-like structure
"""

@max_children Application.compile_env(:estructura, :tree_children_generate_count, 7)

use Estructura,
access: true,
coercion: true,
Expand All @@ -11,8 +14,19 @@ defmodule Estructura.Tree do
generator: [
name: {StreamData, :string, [:alphanumeric]},
attributes:
{StreamData, :fixed_map, [[key1: {StreamData, :integer}, key2: {StreamData, :integer}]]},
content: {StreamData, :list_of, [{Estructura.Tree, :__generator__}]}
{StreamData, :map_of,
[
{StreamData, :atom, [:alphanumeric]},
{StreamData, :one_of,
[
[
{StreamData, :integer},
{StreamData, :boolean},
{StreamData, :string, [:alphanumeric]}
]
]}
]},
content: {StreamData, :tree, [{StreamData, :fixed_list, [[]]}, &Estructura.Tree.child_gen/1]}
]

if {:module, Jason} == Code.ensure_compiled(Jason) do
Expand Down Expand Up @@ -168,4 +182,15 @@ defmodule Estructura.Tree do
defp ast_content(nil), do: nil
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)

@doc false
def child_gen(_child) do
StreamData.list_of(
StreamData.frequency([
{1, nil},
{2, StreamData.string(:alphanumeric)},
{7, Estructura.Tree.__generator__()}
]),
max_length: @max_children)
end
end
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"makeup": {:hex, :makeup, "1.1.1", "fa0bc768698053b2b3869fa8a62616501ff9d11a562f3ce39580d60860c3a55e", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "5dc62fbdd0de44de194898b6710692490be74baa02d9d108bc29f007783b0b48"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.1", "cc9e3ca312f1cfeccc572b37a09980287e243648108384b97ff2b76e505c3555", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "e127a341ad1b209bd80f7bd1620a15693a9908ed780c3b763bccf7d200c767c6"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.3", "d684f4bac8690e70b06eb52dad65d26de2eefa44cd19d64a8095e1417df7c8fd", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "b78dc853d2e670ff6390b605d807263bf606da3c82be37f9d7f68635bd886fc9"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.4", "29563475afa9b8a2add1b7a9c8fb68d06ca7737648f28398e04461f008b69521", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "f4ed47ecda66de70dd817698a703f8816daa91272e7e45812469498614ae8b29"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
"nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"},
Expand Down

0 comments on commit 7d0275e

Please sign in to comment.