acrolink

acrolink

How to check for a condition inside a pipe chain?

Inside a pipe chain, how to execute one pipe line / exclude it based on say is_list(variable)? i.e. execute pipe line 5 only if variable is a list. Is that possible?

Marked As Solved

rossta

rossta

It might be possible to extract a function where you can pattern match and compose the query. Here’s what it could look like for the expires policy (actual code may depend on where your functions and usage are located):

# Policy module
def with_expiry(page, nil) do
  expires_params = "01-01-1970" |> Timex.parse!("%d-%m-%Y", :strftime) |> Ecto.DateTime.cast!
  page |> where([p, c], p.expires >= ^expires_params)
end
def with_expiry(page, expires_params) do
  page |> where([p, c], p.expires >= ^expires_params)
end

#...

page =
  Policy
    |> join(:left, [p], c in Client, p.client_id == c.id)
    |> where([p, c], c.user_id == ^conn.assigns.current_user.id)
    |> Policy.with_expiry(params["policy"]["expires"])
    # ...

Also Liked

acrolink

acrolink

@rossta, @vidalraphael, @kelvinst

Thank you very much… I have learned new things here today… I am happy that I have found my way back to this forum after some bad bi yearly banning experiences at holy stackoverflow (latest only because I asked a duplicate question)…

OvermindDL1

OvermindDL1

I’m often lazy and just put a case between things… >.>

value
|> something(42)
|> something_else()
|> case do
     page where limit_size == nil -> page
     page -> page |> limit(size)
   end
|> some_more_things()
...

Also you really need to use code tags instead of quotes for bodies of code, you do it like:
```elixir
Code goes here like 2+2
```
Makes:

Code goes here like 2+2
wolf4earth

wolf4earth

As a possible alternative to writing a full fledged maybe_* function, you can also consider to use a general purpose maybe_if/3 function. I’ve done that in the past when I needed to chain multiple optional steps.

initial_data
|> first_transformation()
|> maybe_if(should_apply_step2, &second_transformation/1)
|> maybe_if(should_apply_step3, &third_transformation_with_params(&1, my_param))
|> another_one()

where maybe_if is defined like this:

def maybe_if(data, true, action)
  when is_function(action, 1),
  do: apply(action, data)

def maybe_if(data, false, _action), do: data
vidalraphael

vidalraphael

Actually, you should never assign variables inside conditionals like if, case and so on.

Instead of writing:

if is_list(params["policy"]["client_id"]) do
  page = page |> where([p, c], p.client_id in ^params["policy"]["client_id"])
end

You should write:

page = 
  if is_list(params["policy"]["client_id"]) do
    page |> where([p, c], p.client_id in ^params["policy"]["client_id"])
  else
    page
  end

Other than that I think it’s a good solution, but you could also take a look at Ecto’s 2.1 new dynamic expressions: https://github.com/elixir-ecto/ecto/blob/v2.1/CHANGELOG.md

kelvinst

kelvinst

Yep! @rossta thank you! That’s the way to go!

Where Next?

Popular in Questions Top

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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
qwerescape
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
joeerl
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

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
vac
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 ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement