# How to convert a typed definition to a typedstruct?

**URL:** <https://forum.devtalk.com/t/how-to-convert-a-typed-definition-to-a-typedstruct/93291>\
**Category:** Backend Questions/Help\
**Tags:** elixir, types\
**Created:** [24 January 2023 11:06 UTC](https://forum.devtalk.com/t/how-to-convert-a-typed-definition-to-a-typedstruct/93291 "2023-01-24T11:06:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fl4m3Ph03n1x](https://forum.devtalk.com/user_avatar/forum.devtalk.com/fl4m3ph03n1x/32/3808_2.png) [@Fl4m3Ph03n1x](https://forum.devtalk.com/u/Fl4m3Ph03n1x)\
**Post date:** [24 January 2023 11:06 UTC](https://forum.devtalk.com/t/how-to-convert-a-typed-definition-to-a-typedstruct/93291/1 "2023-01-24T11:06:20Z")

</div>

## Background

I am moving towards defined data structures in my application, and I find that [TypedStruct](https://github.com/ejpcmac/typed_struct) is quite useful.

## Questions

However, I have recently found a little hickup. I don’t know how to convert this definition into a typedstruct:

```auto
  @type order_info :: %{
    (visible :: String.t()) => boolean,
    (order_type :: String.t()) => String.t(),
    (platform :: String.t()) => String.t(),
    (platinum :: String.t()) => non_neg_integer,
    (user :: String.t()) => %{
      (ingame_name :: String.t()) => String.t(),
      (status :: String.t()) => String.t()
    }
  }

```

My main issue here is the `user` map. Aside from the fact I don’t know how to define a typedstruct inside a trypedstruct, how do you folks do this?

1. Do you simply define a map inside the `order_info` typedstruct and ignore the mandatory parameters for the `user` map?
2. Do you create another typedstruct (called `user`) that goes inside the typedsctruct called `order_info`?
3. What is the standard option in Elixir ?

---

<div class="post-metadata">

**Author:** ![Fl4m3Ph03n1x](https://forum.devtalk.com/user_avatar/forum.devtalk.com/fl4m3ph03n1x/32/3808_2.png) [@Fl4m3Ph03n1x](https://forum.devtalk.com/u/Fl4m3Ph03n1x)\
**Post date:** [26 January 2023 09:05 UTC](https://forum.devtalk.com/t/how-to-convert-a-typed-definition-to-a-typedstruct/93291/3 "2023-01-26T09:05:05Z")

</div>

## Answer

After talking with other folks at the community I have opted for separating the User into a struct, in its own module, inside an `OrderInfo` directory:

```auto
lib/
  order_info/
    user.ex
  order_info.ex

```

I personally find this works great for readability. So for readability purposes this is what I ended up with:

```auto
defmodule AuctionHouse.Data.OrderInfo.User do
  @moduledoc """
  Represents the account information for a User.
  """

  use TypedStruct

  alias AuctionHouse.Shared.Utils

  @type user :: %{
          (ingame_name :: String.t()) => String.t(),
          (status :: String.t()) => String.t()
        }

  typedstruct enforce: true do
    @typedoc "Account information of an User"

    field(:ingame_name, String.t())
    field(:status, String.t())
  end

  @spec new(user) :: __MODULE__.t()
  def new(
        %{
          "ingame_name" => ingame_name,
          "status" => status
        } = user
      )
      when is_binary(ingame_name) and is_binary(status),
      do: Utils.string_map_to_struct(user, __MODULE__ )
end

```

An then `OrderInfo` is basically the same, as seen by this typedstruct definition:

```auto
  typedstruct enforce: true do
    @typedoc "Information about an order"

    field(:visible, boolean())
    field(:order_type, String.t())
    field(:platform, String.t())
    field(:platinum, non_neg_integer())
    field(:user, __MODULE__.User.t())
  end

```

## Extra

One thing to mention here, is that `struct` will not convert string maps to structures, only atom maps. So in order to achieve my goal of converting string maps to structures, I created this little helper function:

```auto
defmodule AuctionHouse.Shared.Utils do
  @moduledoc """
  Set of functions used across the app, for utility purposes, like dealing with
  tuples, maps and other data structures.
  """

  alias Morphix

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

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

```

Here I used [Morphix](https://github.com/philosodad/morphix) to do the hard work, but this can also be achieved by other means, see:

> <https://stackoverflow.com/questions/31990134/how-to-convert-map-keys-from-strings-to-atoms-in-elixir>

Many thanks to all of the people who helped me get this far, from other communities as well:

- [How to convert a typed definition to a typedstruct? - #5 by xpg - Questions / Help - Elixir Programming Language Forum](https://elixirforum.com/t/how-to-convert-a-typed-definition-to-a-typedstruct/53386/5?u=fl4m3ph03n1x)
- [https://stackoverflow.com/a/75221143/1337392](https://stackoverflow.com/a/75221143/1337392)
