简体   繁体   English

使用Elixir中的地图访问列表中的嵌套元素

[英]Access to nested elements in list with maps in elixir

I am new to elixir an phoenix an got some problems accessing nested elements inside a test. 我是不老长寿的菲尼克斯人,在测试中访问嵌套元素时遇到了一些问题。 I am testing a controller and got so far following response: 我正在测试控制器,到目前为止收到以下响应:

.[%{"attributes" => %{"first_name" => "Timmy 96", "last_name" => 
"Assistant"},
"id" => "bca58c53-7c6e-4281-9bc8-0c4616a30051",
"relationships" => %{"avatar" => %{"data" => %{"id" => "011300fd-ca98-42b4-
9561-f1cdc93d2d25",
"type" => "pictures"}}}, "type" => "users"}]

I am using JSON-API-format for the response and am fetching the attributes with userdata: 我正在使用JSON-API-format进行响应,并使用userdata获取属性:

user_attr = Enum.filter(includes, fn(item)->
    item["relationships"]["avatar"] != nil
end)
IO.inspect user_attr
case Enum.fetch(user_attr ,0) do
  {:ok, value} ->
    assert value["attributes"]["first_name"] == user.first_name
    assert value["attributes"]["last_name"] == user.last_name
  {_} ->
    assert false
end

I want to shorten this part, dont want to use a case, but no idea how to get the value of the user_attr without using the value part in the case. 我想缩短此部分,不想使用大小写,但不知道如何在不使用大小写部分的情况下获取user_attr的值。

I would also want to asser the id of the relationships -> avatar -> data -> id with the id I inserted before, but no idea how to access this value. 我还想用我之前插入的ID来确定关系的ID->头像->数据-> ID,但不知道如何访问该值。 The id is part of the picture i inserted before so I would like to id是我插入的图片的一部分,所以我想

assert XXX == picture.id

But how to get the XXX? 但是,如何获得XXX?

Hope someone can help me. 希望可以有人帮帮我。 Last years only Java and C#, never Ruby and now I got somehow into elixir :/ 去年只有Java和C#,从未有Ruby,现在我不知何故进入了Elixir:/

Thanks. 谢谢。

You should try using pattern matching more. 您应该尝试更多使用模式匹配。

# fixture data.
user = %{first_name: "Timmy 96", last_name: "Assistant"}
picture = %{id: "011300fd-ca98-42b4-\n9561-f1cdc93d2d25"}
value = %{
      "attributes" => %{"first_name" => "Timmy 96", "last_name" => "Assistant"},
      "id" => "bca58c53-7c6e-4281-9bc8-0c4616a30051",
      "relationships" => %{
        "avatar" => %{
          "data" => %{
            "id" => "011300fd-ca98-42b4-\n9561-f1cdc93d2d25",
            "type" => "pictures",
          },
        },
      },
      "type" => "users",
    }

assert %{"attributes" => attributes} = value
# ensure the expected value match with actual value and than bind the attributes variable with actual attributes map.
assert %{"first_name" => user.first_name, "last_name" => user.last_name} == attributes

assert %{"relationships" => %{"avatar" => %{ "data" => avatar_data}}} = value
assert %{"id" => picture.id, "type" => "pictures"} == avatar_data

One of Elixir's most powerful features is pattern matching via the = operator(match operator). Elixir最强大的功能之一是通过=运算符(匹配运算符)进行模式匹配。

The above example shows you that we can use match operator to assert that the data structures of expected value match with actual value. 上面的示例向您展示了我们可以使用match运算符断言期望值的数据结构与实际值匹配。

Learn more about testing and pattern matching: https://semaphoreci.com/community/tutorials/introduction-to-testing-elixir-applications-with-exunit 了解有关测试和模式匹配的更多信息: https : //semaphoreci.com/community/tutorials/introduction-to-testing-elixir-applications-with-exunit

You can use get_in/2 to do this. 您可以使用get_in / 2来执行此操作。

iex()> list
[%{"attributes" => %{"first_name" => "Timmy 96", "last_name" => 
"Assistant"},
"id" => "bca58c53-7c6e-4281-9bc8-0c4616a30051",
"relationships" => %{"avatar" => %{"data" => %{"id" => "011300fd-ca98-
42b4-\n9561-f1cdc93d2d25",
     "type" => "pictures"}}}, "type" => "users"}]

iex()> [map] = list
[%{"attributes" => %{"first_name" => "Timmy 96", "last_name" => 
"Assistant"},
"id" => "bca58c53-7c6e-4281-9bc8-0c4616a30051",
"relationships" => %{"avatar" => %{"data" => %{"id" => "011300fd-ca98-
42b4-\n9561-f1cdc93d2d25",
     "type" => "pictures"}}}, "type" => "users"}]


iex()> get_in map, ["attributes", "first_name"]
"Timmy 96"
iex()> get_in map, ["attributes", "last_name"]
"Assistant"
iex()> get_in map, ["relationships", "avatar", "data", "id"]
"011300fd-ca98-42b4-\n9561-f1cdc93d2d25"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM