Packages
k8s
0.5.2
2.8.0
2.7.0
2.6.2
2.6.1
2.6.0
2.5.0
2.4.2
2.4.1
2.4.0
2.3.0
2.2.0
2.1.1
2.1.0
2.0.3
2.0.2
2.0.1
2.0.0
2.0.0-rc.6
2.0.0-rc.5
2.0.0-rc.4
2.0.0-rc.3
2.0.0-rc.2
2.0.0-rc.1
2.0.0-rc.0
1.2.1
1.2.0
1.1.10
1.1.9
1.1.8
1.1.7
1.1.6
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.0.0
1.0.0-rc1
0.5.2
0.5.1
0.5.0
0.5.0-rc.2
0.5.0-rc.1
0.4.0
0.3.2
0.3.1
0.3.0
0.2.13
0.2.12
0.2.11
0.2.10
0.2.9
0.2.8
0.2.7
retired
0.2.6
0.2.5
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.3
Kubernetes API Client for Elixir
Current section
Files
Jump to
Current section
Files
lib/k8s/resource/named_list.ex
defmodule K8s.Resource.NamedList do
@moduledoc """
Provides an accessor to a list of maps whereas each element in the list has a key named "name". The name should
be unique within the list and therefore defining the element.
## Examples
iex> get_in([%{"name" => "key1", "value" => "value1"}, %{"name" => "key2", "value" => "value2"}], [K8s.Resource.NamedList.access("key2"), "value"])
"value2"
iex> put_in([%{"name" => "key1", "value" => "value1"}, %{"name" => "key2", "value" => "value2"}], [K8s.Resource.NamedList.access("key1"), "value"], "value_new")
[%{"name" => "key1", "value" => "value_new"}, %{"name" => "key2", "value" => "value2"}]
iex> pop_in([%{"name" => "key1", "value" => "value1"}, %{"name" => "key2", "value" => "value2"}], [K8s.Resource.NamedList.access("key1"), "value"])
{"value1", [%{"name" => "key1"}, %{"name" => "key2", "value" => "value2"}]}
"""
@spec access(binary) :: Access.access_fun(data :: list(), get_value :: term)
def access(name) do
fn op, data, next ->
if Enum.count(data, match_name_callback(name)) > 1 do
raise ArgumentError, "The name #{name} is not unique in the given list: #{inspect(data)}"
end
access(op, data, name, next)
end
end
defp access(:get, data, name, next) when is_list(data) do
data |> Enum.find(match_name_callback(name)) |> next.()
end
defp access(:get_and_update, data, name, next) when is_list(data) do
index = Enum.find_index(data, match_name_callback(name))
{value, rest} = List.pop_at(data, index)
case next.(value) do
{get, update} -> {get, [update | rest]}
:pop -> {value, rest}
end
end
defp access(_op, data, _name, _next) do
raise ArgumentError,
"Kubernetes.NamedList.access/1 expected a named list, got: #{inspect(data)}"
end
defp match_name_callback(name), do: &(&1["name"] == name)
end