Fl4m3Ph03n1x
Gradient does not recognize type of TypedStruct structures
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)
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.
Is there a fix for this?
(other than removing typed struct?)
Popular in Questions
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database.
Dep...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
As a follow up to my earlier question:
I have the code compiling and running but not getting a successful login from the rest server. ...
New
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Other popular topics
Manning 2016 Halloween weekend sale via Deal of the Day
Friday, October 28 - Half off all MEAPs - code WM102816LT
Saturday, October 29 ...
New
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New








