I noticed that when creating a generic action that should return the same resource inside an array fails to compile.
For example, I have this generic function:
action :search, {:array, SkipTraceEntity} do
argument :first_name, :string, allow_nil?: false
argument :last_name, :string, allow_nil?: false
run &Actions.Search.run/2
end
This action is defined inside the SkipTraceEntity resource.
This will give the following compile error:
== Compilation error in file lib/pacman/markets/skip_trace_entity.ex ==
** (RuntimeError) {:array, Pacman.Markets.SkipTraceEntity} is not a valid type.
If I change SkipTraceEntity with any other resource, it works.
That’s correct. This is because the struct is still being compiled at the time the DSL is being evaluated. The work around is to use a :struct type with an :instance_of constraint. Eg:
action :search, {:array, :struct} do
constraints instance_of: SkipTraceEntity
argument :first_name, :string, allow_nil?: false
argument :last_name, :string, allow_nil?: false
run &Actions.Search.run/2
end
Oh shit, you’re right. It’s an array of structs. Looks like Zach thought of this case and you can do constraints items: [instance_of: SkipTraceEntity].