覆盖毒药编码器

时间:2017-04-21 22:20:39

标签: elixir elixir-poison

我尝试使用Distillery创建我的Phoenix应用程序版本,并且我已覆盖DateTimeNaiveDateTime的Poison编码器以符合API要求。

当我运行mix release时,我的应用程序会编译,但在.boot生成期间出错。

这是stacktrace:

$> mix release
warning: variable "aliases" does not exist and is being expanded to "aliases()", please use parentheses to remove the ambiguity or change the variable name
  mix.exs:12

warning: variable "deps" does not exist and is being expanded to "deps()", please use parentheses to remove the ambiguity or change the variable name
  mix.exs:13

==> Assembling release..
==> Building release helios:1.0.0 using environment dev
==> One or more direct or transitive dependencies are missing from
    :applications or :included_applications, they will not be included
    in the release:

    :ex_admin
    :floki
    :geo
    :guardian
    :json_web_token
    :mogrify
    :phoenix_pubsub
    :scrivener_ecto
    :timex
    :timex_ecto

    This can cause your application to fail at runtime. If you are sure
    that this is not an issue, you may ignore this warning.

==> Release failed, during .boot generation:
        Duplicated modules:
        'Elixir.Poison.Encoder.NaiveDateTime' specified in poison and helios
        'Elixir.Poison.Encoder.Ecto.DateTime' specified in ecto and helios

有没有办法覆盖毒药编码器而不会遇到这个问题?

编辑: 以下是我的编码器:

  defimpl Poison.Encoder, for: Ecto.DateTime do
    def encode(datetime, options) do

      dt = datetime |> Ecto.DateTime.to_erl
      |> Timex.Timezone.convert("UTC")
      |> Timex.format("{ISO:Extended}")
      |> elem(1)

      <<?", dt::binary, ?">>
    end
  end

  defimpl Poison.Encoder, for: NaiveDateTime do
    def encode(datetime, options) do

      dt = datetime
      |> Timex.Timezone.convert("UTC")
      |> Timex.format("{ISO:Extended}")
      |> elem(1)

      <<?", dt::binary, ?">>
    end
  end

1 个答案:

答案 0 :(得分:2)

您可能希望实施该协议。文档给出了这个例子:

defimpl Poison.Encoder, for: Person do
  def encode(%{name: name, age: age}, options) do
    Poison.Encoder.BitString.encode("#{name} (#{age})", options)
  end
end

如果您不熟悉协议,请发布您的自定义编码器,我们可以帮助您使用协议。

编辑:

因此,经过大量挖掘后,Poison似乎不允许覆盖基类型。这些已经在包中实现了。因此,当您在项目中覆盖它们时,您将创建两个版本的梁文件。

此问题已经this issue开放。