我有这个JSON模块
defmodule EvercamMedia.Types.JSON do
@behaviour Ecto.Type
def type, do: :json
def cast(term)
when is_map(term)
or is_binary(term)
or is_list(term)
or is_number(term),
do: {:ok, term}
def cast(_), do: :error
def load(term), do: {:ok, term}
def dump(term), do: {:ok, term}
end
defmodule EvercamMedia.Types.JSON.Extension do
alias Postgrex.TypeInfo
@behaviour Postgrex.Extension
def init(_parameters, opts),
do: Keyword.fetch!(opts, :library)
def matching(_library),
do: [type: "json", type: "jsonb"]
def format(_library),
do: :binary
def encode(%TypeInfo{type: "json"}, map, _state, library),
do: library.encode!(map)
def encode(%TypeInfo{type: "jsonb"}, map, _state, library),
do: <<1, library.encode!(map)::binary>>
def decode(%TypeInfo{type: "json"}, json, _state, library),
do: library.decode!(json)
def decode(%TypeInfo{type: "jsonb"}, <<1, json::binary>>, _state, library),
do: library.decode!(json)
end
我收到很多警告
warning: undefined behaviour function decode/1 (for behaviour Postgrex.Extension)
lib/evercam_media/json.ex:20
warning: undefined behaviour function encode/1 (for behaviour Postgrex.Extension)
lib/evercam_media/json.ex:20
warning: undefined behaviour function init/1 (for behaviour Postgrex.Extension
我们正在使用它来处理JSON。我只是想删除那些警告。我知道这是过时的postgrex版本,但我只想摆脱这些警告。
答案 0 :(得分:0)
按Postgrex.Extension
行为decode/1
(以及encode/1
和init/1
定义请求。)如果您不打算使用它,请执行以下操作:
def decode(_), do: :ok
def encode(_), do: :ok
def init(_), do: :ok
这里的问题是@behaviour
声明了许多回调,对于任何实现,这都是合同。除非实现实现所有回调,否则Elixir编译器会发出警告。