Skip to content

Commit

Permalink
Typed struct example
Browse files Browse the repository at this point in the history
Investigating #165
  • Loading branch information
erszcz committed Mar 28, 2023
1 parent 743c144 commit 9e33aa8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
28 changes: 28 additions & 0 deletions examples/simple_app/lib/simple_app/typed_struct_example.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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
6 changes: 5 additions & 1 deletion examples/simple_app/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ defmodule SimpleApp.MixProject do

# Run "mix help deps" to learn about dependencies.
defp deps do
[{:gradient, path: "../../"}]
[
{:jason, "~> 1.4"},
{:typed_struct, "~> 0.3.0"},
{:gradient, path: "../../"}
]
end
end
5 changes: 4 additions & 1 deletion examples/simple_app/mix.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
%{
"gradualizer": {:git, "https://github.com/josefs/Gradualizer.git", "6e89b4e1cd489637a848cc5ca55058c8a241bf7d", [ref: "6e89b4e"]},
"gradient_macros": {:git, "https://github.com/esl/gradient_macros.git", "bf5a066a6abd90cc63281d7ccb8eb7ac0fed0e64", [ref: "bf5a066"]},
"gradualizer": {:git, "https://github.com/josefs/Gradualizer.git", "1498d1792155010950c86dc3e92ccb111b706e80", [ref: "1498d17"]},
"jason": {:hex, :jason, "1.4.0", "e855647bc964a44e2f67df589ccf49105ae039d4179db7f6271dfd3843dc27e6", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "79a3791085b2a0f743ca04cec0f7be26443738779d09302e01318f97bdb82121"},
"typed_struct": {:hex, :typed_struct, "0.3.0", "939789e3c1dca39d7170c87f729127469d1315dcf99fee8e152bb774b17e7ff7", [:mix], [], "hexpm", "c50bd5c3a61fe4e198a8504f939be3d3c85903b382bde4865579bc23111d1b6d"},
}

0 comments on commit 9e33aa8

Please sign in to comment.