Retrieving information from a List of maps?

(Customerbuilder.GitLab.get!("/projects/5112/pipelines/245667/variables", [{"Accept", "Application/json"}, {"PRIVATE-TOKEN", token}]) |> Map.get(:body),

This is an array of maps coming from gitlab

[
  %{
    "key" => "CUSTOMER",
    "raw" => false,
    "value" => "EOG.Acme.ManualXML",
    "variable_type" => "env_var"
  },
  %{
    "key" => "TRIGGER_PAYLOAD",
    "raw" => false,
    "value" => "{\"variables\":{\"CUSTOMER\":\"EOG.Acme.ManualXML\",\"VERSION\":\"Version-8.6.2\"},\"id\":\"5112\",\"ref\":\"main\"}",
    "variable_type" => "file"
  },
  %{
    "key" => "VERSION",
    "raw" => false,
    "value" => "Version-8.6.2",
    "variable_type" => "env_var"
  }
]

I want to put the version and customer into a new struct or something so I can pass it to a heex template. I need to add some other details like if the build is running/failed/success.

This seems easy but I am finding this difficult :confused:

|> Enum.into(%{ “key” => key})

|> Map.get(:body), :key)

|> Map.get(:body), “key”)

|> [ key: “CUSTOMER” ]

|> Map.get(:body) |> [ key: “CUSTOMER” ]

Hi @roganjoshua can you show what you’ve tried so far?

What’s the final shape of the data you want to arrive at?

[
%Build{status: "running", version: "8.6.1", customer: "Acme"},
%Build{status: "running", version: "8.6.1", customer: "Bloggs Inc"}
]

I want to iterate of the list in a <.table in the Heex - I think that is the right model?

Where does status come from? I can only see customer and version in your initial snippet.

it comes from a previous request

  case GitLab.get(
           "/projects/5112/jobs?scope[]=pending&scope[]=running",
           [
             {"Accept", "Application/json"},
             {"Content-Type", "Application/json"},
             {"PRIVATE-TOKEN", token}
           ]
         ) do
      {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
        body
        |> Enum.map(fn x ->
          %{
            "pipelineid" => x["pipeline"]["id"],
            "status" => x["status"]
          }
        end)
        |> Enum.map(fn x ->
          {
            case GitLab.get(
                   "/projects/5112/pipelines/#{x["pipelineid"]}/variables",
                   [
                     {"Accept", "Application/json"},
                     {"Content-Type", "Application/json"},
                     {"PRIVATE-TOKEN", token}
                   ]
                 ) do
              {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
                body |> IO.inspect()
            end
          }
        end)
        |> Enum.map(fn x ->
          x["key"]  -> "CUSTOMER"

          %{
            "Version" => x["value"],
            "Customer" => x["value"]
          }
        end)
        |> Enum.map(&IO.puts/1)

This is sub-optimal but I am trying to sort of test to get something working

There are a few things I could change but let’s get your stuff working first.

As Ben asked, what have you tried? You edited your first post but I didn’t find that snippet there convincing, or even fully related to the input data.

BTW do you know about dbg/2? You can put it after each phase of your pipe to see how the data changed. Or you can just do it step by step in iex.

To me it’s fairly easy to get customer and version from your original map and convert it like you need but I’d like to see your thought process and how would you go about solving the problem.

I didn’t know about dbg/2

|> Enum.into(%{ “key” => key})

OR 

|> Map.get(:body), :key)
OR 

|> Map.get(:body), “key”)
OR 

|> [ key: “CUSTOMER” ]
OR 

|> Map.get(:body) |> [ key: “CUSTOMER” ]


That shouldn’t even compile, no?

probably not :frowning:

Then let’s start trying stuff that actually works. :smiley:

Say you have a single map, only one:

  %{
    "key" => "CUSTOMER",
    "raw" => false,
    "value" => "EOG.Acme.ManualXML",
    "variable_type" => "env_var"
  }

How do you get the customer out of it?

2 Likes

It’s an array of maps

I am trying to walk you through the process, hence I am simplifying.

Stay with me here now and tell me how do you fetch a value out of a map?

I think I can pattern match where key = “CUSTOMER”?

OK, show me how. Go to iex and paste this:

map = %{
    "key" => "CUSTOMER",
    "raw" => false,
    "value" => "EOG.Acme.ManualXML",
    "variable_type" => "env_var"
  }

Then, again in iex, show me how do you get "CUSTOMER" out of the whole thing.

map[“key”] and map[“value”]

OK, that will do for now. Now how do you make a map like %{customer: "CUSTOMER"} out of the previous value?

%{customer: map["value"]}

Right, now let’s modify the example. Now you have a list of maps. How will you process each element like that?