我使用结构在Phoenix/Elixir应用程序中创建自定义模型。像这样:协议枚举没有为struct实现。如何将结构转换为可枚举?
defmodule MyApp.User do
defstruct username: nil, email: nil, password: nil, hashed_password: nil
end
new_user = %MyApp.User{email: "[email protected]", hashed_password: nil, password: "secret", username: "ole"}
为了使用它与我的数据库适配器,我需要的数据是可枚举的。哪一个结构显然不是。至少我收到这个错误:
(Protocol.UndefinedError) protocol Enumerable not implemented for %MyApp.User{ ...
所以我试着用理解力来运气。这当然也不起作用,因为结构是不可枚举(愚蠢的我)
enumberable_user = for {key, val} <- new_user, into: %{}, do: {key, val}
我怎样才能将数据转换为可枚举的地图?