fix: revert :wrap_list usage for topic in PubSub, needs recursion (#702)

This commit is contained in:
Dmitry Maganov 2023-09-22 13:52:53 +03:00 committed by GitHub
parent 2813b3c9b2
commit 1aa0c9c84c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 6 deletions

View file

@ -70,7 +70,7 @@ publish :assign, "assigned"
| Name | Type | Default | Docs |
| --- | --- | --- | --- |
| `action`* | `atom` | | The name of the action that should be published |
| `topic`* | `list(String.t \| atom) \| String.t \| atom` | | The topic to publish |
| `topic`* | ``any`` | | The topic to publish |
### Options
| Name | Type | Default | Docs |
| --- | --- | --- | --- |
@ -110,7 +110,7 @@ publish_all :create, "created"
| Name | Type | Default | Docs |
| --- | --- | --- | --- |
| `type` | `:create \| :update \| :destroy` | | Publish on all actions of a given type |
| `topic`* | `list(String.t \| atom) \| String.t \| atom` | | The topic to publish |
| `topic`* | ``any`` | | The topic to publish |
### Options
| Name | Type | Default | Docs |
| --- | --- | --- | --- |

View file

@ -16,7 +16,7 @@ defmodule Ash.Notifier.PubSub.Publication do
required: true
],
topic: [
type: {:wrap_list, {:or, [:string, :atom]}},
type: {:custom, __MODULE__, :topic, []},
doc: "The topic to publish",
required: true
],
@ -40,4 +40,35 @@ defmodule Ash.Notifier.PubSub.Publication do
def schema, do: @schema
def publish_all_schema, do: @publish_all_schema
@doc false
def topic(topic) when is_binary(topic) do
{:ok, [topic]}
end
def topic(topic) when is_list(topic) do
if nested_list_of_binaries_or_atoms?(topic) do
{:ok, topic}
else
{:error,
"Expected topic to be a string or a list of strings or attribute names (as atoms), got: #{inspect(topic)}"}
end
end
def topic(other) do
{:error,
"Expected topic to be a string or a list of strings or attribute names (as atoms), got: #{inspect(other)}"}
end
defp nested_list_of_binaries_or_atoms?(list) when is_list(list) do
Enum.all?(list, &nested_list_of_binaries_or_atoms?/1)
end
defp nested_list_of_binaries_or_atoms?(value) when is_binary(value) or is_atom(value) do
true
end
defp nested_list_of_binaries_or_atoms?(_) do
false
end
end

View file

@ -115,14 +115,13 @@ defmodule Ash.Resource.Validation do
end
@doc false
def transform(%{validation: {module, opts}, where: where} = validation) do
def transform(%{validation: {module, opts}} = validation) do
{:ok,
%{
validation
| validation: {module, opts},
module: module,
opts: opts,
where: List.wrap(where)
opts: opts
}}
end