ahamez / protox

A fast, easy to use and 100% conformant Elixir library for Google Protocol Buffers (aka protobuf)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Inconsistency in generated functions specs?

jacek-adamek opened this issue · comments

Protox: v. 1.6.2

When I generate a module for a given protobuf message the code is decorated with functions specs. For example:

defmodule PB.Hello do
  ...

  @spec json_decode(iodata(), keyword()) :: {:ok, struct()} | {:error, any()}
  def(json_decode(input, opts \\ [])) do
    try do
      {:ok, json_decode!(input, opts)}
    rescue
      e in Protox.JsonDecodingError ->
        {:error, e}
    end
  end

  @spec json_decode!(iodata(), keyword()) :: iodata() | no_return()
  def(json_decode!(input, opts \\ [])) do
    {json_library_wrapper, json_library} = Protox.JsonLibrary.get_library(opts, :decode)

    Protox.JsonDecode.decode!(
      input,
      PB.Hello,
      &json_library_wrapper.decode!(json_library, &1)
    )
  end

  ...
end

Unfortunately it seems there's inconsistency between json_decode! and json_decode specs for their happy paths. The former one is to return iodata whereas the latter one struct. That's impossible taking into account how the result value is defined for json_decode:

 {:ok, json_decode!(input, opts)}

Shouldn't they both return {:ok, struct()} or struct respectively?

Good catch! I'll fix this ASAP. Thank you :-)