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
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)
== 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?
2 Replies
dblack
dblack•2y ago
probably just a typo in your post, but should argument :instance, struct be argument :instance, :struct? ie :struct atom for the type you may be able to use get_path on arguments, so your filter would look something like: filter expr(instance_type == get_path(^arg(:instance), [:machine_type])) there was some discussion over here: https://discord.com/channels/711271361523351632/799097774523547669/1034088336576491575
Dirigible
DirigibleOP•2y ago
cheers for the link, looks like at the time the discussion was had zac said you basically need to use a preparation, so that's what I've done! 🙂

Did you find this page helpful?