我使用Elixir和Phoenix创建了一个 JSON api
我的控制器中有一个用于创建操作的端点,它采用如下所示的 json 数据:
[{"opens_detail"=>
[{"ua"=>"Linux/Ubuntu/Chrome/Chrome 28.0.1500.53",
"ip"=>"55.55.55.55",
"ts"=>1365190001,
"location"=>"Georgia, US"}],
"template"=>"example-template",
"metadata"=>{"user_id"=>"123", "website"=>"www.example.com"},
"clicks"=>42,
"ts"=>1365190000,
"state"=>"sent",
"clicks_detail"=>
[{"ua"=>"Linux/Ubuntu/Chrome/Chrome 28.0.1500.53",
"ip"=>"55.55.55.55",
"ts"=>1365190001,
"url"=>"http://www.example.com",
"location"=>"Georgia, US"}],
"email"=>"recipient.email@example.com",
"subject"=>"example subject",
"sender"=>"sender@example.com",
"_id"=>"abc123abc123abc123abc123",
"tags"=>["password-reset"],
"opens"=>42}]
我的目标是获取这个 json 并从中创建一个新的,其中一些键和值被重命名以匹配我下面的模式:
在web/models/messages.ex
...
schema "messages" do
field :sender, :string
field :uniq_id, :string # equal to '_id' in the payload
field :ts, :utc_datetime
field :template, :string
field :subject, :string
field :email, :string
field :tags, {:array, :string}
field :opens, :integer
field :opens_ip, :string # equal to nested 'ip' value in 'open_details'
field :opens_location, :string # equal to nested 'location' value in 'open_details'
field :clicks, :integer
field :clicks_ip, :string # equal to nested 'ip' value in 'click_details'
field :clicks_location, :string # equal to nested 'location' value in 'click_details'
field :status, :string # equal to the "state" in the payload
timestamps()
end
...
这是我尝试过的:
在web/controller/message_controller.ex:
def create(conn, payload) do
%{ payload |
"uniq_id" => payload["_id"],
"status" => payload["type"]
"open_ips" => Enum.at(payload["opens_detail"], 0)['ip'],
"open_location" => Enum.at(payload["opens_detail"], 0)['location'],
"click_ips" => Enum.at(payload["clicks_detail"], 0)['ip'],
"click_location" => Enum.at(payload["clicks_detail"], 0)['location'],
}
changeset = Message.changeset(%Message{}, payload)
...
end
但很快就清楚它也不起作用,因为我仍然需要删除一些键。
我来自 Ruby/Python (Rails/Django),不想用我的 OO 知识开始污染我对函数式编程的学习,特别是 elixir/phoenix。
你将如何解决这个问题?