Reputation: 1372
I have a structure (data) where the real values are dependent about the customer is making the request, but I am not able to reassign them. This is my code:
The base structure:
Party: [
Sender: [
]
]
And an example about the real values of a customer:
Party: [
Sender: [
AgentUserSender: [
Name: "The_name",
Type: "The_type",
OtherIDs: [
{:OtherID, %{Description: "value"}, "_value"},
{:OtherID, %{Description: "value_1"}, "_value_1"}
],
OtherKey: "other_value"
]
]
]
And how I (poorly) trying to do:
data[:Party][:Sender] = customer[:Party][:Sender]
There is another added problem: not all customers has the same fields on the structure. This is other example, of other customer:
Party: [
Sender: [
TravelAgencySender: [
Name: "NAME",
IATA_Number: "xxxxxxxx",
AgencyID: "agency"
]
]
Thank you very much.
Upvotes: 1
Views: 116
Reputation: 417
I believe what you want to do is this:
> data = [Party: [Sender: []]]
[Party: [Sender: []]]
> data = put_in(data[:Party][:Sender], ["something"])
[Party: [Sender: ["something"]]]
or this:
> data = [Party: [Sender: ["something"]]]
[Party: [Sender: ["something"]]]
> data = put_in(data[:Party][:Sender], data[:Party][:Sender] ++ ["something_else"])
[Party: [Sender: ["something", "something_else"]]]
#Alternatively update_in instead of put_in
> data = [Party: [Sender: ["something"]]]
[Party: [Sender: ["something"]]]
> data = update_in(data[:Party][:Sender], &(&1 ++ ["something_else"]))
[Party: [Sender: ["something", "something_else"]]]
Upvotes: 1
Reputation: 121000
The most straightforward way would be to use Keyword.get_and_update/3
:
iex(1)> mine = [Party: [ Sender: [ ]]]
iex(2)> cust = [Party: [ Sender: [ .......... ]]]
iex(3)> { _, data } = mine |> Keyword.get_and_update(:Party, fn party ->
...(3)> { _, result } = mine[:Party]
...(3)> |> Keyword.get_and_update(:Sender, fn sender ->
...(3)> { sender, cust[:Party][:Sender] }
...(3)> end)
...(3)> { party, result }
...(3)> end)
iex(4)> data
[Party: [Sender: [AgentUserSender: [Name: "The_name", Type: "The_type",
OtherIDs: [{:OtherID, %{Description: "value"}, "_value"},
{:OtherID, %{Description: "value_1"}, "_value_1"}],
OtherKey: "other_value"]]]]
If you want to merge values, or modify it in some sophisticated way, just modify
{ sender, cust[:Party][:Sender] }
line to return what is needed as second tuple item (the first is to be the value got, and hence is to be left intact.)
Upvotes: 0