https://github.com/chugunov/to_struct
Transforms map to struct
https://github.com/chugunov/to_struct
elixir map struct
Last synced: about 1 year ago
JSON representation
Transforms map to struct
- Host: GitHub
- URL: https://github.com/chugunov/to_struct
- Owner: chugunov
- License: mit
- Created: 2018-03-05T08:25:05.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-03-15T15:49:32.000Z (over 8 years ago)
- Last Synced: 2024-04-24T15:09:55.029Z (about 2 years ago)
- Topics: elixir, map, struct
- Language: Elixir
- Homepage:
- Size: 22.5 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# ToStruct
[](https://travis-ci.org/chugunov/to_struct)
[](https://hex.pm/packages/to_struct)
Package helps you transform maps (ex. external data) to structs (internal data).
## Example
Define a structs:
```elixir
defmodule Product do
defstruct name: nil
end
defmodule Cart do
defstruct id: nil,
products: nil
end
```
Define a transformation schema and then transform a map:
```elixir
def to_struct() do
map = %{
"id" => 1,
"products" => [
%{
"name" => "milk"
},
%{
"name" => "potato",
}
]
}
schema = %Cart{
products: [%Product{}],
}
ToStruct.transform(schema, map)
end
```
Result:
```elixir
%Cart{
id: 1,
products: [
%Product{name: "milk"},
%Product{name: "potato"}
]
}
```
## Installation
```elixir
def deps do
[
{:to_struct, "~> 0.1.0"}
]
end
```