shahryarjb
Nested list of maps add or update another nested key
I have a nested list that should follow a rules to update a record or add a record.
I have a keys list which is the path I need to update or add
Entry list
[
%{
name: :actor,
opts: [
struct: MishkaDeveloperToolsTest.GuardedStruct.NestedConditionalFieldTest.Actor,
derive: "validate(map, not_empty)",
hint: "001-1-map",
validator: {ConditionalFieldValidatorTestValidators, :is_map_data},
__tag__: "root"
]
},
%{
name: :actor,
opts: [
derive: "sanitize(tag=strip_tags) validate(url, max_len=160)",
hint: "001-2-url",
validator: {ConditionalFieldValidatorTestValidators, :is_string_data},
__tag__: "root"
]
}
]
For example I have a keys list like this ["root", "aa"], and I want to add new record to the list
- I need to find is there a list that has
__tag__: "root::aa"if not add another map to bottom of the list - if it can be found, I need add a map inside
fieldskey
When can not find
[
%{
name: :actor,
opts: [
struct: MishkaDeveloperToolsTest.GuardedStruct.NestedConditionalFieldTest.Actor,
derive: "validate(map, not_empty)",
hint: "001-1-map",
validator: {ConditionalFieldValidatorTestValidators, :is_map_data},
__tag__: "root"
]
},
%{
name: :actor,
opts: [
derive: "sanitize(tag=strip_tags) validate(url, max_len=160)",
hint: "001-2-url",
validator: {ConditionalFieldValidatorTestValidators, :is_string_data},
__tag__: "root"
]
},
// add new map last of the list
%{
name: :actor,
opts: [
derive: "sanitize(tag=strip_tags) validate(url, max_len=160)",
hint: "001-2-url",
validator: {ConditionalFieldValidatorTestValidators, :is_string_data},
__tag__: "root:aa"
]
}
]
When we find it
[
%{
name: :actor,
opts: [
struct: MishkaDeveloperToolsTest.GuardedStruct.NestedConditionalFieldTest.Actor,
derive: "validate(map, not_empty)",
hint: "001-1-map",
validator: {ConditionalFieldValidatorTestValidators, :is_map_data},
__tag__: "root"
]
},
%{
name: :actor,
opts: [
derive: "sanitize(tag=strip_tags) validate(url, max_len=160)",
hint: "001-2-url",
validator: {ConditionalFieldValidatorTestValidators, :is_string_data},
__tag__: "root"
]
},
%{
name: :actor,
opts: [
derive: "sanitize(tag=strip_tags) validate(url, max_len=160)",
hint: "001-2-url",
validator: {ConditionalFieldValidatorTestValidators, :is_string_data},
__tag__: "root:aa"
],
// add the map inside fields
fields: [
%{
name: :actor,
opts: [
derive: "sanitize(tag=strip_tags) validate(url, max_len=160)",
hint: "001-2-url",
validator: {ConditionalFieldValidatorTestValidators, :is_string_data},
__tag__: "root:aa"
]
}
]
}
]
As you see I added it inside the last map and add fields keys.
It should be noted the path keys can have 3 or 4 etc items and my code needs to implement none-limit nested map. for example:
__tag__: "root:aa::bb::cc::gg"
["root", "aa", "bb", "cc", "gg"]
Thank you very much for your guidance
First Post!
gpopides
defmodule E do
def foo(record, keys, list) do
key = Enum.join(keys, "::")
# a function that extracts the key from the record
record_key = fn record ->
record.opts[:__tag__]
end
assign_key = fn record ->
%{record | opts: Keyword.replace(record.opts, :__tag__, key)}
end
Enum.group_by(list, record_key)
|> Map.update(key, record, fn existing_record ->
existing_record[:fields] = [record | existing_record[:fields]]
end)
|> Map.values()
end
end
You can use something like this. Probably its not 100% correct but the idea is there.
- construct the key from the list
- Convert the list to a map using the key you want to check if it exists
- Use Map.update/4 to update the record with the given key. If it exists, you push the record to the
fieldsfield. If not you assign the record as a value of the map - Convert back to a list to get the same structure you had as input
- use
assign_keyto set the recordstagto the join key constructed from the list.
Popular in Questions
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service.
Currently when I de...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
Hi,
I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
Hi!
In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir?
Searched the docs for ip address and the web, no good results.
Thanks!
New
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
New
Lets say I have map like this fetching from my database
%{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
Other popular topics
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
Phoenix 1.4.0 released
Phoenix 1.4 is out! This release ships with exciting new features, most notably
with HTTP2 support, improved deve...
New
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









