Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an accessor pattern #25

Open
sitch opened this issue Dec 16, 2020 · 2 comments
Open

Add an accessor pattern #25

sitch opened this issue Dec 16, 2020 · 2 comments

Comments

@sitch
Copy link

sitch commented Dec 16, 2020

I find often times I'm just folding a map, and most of the code ends up just being getting a key out of the map and then passing it to some validation/execution function

Current behavior

defmodule MyOpus do
  use Opus.Pipeline
  
  check :valid_email?

  def valid_email?(%{email: email}) when is_binary(email) do
    MyValidator.valid_email?(email)
  end
end

Useful/Dry behavior

defmodule MyOpus do
  use Opus.Pipeline
  import MyValidator, only: [valid_email?: 1]
  
  # some accessor/getter/lens syntax
  check :valid_email?, in: :email
  # or
  check :valid_email?, get_in: [:email]
  # or
  check {:email, :valid_email?}
end
@zorbash
Copy link
Owner

zorbash commented Dec 17, 2020

Hi @sitch, thank you for using Opus.

Have you tried?

defmodule MyOpus do
  use Opus.Pipeline

  import MyValidator, only: [valid_email?: 1]

  check :valid_email?, with: &valid_email?(&1.email)
end

@sitch
Copy link
Author

sitch commented Jan 7, 2021

Yes of course, but my general use case is:

  1. Pass initial context as a map
  2. Run a step
  3. if it returns {:ok, map} then merge it into the current context

So what ends up happening is:

  • Map.merge ceremony
  • :ok tuple ceremony
  • extracting key-values to pass to a function like that valid_email?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants