Hello friends,
I have a JSON file in which I save some information about my apps and their dependencies, so some of my app dependencies may be the same, and I need to merge them, but my priority is the version, so I should reject the lower version and keep the higher version.
[
{"app":"testi","dependencies":[{"app":"phoenix","min":"3.7","max":"4"},{"app":"phoenix_live_view","max":"0.17.7","min":"4.01.0"},{"app":"ueberauth","max":"0.17.7","min":"1.17.7"},{"app":"ueberauth_github","min":"0.8.1"},{"app":"ueberauth_google","min":"0.10.1"}],"dependency_type":"soft_update","git_tag":null,"timeout":null,"type":"hex","update_server":null,"url":"https://hex.pm/packages/mishka_social","version":"0.0.2 "},
{"app":"testi2","dependencies":[{"app":"phoenix","min":"1.6"},{"app":"phoenix_live_view","max":"0.1.0","min":"0.01.78"},{"app":"ueberauth","max":"0.17.7","min":"0.17.7"},{"app":"ueberauth_github","min":"0.8.1"},{"app":"ueberauth_google","min":"0.10.1"},{"app":"test","max":"0.10.25","min":"0.10.1"}],"dependency_type":"soft_update","git_tag":null,"timeout":null,"type":"hex","update_server":null,"url":"https://hex.pm/packages/mishka_social","version":"0.0.2 "}
]
I convert top JSON with Jason to a list and get dependencies
key, the output
[
[
%{"app" => "phoenix", "max" => "4", "min" => "3.7"},
%{"app" => "phoenix_live_view", "max" => "0.17.7", "min" => "4.01.0"},
%{"app" => "ueberauth", "max" => "0.17.7", "min" => "1.17.7"},
%{"app" => "ueberauth_github", "min" => "0.8.1"},
%{"app" => "ueberauth_google", "min" => "0.10.1"}
],
[
%{"app" => "phoenix", "min" => "1.6"},
%{"app" => "phoenix_live_view", "max" => "0.1.0", "min" => "0.01.78"},
%{"app" => "ueberauth", "max" => "0.17.7", "min" => "0.17.7"},
%{"app" => "ueberauth_github", "min" => "0.8.1"},
%{"app" => "ueberauth_google", "min" => "0.10.1"},
%{"app" => "test", "max" => "0.10.25", "min" => "0.10.1"}
]
]
As you see, some app
are the same, but the versions are not and maybe are.
For example
we have 2 times phoenix
, the first one min
(it should be sorted by min, this key is important in version) is 3.7
and the second is 1.6
, so I need to keep
%{"app" => "phoenix", "max" => "4", "min" => "3.7"},
and reject the others
my code:
Enum.map(json_data, &(&1["dependencies"]))
|> Enum.concat
|> Enum.group_by(&(&1["app"]))
|> Map.to_list()
|> Enum.map(fn {_key, list} ->
Enum.sort_by(list, &(&1["min"]))
|> List.last()
end)
I think i’m in a wrong way, do you have any suggestions?
This is the final output
[
%{"app" => "phoenix", "max" => "4", "min" => "3.7"},
%{"app" => "phoenix_live_view", "max" => "0.17.7", "min" => "4.01.0"},
%{"app" => "test", "max" => "0.10.25", "min" => "0.10.1"},
%{"app" => "ueberauth", "max" => "0.17.7", "min" => "1.17.7"},
%{"app" => "ueberauth_github", "min" => "0.8.1"},
%{"app" => "ueberauth_google", "min" => "0.10.1"}
]
By the way, maybe I have another apps which have empty dependencies.
Thank you in advance.