Packages
graphqexl
0.1.0-alpha-rc.23
0.1.0
0.1.0-alpha-rc.docs-test-6
0.1.0-alpha-rc.docs-test-5
0.1.0-alpha-rc.docs-test-4
0.1.0-alpha-rc.docs-test-3
0.1.0-alpha-rc.docs-test-2
0.1.0-alpha-rc.docs-test
0.1.0-alpha-rc.25
0.1.0-alpha-rc.24
0.1.0-alpha-rc.23
0.1.0-alpha-rc.22
0.1.0-alpha-rc.21
0.1.0-alpha-rc.20
0.1.0-alpha-rc.19
0.1.0-alpha-rc.18
0.1.0-alpha-rc.17
0.1.0-alpha-rc.16
0.1.0-alpha-rc.15
0.1.0-alpha-rc.14
0.1.0-alpha-rc.13
0.1.0-alpha-rc.12
0.1.0-alpha-rc.11
0.1.0-alpha-rc.10
0.1.0-alpha-rc.9
0.1.0-alpha-rc.8
0.1.0-alpha-rc.7
0.1.0-alpha-rc.6
0.1.0-alpha-rc.5
0.1.0-alpha-rc.4
0.1.0-alpha-rc.3
0.1.0-alpha-rc.2
0.1.0-alpha-rc.1
0.1.0-alpha.rc-4
0.1.0-alpha.rc.4
Fully-loaded, pure-Elixir GraphQL server implementation with developer tools
Current section
Files
Jump to
Current section
Files
lib/treex/tree.ex
defmodule Treex.Tree do
defstruct value: nil, children: [], key: nil
@type t :: %Treex.Tree{value: any, children: [t], key: any}
@doc """
Convert the given map into a `t:Treex.Tree.t/0`. If the given map has more than one top-level key,
the optional `root` parameter specifies what value to give a virtual root node that will be
inserted at the top of the key and contain the given `map`'s top-level keys as children.
Returns: `t:Treex.Tree.t/0`
"""
@doc since: "0.1.0"
@spec from_map(Map.t):: Treex.Tree.t
@spec from_map(Map.t, term):: Treex.Tree.t
def from_map(map, root \\ :root)
def from_map(map = %{}, root) when map |> map_size == 0,
do: %Treex.Tree{value: root, children: []}
def from_map(map = %{}, _root) when map |> map_size == 1,
do: map |> Enum.reduce(%Treex.Tree{}, &node_from_element/2)
def from_map(map = %{}, root), do: %{root => map} |> from_map(root)
@doc false
defp node_from_element(pair, _), do: pair |> node_from_element
defp node_from_element({root, children = %{}}) when children |> map_size == 0,
do: %Treex.Tree{value: root, children: []}
defp node_from_element({root, children = %{}}),
do: %Treex.Tree{value: root, children: children |> Enum.map(&node_from_element/1)}
defp node_from_element({root, children}),
do: %Treex.Tree{value: root, children: [%Treex.Tree{value: children, children: []}]}
end