Creating Structs

We had a pleasant surprise when we built this API. We’d accidentally built in support for structs! Since an Elixir struct is just a map with the extra __struct__ key, just adding the key __struct__: User to the existing User record would create users that satisfy a struct called User like this:

 ~~~
  register :user,
  __struct__: User,
  first_name: Faker.Name.first_name,
  last_name: Faker.Name.last_name,
  email: Sequence.next(:email, &"test#{&1}@example.com")
 ~~~

Get Functional Programming: A PragPub Anthology now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.