在编写函数时,我使用参数匹配如下:
def process_thing( %{} = thing )
Run Code Online (Sandbox Code Playgroud)
我期待这thing是一个地图,并且是可枚举的.不幸的是,这个参数列表也匹配指定的正则表达式~r/regex/和正则表达式(虽然它返回真is_map(~r/thing/))是不可枚举.
我如何制作这个函数定义,以便只将地图 - 或理想情况下,可枚举的东西 - 分派给这个函数?
我Guardian.Plug.EnsureAuthenticated在路由器管道中。如何使它在Controller测试期间通过?
我正在考虑跳过凤凰城,因为我只打算为React应用程序构建一个演示程序,该应用程序使用一些API路由作为其状态.似乎是一个熟悉底层技术的机会.
我想出了以下内容,但感觉非常"硬编码",我很好奇是否有更优雅的解决方案来实现同样的目标.
defmodule DemoApp.Plug.ServeStatic do
use Plug.Builder
@static_opts [at: "/", from: "priv/static"]
plug :default_index
plug Plug.Static, @static_opts
plug :default_404
plug Plug.Static, Keyword.put(@static_opts, :only, ["error_404.html"])
# Rewrite / to "index.html" so Plug.Static finds a match
def default_index(%{request_path: "/"} = conn, _opts) do
%{conn | :path_info => ["index.html"]}
end
def default_index(conn, _), do: conn
# Rewrite everything that wasn't found to an existing error file
def default_404(conn, _opts) do
%{conn | :path_info => ["error_404.html"]}
end
end
Run Code Online (Sandbox Code Playgroud)
这个想法是让/服务index.html没有重定向,并在找不到某些东西时提供错误文件的内容,而不是最小的响应"404 file …
我看到包含一个接收块的函数被递归调用,似乎不是在elixir示例代码中的所有位置的尾部位置.例如:
defmodule A do
def loop do
receive do
:ping ->
IO.puts "Received :ping"
loop # <- Tail position?
:pong ->
IO.puts "Received :pong"
loop # <- Also Tail position?
after
5000 ->
loop # <- Also Tail position?
end
loop # <- Also Tail position?
end
end
Run Code Online (Sandbox Code Playgroud)
是否会收到一个特殊的构造,在所有匹配块的末尾优化尾部位置?如果接收块有after块,它是否适用?如果函数中的接收块之后有代码怎么办?
检查参数是保护子句中的列表列表还是键值对列表的最佳方法是什么?
我提出的解决方案只是抓住头部并进行检查,但我觉得必须有更好的方法.
def stuff(items) when is_list(hd(items)) do
something
end
Run Code Online (Sandbox Code Playgroud) 在Elixir中,我们可以使用以下方法从嵌套数据结构中获取数据:
data = %{field: %{other_field: 1}}
data[:field][:other_field]
Run Code Online (Sandbox Code Playgroud)
如果它包含列表,也可以使用
data = %{field: %{other_field: [1]}}
get_in data, [:field, :other_field, Access.at(0)]
Run Code Online (Sandbox Code Playgroud)
但是考虑到data.field.other_field是一个结构,如何获取该数据?上面的两种方法都会失败,因为结构没有实现Access.fetch/2。
data = %{field: %{other_field: %Struct{a: 1}}}
Run Code Online (Sandbox Code Playgroud)
那么访问除模式匹配之外的嵌套结构数据的正确方法是什么?
鉴于以下内容:
for fn_name <- [:foo, :bar, :baz] do
defmacro unquote(fn_name)(do: inner) do
fn_name = unquote(fn_name) # <--- Why?
quote do
IO.puts "#{unquote(fn_name)} called"
unquote(inner)
end
end
end
Run Code Online (Sandbox Code Playgroud)
是什么原因fn_name = unquote(fn_name)?如果我省略这一行,那就是编译错误.这种"双重"不引用的原因是什么?
我有一个GenServer通过连接到远程TCP连接的gen_tcp。
opts = [:binary, active: true, packet: :line]
{:ok, socket} = :gen_tcp.connect('remote-url', 8000, opts}
我正在处理以下消息:
def handle_info({:tcp, socket, msg}, stage) do
IO.inspect msg
{:noreply, state}
end
哪个很棒。但是,TCP服务器容易超时。如果使用gen_tcp.recv,则可以指定超时。但是,我使用active: true来接收消息handle_info,而不必遍历和调用recv。因此,GenServer即使服务器已超时,也愉快地等待下一条消息。
GenServer如果X秒后仍未从TCP连接接收到消息,该如何触发函数?我会坚持使用recv吗?
我可以做一个GET请求,但我似乎无法获得POST请求.
这些是我的路线
article_path GET /api/articles Lxphnx.ArticleController :index
article_path GET /api/articles/:id Lxphnx.ArticleController :show
article_path POST /api/articles Lxphnx.ArticleController :create
article_path PATCH /api/articles/:id Lxphnx.ArticleController :update
PUT /api/articles/:id Lxphnx.ArticleController :update
article_path DELETE /api/articles/:id Lxphnx.ArticleController :delete
Run Code Online (Sandbox Code Playgroud)
除了使用mix phoenix.gen.json之外,我还没有碰过任何东西.这个项目只是一个API,因此我在创建项目时也使用了--no-brunch --no-html.
控制器:
defmodule Lxphnx.ArticleController do
use Lxphnx.Web, :controller
alias Lxphnx.Article
def index(conn, _params) do
articles = Repo.all(Article)
render(conn, "index.json", articles: articles)
end
def create(conn, %{"article" => article_params}) do
changeset = Article.changeset(%Article{}, article_params)
case Repo.insert(changeset) do
{:ok, article} ->
conn
|> put_status(:created)
|> put_resp_header("location", article_path(conn, :show, article))
|> …Run Code Online (Sandbox Code Playgroud)