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

PragmaticBookshelf
Stop developing web apps with yesterday’s tools. Today, developers are increasingly adopting Clojure as a web-development platform. See f...
New
AstonJ
Currently a hot topic in the BEAM world, let’s start a thread for it (as suggested by @crowdhailer here) :smiley: What are your current...
New
ErlangSolutions
Hi this week it’s Code Mesh V when London’s leading alternative tech conference goes virtual for the first time. Talks are set for both U...
New
AstonJ
In case anyone else is wondering why Ruby 3 doesn’t show when you do asdf list-all ruby :man_facepalming: do this first: asdf plugin-upd...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
First poster: bot
A new PostgreSQL blog post/announcement has been posted! Get the full details here: PostgreSQL: Generate realistic test Data for Postgr...
New
DevotionGeo
What do you people do for reading Erlang docs? Is there something like Ruby’s ri? I installed manual pages for Erlang under /usr/local/...
New
TwistingTwists
Hello Folks, I am a novice developer from India. Intending to learn Elixir and web apps (phoenix framework). What are things that I MUS...
New
First poster: bot
Laravel v10.0.5 has been released. Link: Release v10.0.5 · laravel/laravel · GitHub
New
First poster: bot
Node.js v18.16.0 has been released. Link: Release 2023-04-12, Version 18.16.0 'Hydrogen' (LTS), @danielleadams · nodejs/node · GitHub
New

Other popular topics Top

Devtalk
Reading something? Working on something? Planning something? Changing jobs even!? If you’re up for sharing, please let us know what you’...
1016 16836 371
New
PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New
AstonJ
This looks like a stunning keycap set :orange_heart: A LEGENDARY KEYBOARD LIVES ON When you bought an Apple Macintosh computer in the e...
New
AstonJ
Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New
PragmaticBookshelf
A Hero’s Journey with Chris Pine @chrispine Chris Pine, author of Learn to Program, Third Edition, discusses his journey to beco...
New
AstonJ
Was just curious to see if any were around, found this one: I got 51/100: Not sure if it was meant to buy I am sure at times the b...
New
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New
husaindevelop
Inside our android webview app, we are trying to paste the copied content from another app eg (notes) using navigator.clipboard.readtext ...
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
New
AstonJ
This is cool! DEEPSEEK-V3 ON M4 MAC: BLAZING FAST INFERENCE ON APPLE SILICON We just witnessed something incredible: the largest open-s...
New