0

lets say I have a function that gives me a UUIDv4.

def get_uuid() do
  # do magic
end

And I have a nested map that can be n level deep:

map = %{
  name: "Alice",
  friends: [
    %{
      uid: "EXISTINGUID",
      name: "Betty"
    },
    %{
      name: "Bob",
      job: "Truck driver"
    }
  ]
}

How do I traverse the complete map and on each level make sure that there is a key uid and if not, insert it with the value of a call of get_uuid()?

Expected result:

map = %{
  name: "Alice",
  uid: "NEWUUID",
  friends: [
    %{
      uid: "EXISTINGUUID",
      name: "Betty"
    },
    %{
      uid: "ANOTHERNEWUUID"
      name: "Bob",
      job: "Truck driver"
    }
  ]
}

It would already be very helpful without the condition to look for an existing uid key.

1 Answer 1

1
defmodule Uuid do
  def generate(), do: :some_uuid
end

defmodule UuidConsistencyEnforcer do 
  @uuid_key :uid
  def run(some_list) when is_list(some_list), do: Enum.map(some_list, &run/1)

  def run(some_map) when is_map(some_map) do
    some_map
    |> Map.update(@uuid_key, Uuid.generate(), fn existing_uuid -> existing_uuid end)
    |> Enum.reduce(%{}, fn 
      {key, a_map = %{}}, a -> Map.merge(a, %{key => run(a_map)})
      {key, not_a_map}, a -> Map.merge(a, %{key => run(not_a_map)})
    end)
  end

  def run(some_value), do: some_value
end

To test:

Given a deeply nested map such as:

map = %{
  name: "Alice",
  uid: "NEWUUID",
  friends: [
    %{
      uid: "EXISTINGUUID",
      name: "Betty"
    },
    %{
      name: "Bob",
      job: "Truck driver"
    },
    %{
      n1: %{
        n2: %{
          n3: %{
            n4: %{
              name: "Alice",
              uid: "NEWUUID",
              friends: [
                %{
                  uid: "EXISTINGUUID",
                  name: "Betty"
                },
                %{
                  name: "Bob",
                  job: "Truck driver"
                }
              ]
            }
          }
        }
      }
    }
  ]
}

Running the following:

UuidConsistencyEnforcer.run(map)

will render:

%{
  friends: [
    %{name: "Betty", uid: "EXISTINGUUID"},
    %{job: "Truck driver", name: "Bob", uid: :some_uuid},
    %{
      n1: %{
        n2: %{
          n3: %{
            n4: %{
              friends: [
                %{name: "Betty", uid: "EXISTINGUUID"},
                %{job: "Truck driver", name: "Bob", uid: :some_uuid}
              ],
              name: "Alice",
              uid: "NEWUUID"
            },
            uid: :some_uuid
          },
          uid: :some_uuid
        },
        uid: :some_uuid
      },
      uid: :some_uuid
    }
  ],
  name: "Alice",
  uid: "NEWUUID"
}
7
  • In order to account for tuples, etc. you will need to add run/1 functions accordingly inside the module UuidConsistencyEnforcer. Mar 14, 2018 at 23:40
  • Perfect. Thanks! Only thing: Using Uuid as module name yields an error from Elixir. Mar 14, 2018 at 23:41
  • It shouldn't give you an error. I am able to copy paste as is inside iex. What is exactly the error that you are seeing, perhaps you already have a library with the name Uuid defined somewhere. Mar 14, 2018 at 23:43
  • [error] Loading of ~/app/_build/dev/lib/uuid/ebin/Elixir.Uuid.beam failed: :badfile [error] beam/beam_load.c(1412): Error loading module 'Elixir.Uuid': module name in object code is Elixir.UUID Mar 14, 2018 at 23:45
  • Can you copy paste the code in here as is inside a new iex(e.g. do not do iex -S mix inside an existing project)? Mar 14, 2018 at 23:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.