Pratik Khadloya
Pratik Khadloya

Reputation: 12869

Elixir update nested keyword list

Is it possible to update a nested keyword list in elixir? For example i am trying to make the following work

Keyword.put(conn.private.phoenix_endpoint.config(:url), :host, conn.host)

But somehow, the updated host is not reflected in the conn variable.

Upvotes: 2

Views: 1816

Answers (2)

Gazler
Gazler

Reputation: 84170

You can use Kernel.put_in/3 which will work with a combination of maps and keyword lists.

put_in(conn, [:private, :phoenix_endpoint, :config, :url, :host], conn.host) 

Edit As @manukall has pointed out, this won't work in this particular case as conn.private.phoenix_endpoint returns a module and not a map or a keyword list.

Upvotes: 2

manukall
manukall

Reputation: 1462

The problem here is that, assuming you're in a phoenix app, conn.private.phoenix_endpoint will return your endpoint module. So you are basically calling MyApp.Endpoint.config(:url). You can not change that functions return value in that way.

What you could probably do is overwrite the phoenix_endpoint key in conn.private to point it to some other module, but I don't think this is what you want to do.

Upvotes: 1

Related Questions