比较2个结构并提取Elixir Phoenix的变化

时间:2017-06-15 08:22:55

标签: elixir phoenix-framework ecto

有2个结构,一个是Camera,另一个是变更集,我正在寻找一种方法来保存2个结构中的旧值和新值,例如

%Camera{__meta__: #Ecto.Schema.Metadata<:loaded, "cameras">, access_rights: [],
 alert_emails: nil, cloud_recordings: nil,
 config: %{"external_host" => "46.126.223.13", "external_http_port" => "81",
   "snapshots" => %{"audio" => "", "h264" => "axis-media/media.amp",
     "jpg" => "axis-cgi/jpg/image.cgi", "mjpg" => "axis-cgi/mjpg/video.cgi",
     "mpeg" => ""}}, created_at: #Ecto.DateTime<2017-03-09 06:27:47>,
 discoverable: false, exid: "aarga-buezp", id: 12361, is_online: true,
 is_online_email_owner_notification: false, is_public: false,
 last_online_at: #Ecto.DateTime<2017-05-15 07:28:49>,
 last_polled_at: #Ecto.DateTime<2017-06-15 07:40:09>, location: nil,
 mac_address: nil, model_id: 4, motion_detections: nil,
 name: " Aargau Obersiggenthal", timezone: "UTC"}

然后有相机的变更集

%{config: %{"auth" => %{"basic" => %{"password" => "", "username" => ""}},
    "external_host" => "46.126.223.13", "external_http_port" => "81",
    "external_rtsp_port" => "",
    "snapshots" => %{"audio" => "", "h264" => "axis-media/media.amp",
      "jpg" => "axis-cgi/jpg/image.cgi", "mjpg" => "axis-cgi/mjpg/video.cgi",
      "mpeg" => ""}}, timezone: "Atlantic/Cape_Verde"}

目前只有一个值,时区正在更新,我正在寻找一种方法来检查所有从结构中收集所有新旧值,因为我在变更集中有几个键,我想比较旧结构的所有键值,看看我改变了什么,然后得到一个只有那些已被改变的值的结构,像这样

%{settings: %{old: old_values, new: new_values}}

有可能用Ecto完成这项工作吗?通过它自己?或者有没有任何方法让Elixir比较两个结构,看看有什么变化?

UPADTE:

#Ecto.Changeset<action: nil,
 changes: %{config: %{"auth" => %{"basic" => %{"password" => "",
         "username" => ""}}, "external_host" => "46.126.223.13",
     "external_http_port" => "81", "external_rtsp_port" => "",
     "snapshots" => %{"audio" => "", "h264" => "axis-media/media.amp",
       "jpg" => "axis-cgi/jpg/image.cgi", "mjpg" => "axis-cgi/mjpg/video.cgi",
       "mpeg" => ""}}, timezone: "Atlantic/Cape_Verde"}, errors: [],
 data: #Camera<>, valid?: true>

第一部分已存在的对象,第二部分是用户尝试更新第一个对象时来自form的值,所以我想要的是创建一个新的地图/结构或者我真的不知道我们称之为只在DB中保存该对象, 1:将变更集值与第一个对象进行比较,如果变更集中的新值很少,则使用新旧子密钥创建新映射或结构。

0 个答案:

没有答案