Fl4m3Ph03n1x

Fl4m3Ph03n1x

Gradient does not recognize type of TypedStruct structures

Background

I have a module that uses TypedStruct to create structs. This is the code:

defmodule Shared.Data.Authorization do
  @moduledoc """
  Saves authorization details for a user. It also contains other details.
  """

  use TypedStruct

  alias Shared.Utils.Structs

  @type authorization :: %{
          (cookie :: String.t()) => String.t(),
          (token :: String.t()) => String.t()
        }

  @derive Jason.Encoder
  typedstruct enforce: true do
    @typedoc "Authorization information for a user"

    field(:cookie, String.t())
    field(:token, String.t())
  end

  @spec new(authorization()) :: __MODULE__.t()
  def new(%{"cookie" => cookie, "token" => token} = auth)
      when is_binary(cookie) and is_binary(token) do
    Structs.string_map_to_struct(auth, __MODULE__)
  end
end

The problem here is that Gradient does not seem to understand that t() is created, so it errors out:

 lib/data/authorization.ex: The function call on line 26 is expected to have type t() but it has type struct()
 24   def new(%{"cookie" => cookie, "token" => token} = auth)
 25       when is_binary(cookie) and is_binary(token) do
 26     Structs.string_map_to_struct(auth, __MODULE__)
 27   end

For additional context, here is the string_map_to_struct code:

  @spec string_map_to_struct(map, module | struct) :: struct
  def string_map_to_struct(data, target_struct) do
    data
    |> Morphix.atomorphiform!() # string_map to atom_map
    |> data_to_struct(target_struct)
  end

  @spec data_to_struct(Enumerable.t(), module | struct) :: struct
  def data_to_struct(data, target_struct), do: struct(target_struct, data)

Temporary fix

I decided to convert that code into its native form using defstruct:

defmodule Shared.Data.Authorization do
  @moduledoc """
  Saves authorization details for a user. It also contains other details.
  """

  alias Shared.Utils.Structs

  @enforce_keys [:cookie, :token]
  defstruct [:cookie, :token]

  @type authorization :: %{
          (cookie :: String.t()) => String.t(),
          (token :: String.t()) => String.t()
        }

  @typedoc "Authorization information for a user"
  @type t() :: %__MODULE__{
          cookie: String.t(),
          token: String.t()
        }

  @spec new(authorization()) :: t()
  def new(%{"cookie" => cookie, "token" => token} = auth)
      when is_binary(cookie) and is_binary(token) do
    Structs.string_map_to_struct(auth, __MODULE__)
  end
end

Gradient does not complain here.

Question

Is there a fix for this?
(other than removing typed struct?)

Most Liked

mafinar

mafinar

It’s cool that you also raised issue here: Gradient does not recognize type of TypedStruct structures · Issue #165 · esl/gradient · GitHub I am adding this as comment so that anyone coming to this post can go and follow the progress there. I myself am interested in this one. Thanks.

Fl4m3Ph03n1x

Fl4m3Ph03n1x

I usually update both threads if/when the issue is solved, so I got you covered there :stuck_out_tongue:

Popular Backend topics Top

andrea
Can Phoenix LiveView be used in multi-page applications, unlike React/Vue/Blazor which seems to be targeted for SPA?
New
joshi
Hey everybody! I’m working on the project that includes import of Oracle data to PostgreSQL. That data comes as Oracle export (expdp) fi...
New
GermaVinsmoke
Reading Programming Elixir 1.6 book, I’ve completed part 1 of the book. Now I’m thinking of reading Elixir in Action. What do you all sug...
New
sampu
I have a use case where a client is invoking a Rest endpoint via a load balancer, which in turn invokes a third party endpoint which is r...
New
Fl4m3Ph03n1x
Background I am trying to encode a structure into json format using the Jason library. However, this is not working as expected. Code L...
New
GermaVinsmoke
Does anyone know beginner friendly Elixir/Phoenix Open source projects? For learning purpose :slightly_smiling_face:
New
harwind
I received this error for a binary search programme in C, despite the fact that it requested for inputs and produced the right output. Th...
/c
New
pillaiindu
Currently reading the book “Programming Phoenix LiveView”. At the end of the Chapter 1, I’m trying to solve the guess game. If the user ...
New
Fl4m3Ph03n1x
Background I have a release file inside a tarball. However I want the final release to have some additional files and to move things aro...
New
Fl4m3Ph03n1x
Background I have an umbrella project, where I run mix test from the root. In one of the apps, I am mocking the File module using the Mo...
New

Other popular topics Top

Devtalk
Hello Devtalk World! Please let us know a little about who you are and where you’re from :nerd_face:
New
malloryerik
Any thoughts on Svelte? Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue...
New
wolf4earth
@AstonJ prompted me to open this topic after I mentioned in the lockdown thread how I started to do a lot more for my fitness. https://f...
New
AstonJ
Or looking forward to? :nerd_face:
New
Exadra37
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New
siddhant3030
I’m thinking of buying a monitor that I can rotate to use as a vertical monitor? Also, I want to know if someone is using it for program...
New
AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
Exadra37
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New
Maartz
Hi folks, I don’t know if I saw this here but, here’s a new programming language, called Roc Reminds me a bit of Elm and thus Haskell. ...
New
AstonJ
Curious what kind of results others are getting, I think actually prefer the 7B model to the 32B model, not only is it faster but the qua...
New