fix: properly surface errors all the way from runtime filters

This commit is contained in:
Zach Daniel 2022-09-14 20:05:51 -04:00
parent 3baf2b8c97
commit 4b88300e00
3 changed files with 77 additions and 17 deletions

View file

@ -55,17 +55,28 @@ defmodule Ash.DataLayer.Simple do
end
def run_query(%{data: data, sort: sort, api: api, filter: filter, limit: limit}, _resource) do
{:ok,
data
|> Enum.filter(&Ash.Filter.Runtime.matches?(api, &1, filter))
|> Ash.Actions.Sort.runtime_sort(sort)
|> then(fn data ->
if limit do
Enum.take(data, limit)
else
data
end
end)}
data
|> do_filter_matches(filter, api)
|> case do
{:ok, results} ->
{:ok,
results
|> Ash.Actions.Sort.runtime_sort(sort)
|> then(fn data ->
if limit do
Enum.take(data, limit)
else
data
end
end)}
{:error, error} ->
{:error, error}
end
end
defp do_filter_matches(data, filter, api) do
Ash.Filter.Runtime.filter_matches(api, data, filter)
end
@doc false

View file

@ -37,10 +37,25 @@ defmodule Ash.Filter.Runtime do
|> Enum.map(&path_to_load/1)
|> case do
[] ->
{:ok,
Enum.filter(records, fn record ->
matches?(nil, record, filter)
end)}
Enum.reduce_while(records, {:ok, []}, fn record, {:ok, records} ->
case matches(record, filter) do
{:ok, falsey} when falsey in [false, nil] ->
{:cont, {:ok, records}}
{:ok, _} ->
{:cont, {:ok, [record | records]}}
{:error, error} ->
{:halt, {:error, error}}
end
end)
|> case do
{:ok, records} ->
{:ok, Enum.reverse(records)}
other ->
other
end
need_to_load when not loaded? ->
case api.load(records, need_to_load) do
@ -85,12 +100,15 @@ defmodule Ash.Filter.Runtime do
{:load, loads} when not is_nil(api) ->
matches?(api, api.load!(record, loads), filter)
{:error, _} ->
false
{:load, _} ->
false
end
end
def matches(record, expression) do
def matches(record, expression, load_with \\ nil) do
relationship_paths =
expression
|> Ash.Filter.relationship_paths(true)
@ -116,7 +134,11 @@ defmodule Ash.Filter.Runtime do
end)
need_to_load ->
{:load, Enum.map(need_to_load, &path_to_load/1)}
if load_with do
matches(load_with.load!(record, need_to_load), expression, load_with)
else
{:load, Enum.map(need_to_load, &path_to_load/1)}
end
end
end

27
logos/ascii-art.ans Normal file
View file

@ -0,0 +1,27 @@
                                        (                                      
                                      *(((                                     
                                     (((((((                                   
                                   ((((((((((                                  
                                  (((((((((((((                                
                                ((((((((((((((((/                              
                               (((((((((((((((((((                             
                              ((((((((((((((((((((((                           
                            (##((((((((((((((((((((((                          
                          #(###### ((((((((((((((((((((                        
                        (# ##########(((((((((((((((((((.                      
                       ## ############# ((((((((((((((((((                     
                     ###*#################,((((((((((((((((,                   
                    ######################## ((((((((((((((((                  
                  ######################### ##  (((((((((((((((                
                ,#####(##################.#####/##((((((((((((((               
               ######.################# #########(## (((((((((((((             
             ####### ################ ############ ####*(((((((((((            
            #######.############### ##################### (((((((((((          
          #######################,#*((/,#############.######*(((((((((*        
        .###################### ###((((((( ###########.####### (((((((((       
       ######################.####/((((((((((( ################## (((((((*     
     /##########.##########/###### ((((((((((((((/####### #########/(((((((    
    ########### #########,######## (((((((((((((((((( ################ ((((((  
  ############ ########,##########.(((((((((((((((((((((*%##,############/((((,
 ############ #######(############((((((((((((((((((((((((((  ############# ((((