RoboZoom

RoboZoom

Struct Data Transformations - best approach to simple data transformations?

New to elixir here - looking for help for the best approach to simple data transformations.

I have a config file that I need to transform slightly into a slightly different format to fit a service call. In essence, it looks like this:

defmodule AppConfig do
  @enforce_keys [:username, :endpoints]
  defstruct username: "",
            password: "",
            endpoints: "",
            stacktrace: false,
            show_sensitive_data_on_connection_error: false,
            pool_size: 10,
            database: ""
end

defmodule DatabaseConfig do
  alias DatabaseUserConfig

  @type t() :: %__MODULE__{
          name: String.t(),
          users: DatabaseUserConfig.t(),
          options: any()
        }
  @enforce_keys [:name]
  defstruct [:name, :users, :options]
end

defmodule DatabaseUserConfig do
  @type t() :: %__MODULE__{
          username: String.t(),
          passwd: String.t(),
        }

  @enforce_keys [:username]
  defstruct [:username, :passwd]
end

I need to translate AppConfig into DatabaseConfig. Ideally, I’d like to do this in a way so that if I change options, I don’t need to redo this function.

Looking at other libraries, it appears as if the normal way to do this would be to create new function for DatabaseConfig and pass AppConfig as an argument, then inside that function I’d manually assign each property:

def new(app_config) do 
  %DatabaseConfig{ name: app_config.database, users: [%User{name: app_config.name, password: app_config.password], options: %{...}]
end

If I filled out options manually, every time I changed the options format in one place or the other, I’d have to redo this function (and any tests with this of course). Is there a way to do the formatting that I know that I need to do, and pass through everything else?

Marked As Solved

kip

kip

ex_cldr Core Team

Maybe something like this, that uses Map.split/2 to split the map in two?

def new(%AppConfig{} = app_config) do 
  {config, options} = Map.split(app_config, [:name, :password, :database])
  
  %DatabaseConfig{ name: config.database, users: [%User{name: config.name, password: config.password], options: options]
end

Last Post!

RoboZoom

RoboZoom

This will definitely push me in the right direction - thanks!

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40042 209
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement