Arc.Ecto.changeset的Typespecs

时间:2018-03-27 11:05:04

标签: elixir dialyzer

我正在复制Arc.Ecto changeset示例https://github.com/stavro/arc_ecto 而且我不确定typepecs,我正在尝试使用这些,但似乎不适用于透析器,它在|> 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是一个宏,因此我不确定如何为宏定义typespec,因为它的源代码没有定义任何规范https://github.com/stavro/arc_ecto/blob/master/lib/arc_ecto/schema.ex

1 个答案:

答案 0 :(得分:2)

该错误是由检查changeset_or_data类型的case语句引起的。由于它是一个宏,当透析器在编译后对其进行检查时,changeset_or_data已在调用模块中定义,因此透析器知道它是什么类型,因此永远无法到达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函数

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

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