-
-
Notifications
You must be signed in to change notification settings - Fork 135
Add required!/1, ash_required/1, and ash_required!/2 (issue #261) #707
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
Open
dallingson
wants to merge
2
commits into
ash-project:main
Choose a base branch
from
dallingson:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # SPDX-FileCopyrightText: 2019 ash_postgres contributors <https://github.com/ash-project/ash_postgres/graphs/contributors> | ||
| # | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| defmodule AshPostgres.Functions.RequiredError do | ||
| @moduledoc """ | ||
| Function that returns the value if present, or an error if nil. | ||
| Used for required-attribute validation: `ash_required!(^value, ^attribute)`. | ||
|
|
||
| When the data layer supports `:required_error`, Ash can build | ||
| `expr(ash_required!(^value, ^attribute))` instead of the inline if/is_nil/error block. | ||
| This module is returned from the data layer's `functions/1` so the function is available | ||
| when using AshPostgres. | ||
| """ | ||
| use Ash.Query.Function, name: :ash_required!, predicate?: false | ||
|
|
||
| @impl true | ||
| def args, do: [[:any, :any]] | ||
|
|
||
| @impl true | ||
| def new([value_expr, attribute]) when is_struct(attribute) or is_map(attribute) do | ||
| {:ok, %__MODULE__{arguments: [value_expr, attribute]}} | ||
| end | ||
|
|
||
| def new(_), do: {:error, "ash_required! expects (value, attribute)"} | ||
|
|
||
| @impl true | ||
| def evaluate(%{arguments: [value, attribute]}) do | ||
| if is_nil(value) do | ||
| resource = | ||
| Map.get(attribute, :resource) || raise("attribute must have :resource for ash_required!") | ||
|
|
||
| field = | ||
| Map.get(attribute, :name) || Map.get(attribute, "name") || | ||
| raise("attribute must have :name for ash_required!") | ||
|
|
||
| {:error, | ||
| Ash.Error.Changes.Required.exception( | ||
| field: field, | ||
| type: :attribute, | ||
| resource: resource | ||
| )} | ||
| else | ||
| {:known, value} | ||
| end | ||
| end | ||
|
|
||
| @impl true | ||
| def can_return_nil?(_), do: false | ||
|
|
||
| @impl true | ||
| def evaluate_nil_inputs?, do: true | ||
|
|
||
| @impl true | ||
| def returns, do: :unknown | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # SPDX-FileCopyrightText: 2019 ash_postgres contributors <https://github.com/ash-project/ash_postgres/graphs/contributors> | ||
| # | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| defmodule AshPostgres.Functions.RequiredErrorTest do | ||
| use ExUnit.Case, async: true | ||
|
|
||
| alias AshPostgres.Functions.RequiredError | ||
|
|
||
| test "ash_required!/2 returns error when value is nil" do | ||
| attribute = %{name: :title, resource: MyApp.Post} | ||
|
|
||
| assert {:error, %Ash.Error.Changes.Required{} = err} = | ||
| RequiredError.evaluate(%{arguments: [nil, attribute]}) | ||
|
|
||
| assert err.field == :title | ||
| assert err.resource == MyApp.Post | ||
| assert err.type == :attribute | ||
| end | ||
|
|
||
| test "ash_required!/2 returns value when non-nil" do | ||
| attribute = %{name: :title, resource: MyApp.Post} | ||
|
|
||
| assert {:known, "hello"} = | ||
| RequiredError.evaluate(%{arguments: ["hello", attribute]}) | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function should also live in ash core. We would just need to add support for it in
ash_sql, update the dep here, and then add it to the capabilities list.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would love to help get this implemented in ash core if you would like. Do we need to have this in a separate issue for ash core?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can also go in and make changes to ash_sql if you would like.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please feel free 😄 There are other functions in AshSql and Ash core that you can use as reference. LMK if you have any questions! So first the change in Ash, second the change in AshSql, third the change in AshPostgres.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, just to clarify. What changes need to be made in ash_sql? As of right now I know that I am moving the required_error to ash core and then updating ash_sql and ash_postgres. I just want to make sure I know what needs to be changed in ash_sql. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, actually 🤔 You wouldn't make any changes in AshSql come to think of it. You can modify the SQL implementation file in
ash_postgresto add a handler for the new function in ash core. So we just need to first add the function to ash, and then add an expression handler here 😄 Sorry for misleading.