diff --git a/examples/simple_app/lib/simple_app/typed_struct_example.ex b/examples/simple_app/lib/simple_app/typed_struct_example.ex new file mode 100644 index 00000000..91f91bcd --- /dev/null +++ b/examples/simple_app/lib/simple_app/typed_struct_example.ex @@ -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 diff --git a/examples/simple_app/mix.exs b/examples/simple_app/mix.exs index 4a472d38..15b3a423 100644 --- a/examples/simple_app/mix.exs +++ b/examples/simple_app/mix.exs @@ -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 diff --git a/examples/simple_app/mix.lock b/examples/simple_app/mix.lock index fa63b4f3..10c25044 100644 --- a/examples/simple_app/mix.lock +++ b/examples/simple_app/mix.lock @@ -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"}, }