gpt4 book ai didi

elixir - Arc.Ecto.changeset 的类型规范

转载 作者:行者123 更新时间:2023-12-04 16:04:03 30 4
gpt4 key购买 nike

我正在复制 Arc.Ecto changeset 示例 https://github.com/stavro/arc_ecto我不确定类型规范,我正在尝试使用这些但似乎不适用于透析器,它在 |> cast_attachments(params, [:avatar])

上提示
@spec changeset(Ecto.Schema.t, map | :invalid) :: Changeset.t
def changeset(user, params \\ :invalid) do
user
|> cast(params, [:name])
|> cast_attachments(params, [:avatar])
|> validate_required([:name, :avatar])
end


> The pattern #{'__meta__':=_} can never match the type
> #{'__struct__':='Elixir.Ecto.Changeset', 'action':='delete' | 'insert' | 'nil' | 'replace' | 'update', 'changes':=#{atom()=>_},
> 'constraints':=[#{'constraint':=binary(), 'field':=atom(),
> 'match':='exact' | 'suffix', 'message':={binary(),[{atom(),_}]},
> 'type':='unique'}], 'data':='nil' | map(), 'empty_values':=_,
> 'errors':=[{atom(),{binary(),[{atom(),_}]}}], 'filters':=#{atom()=>_},
> 'params':='nil' | #{binary()=>_},
> 'prepare':=[fun((#{'__struct__':='Elixir.Ecto.Changeset',
> 'action':='delete' | 'insert' | 'nil' | 'replace' | 'update',
> 'changes':=map(), 'constraints':=[any()], _=>_}) ->
> #{'__struct__':='Elixir.Ecto.Changeset', 'action':='delete' | 'insert' | 'nil' | 'replace' | 'update', 'changes':=map(),
> 'constraints':=[any()], _=>_})], 'repo':=atom(), 'required':=[atom()],
> 'types':='nil' | #{atom()=>atom() | {'array',_} |
> {'embed',#{'__struct__':='Elixir.Ecto.Embedded', 'cardinality':='many'
> | 'one', 'field':=atom(), 'on_cast':='nil' | fun(),
> 'on_replace':='delete' | 'mark_as_invalid' | 'raise', 'owner':=atom(),
> 'related':=atom(), 'unique':=boolean()}} | {'in',_} | {'map',_}},
> 'valid?':=boolean(), 'validations':=[{atom(),_}]}

cast_attachments 是一个宏,所以我不确定如何为宏定义类型规范,因为在它的源代码中没有定义任何规范 https://github.com/stavro/arc_ecto/blob/master/lib/arc_ecto/schema.ex

最佳答案

错误是由检查 changeset_or_data 类型的 case 语句引起的。由于是宏,dialyzer编译后检查时,changeset_or_data已经定义在调用模块中,所以dialyzer知道它是什么类型,所以永远无法到达其中一个case语句子句。这就是我的理解方式……无论如何,将 case 语句中的逻辑移到函数中似乎可以解决这个问题:

  defmacro cast_attachments(changeset_or_data, params, allowed, options \\ []) do
quote bind_quoted: [changeset_or_data: changeset_or_data,
params: params,
allowed: allowed,
options: options] do

# If given a changeset, apply the changes to obtain the underlying data

scope = do_apply_changes(changeset_or_data)

# Cast supports both atom and string keys, ensure we're matching on both.
allowed_param_keys = Enum.map(allowed, fn key ->
case key do
key when is_binary(key) -> key
key when is_atom(key) -> Atom.to_string(key)
end
end)

arc_params = case params do
:invalid ->
:invalid
%{} ->
params
|> Arc.Ecto.Schema.convert_params_to_binary
|> Map.take(allowed_param_keys)
|> Enum.reduce([], fn
# Don't wrap nil casts in the scope object
{field, nil}, fields -> [{field, nil} | fields]

# Allow casting Plug.Uploads
{field, upload = %{__struct__: Plug.Upload}}, fields -> [{field, {upload, scope}} | fields]

# If casting a binary (path), ensure we've explicitly allowed paths
{field, path}, fields when is_binary(path) ->
if Keyword.get(options, :allow_paths, false) do
[{field, {path, scope}} | fields]
else
fields
end
end)
|> Enum.into(%{})
end

cast(changeset_or_data, arc_params, allowed)
end
end

def do_apply_changes(%Ecto.Changeset{} = changeset), do: Ecto.Changeset.apply_changes(changeset)
def do_apply_changes(%{__meta__: _} = data), do: data

注意最后的新 do_apply_changes 函数

这里有一个带有修复的 fork - https://github.com/madebymany/arc_ecto

我已提交拉取请求以将其合并到 - https://github.com/stavro/arc_ecto/pull/106

关于elixir - Arc.Ecto.changeset 的类型规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49511255/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com