繁体   English   中英

在功能标题中匹配地图中的地图的模式

[英]Pattern matching a map within a map in the function header

我目前有一个params实例,看起来像这样:

  params: %{
    "continent" => %{
      "deleted_date" => nil,
      "name" => "Asia",
      "to_be_deleted" => true
    },
    "id" => "16"
  },

我正在尝试对to_be_deleted键进行模式匹配,以便如果为true,则将运行不同版本的update/2 ,同时还将continents的内容分配给continent_params

我当前的update/2

  def update(conn, %{"id" => id, "continent" => continent_params}) do
    # stuff         
  end

我尝试匹配其他版本的样式:

  def update(conn, %{"id" => id, %{"to_be_deleted" = true} => continent_params}) do
    #stuff
  end

但是,这会产生关于}的语法警告,我无法清除。 我不确定是否要在函数头中做太多事情,还是应该使用其他语法访问“地图中的地图”。

尝试这种方式:

def update(conn, %{"id" => id, "continent" => %{"to_be_deleted" = true} = continent_params}) do
  #stuff                       ^
end

您忘记了针对"continent"键的图案匹配。

暂无
暂无

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

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