Ash FrameworkAF
Ash Framework8mo ago
13 replies
sevenseacat

Using struct data in filter expressions

I think I've tried to do this a few times and have never gotten it to work properly.

I have a calculation that takes a struct as an argument, and I want to use data from that struct in the expression, eg.

    calculate :valid_for_npc_and_topic,
              :boolean,
              expr(
                topic_id == ^arg(:topic) &&
                  (is_nil(speaker_faction_id) || speaker_faction_id == ^get_path(arg(:npc), :faction_id))
              ) do
      argument :topic, :string, allow_nil?: false
      argument :npc, :struct, allow_nil?: false, constraints: [instance_of: Resdayn.Codex.World.NPC]
    end

This doesn't error, but generates SQL like ash_elixir_or((d0."speaker_faction_id"::text IS NULL)::boolean, (d0."speaker_faction_id"::text::text = NULL::text)::boolean)::boolean)::boolean) (so the second part isn't working properly).

It's probably something simple but I've tried lots of things like ^arg(:npc)[:id], ^ref([arg(:npc)], :id), and nothing else will even compile
Solution
instead of ^get_path(arg(:npc), :faction_id) it should be get_path(^arg(:npc), :faction_id)
Was this page helpful?