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

Code Interface: Allow defining ? functions that return the unwrapped boolean result #1696

Open
chazwatkins opened this issue Jan 6, 2025 · 0 comments
Labels
enhancement New feature or request

Comments

@chazwatkins
Copy link
Contributor

Is your feature request related to a problem? Please describe.

There isn't a way to define a question mark function on code interfaces that return a boolean. All code interface functions auto-generate the non-bang (tuple wrapped result) and bang (unwrapped result) versions today.

If you want to define a resource action that returns a boolean typically suffixed with a question mark, you end up with a non-bang and bang-version of it.

For example

define :user_exists?

# Generates
user_exists?(...) -> {:ok, boolean()}
user_exists?! -> boolean()

Question mark functions aren't written this way by convention since they operate like the bang version.

Describe the solution you'd like
A clear and concise description of what you want to happen.

When a code interface function has a question mark suffix, generate one function that works like the bang version, returning only the result.

define :user_exists?

# Generates only
user_exists?(...) -> boolean()

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Express the feature either with a change to resource syntax, or with a change to the resource interface

The current alternative is not to use the code interface to define the function; instead, the question mark function is created in the domain directly.

def user_exists?(user_id, opts \\ []) do
  MyApp.Accounts.User
  |> Ash.Query.for_read(:read, opts)
  |> Ash.Query.filter(id == ^user_id)
  |> Ash.exists?()
end

Or

Combine with a generic action in the resource.

def user_exists?(user_id, opts) do
  MyApp.Accounts.User
  |> Ash.ActionInput.for_action(:user_exists?, opts)
  |> Ash.run_action!()
end

This is a simple example of checking where a record exists but applies to any action that returns a boolean, which should be suffixed with a question mark.

Additional context
This isn't a critical issue and has an easy workaround with defining functions inside the domain. However, this helps keep the resource's actions inside the resource and the use of the domain to expose them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Someday
Development

No branches or pull requests

2 participants