gpt4 book ai didi

elixir - 覆盖毒物编码器

转载 作者:行者123 更新时间:2023-12-02 08:09:11 26 4
gpt4 key购买 nike

我正在尝试使用 Distillery 创建 Phoenix 应用程序的版本,并且我已覆盖 DateTimeNaiveDateTime 的 Poison 编码器以符合 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

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

编辑:这是我拥有的编码器:

  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

最佳答案

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

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

如果您不熟悉协议(protocol),请发布您的自定义编码器,我们可以帮助您完成协议(protocol)。

编辑:

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

已经有 this issue公开反对这个问题。

关于elixir - 覆盖毒物编码器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43552848/

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