Hi everyone,
Miss Elixir library brings in a non-intrusive way some extra functions that, for different reasons, are not part of the Elixir standard library.
The motivation to create this library and some examples are detailed in this blog post.
Just an example of one of the functions available
Given the following Post
struct with author and comments:
defmodule Post do
defstruct [:title, :text, :date, :author, comments: []]
end
defmodule Author do
defstruct [:id, :name]
end
defmodule Comment do
defstruct [:text]
end
post = %Post{
title: "My post",
text: "Something really interesting",
date: ~D[2010-09-01],
author: %Author{
id: 1234,
name: "Pedro Bonamides"
},
comments: [
%Comment{text: "Comment one"},
%Comment{text: "Comment two"}
]
}
Using Map.from_struct/1
only the root struct will be converted:
iex> Map.from_struct(post)
%{
title: "My post",
text: "Something really interesting",
date: ~D[2010-09-01],
author: %Author{
id: 1234,
name: "Pedro Bonamides"
},
comments: [
%Comment{text: "Comment one"},
%Comment{text: "Comment two"}
]
}
Using Miss.Map.from_nested_struct/2
all the nested structs are converted:
iex> Miss.Map.from_nested_struct(post, [{Date, :skip}])
%{
title: "My post",
text: "Something really interesting",
date: ~D[2010-09-01],
author: %{
id: 1234,
name: "Pedro Bonamides"
},
comments: [
%{text: "Comment one"},
%{text: "Comment two"}
]
}
Check out the source code at:
And the full documentation is at:
https://hexdocs.pm/miss
Suggestions, comments, contributions, or any kind of feedback are welcome.