Ash FrameworkAF
Ash Framework3y ago
5 replies
Dirigible

Filter by struct keys in a read action

So I have an ash actions.
It receives a struct as an input.
I want to create a filter that filters by values inside the struct.

here's a simplified version of the action:

read :get_corresponding_price do
  argument :instance, :struct do
    allow_nil? false
    contraints [instance_of: Instance]
  end

  get? true

  filter expr(instance_type == ^arg(:instance).machine_type)
end


I get an error like this:

== Compilation error in file lib/my_project/cloud/instance_price.ex ==
** (KeyError) key :machine_type not found in: {:_arg, :instance}. If you are using the dot syntax, such as map.field, make sure the left-hand side of the dot is a map
    lib/my_project/cloud/instance_price.ex:146: (module)


Is there a way to get the key out within a filter like this, or will I need to filter the data in a different way?
Is the only way of doing this through a preparation?
Was this page helpful?