【问题标题】:Overriding Poison Encoders覆盖毒药编码器
【发布时间】:2017-09-19 01:18:53
【问题描述】:

我正在尝试使用 Distillery 创建我的 Phoenix 应用程序的版本,并且我已经覆盖了 DateTimeNaiveDateTime 的毒药编码器以匹配 API 要求。

当我运行 mix release 时,我的应用程序可以编译,但在生成 .boot 时出现错误。

这是堆栈跟踪:

$> 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

【问题讨论】:

    标签: elixir elixir-poison


    【解决方案1】:

    您可能想要实现该协议。文档给出了这个例子:

    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 公开反对这个问题。

    【讨论】:

    • 这是我目前所做的,但我收到 duplicated modules 错误。如果我覆盖 DateTimeNaiveDateTime 的编码器,Distilleryexrm 似乎都不喜欢它:/
    • 我猜你的项目叫做:helios。您收到的错误消息表明您的项目中有一个 Elixir.Poison.Encoder.NaiveDateTime 模块的副本本地。其他模块也一样。请检查以确保您的项目中没有这些模块。顺便说一句,defimpl 宏没有定义模块,它只实现协议。如果您没有自己的本地副本,那么我建议您尝试mix clean 并再次构建您的版本。你也可以试试mix release.clean
    • 我更新了帖子以包含我的自定义编码器。我尝试了mix cleanmix release.clean 并再次运行mix release,但又遇到了同样的错误。顺便说一句,感谢您的帮助,不胜感激!另外,喜欢你在 ExAdmin 上的工作,我们也使用它:)
    • 我能想到的只有两件事可以解决这个问题。 1,您在本地项目的某处有一个源文件,其中包含Poison.Encoder.NaiveDateTime 的模块定义,或者 2. 您有一个正在被混合发布拾取的梁文件。如果您已经在项目中为 Poison.Encoder.NaiveDateTime 执行了 grep,那么您将删除 deps_build 文件夹。然后运行mix do deps.get, compile
    • 所以当我在weblib 文件夹中进行grep 时,我没有Poison.Encoder.NaiveDateTime 的任何模块定义。但是,它们确实在我的_build 目录中以.beam 文件的形式出现在_build/dev/lib/helios_build/dev/lib/poison_build/dev/consolidated
    猜你喜欢
    • 1970-01-01
    • 2017-08-07
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 2011-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多