Parsing from one struct to the other one when the fields are different?

Hello everyone, right now i have 2 different structures. The first one is a message struct and the 2nd one is a thanks struct. These structures have different fields, i’m new to Elixir and i’m not very sure what is a good approach for parsing from one struct to the other one when the fields are different. To be more precise, i have to parse the 1st one into the 2nd one.

%Message{
	date: date,
	text: "...",
	type: %MesageType{
					type: "command",
					selector: "/start",
				},
	from: %Telegram.User{
         first_name: "joel",
         id: 3132,
         last_name: "Ho",
         username: "Joel"
       },
	chat: %Telegram.Chat{
         id: 3132,
         type: "private",
         username: "JH"
       }
}
%Thanks{
	text: "text",
	type: %MesageType{
					type: "command",
					selector: "/start",
				},
	chat_id: 3132,
	from_user:  %User{
				username: "JH"
				first_name: "Joel"
			        last_name: "Ho"
			        chat_id: 3132
			},
	to_user: %User{
				username: "Luis"
				first_name: "Luis"
			        last_name: "Maxwell"
			        chat_id: 6789
			},
}

I don’t know if it’s the best/most idiomatic way of doing it but why not simply pattern match on the first struct and update the second with the new fields :

def thanks_from_message(%Message{text: text, from: from, type:type}, %Thanks{}= thanks) do
    %{thanks | chat_id: from.chat_id, text: text, type: type, from_user: from}
end
5 Likes