Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/coingaming/castable
https://github.com/coingaming/castable
Last synced: about 15 hours ago
JSON representation
- Host: GitHub
- URL: https://github.com/coingaming/castable
- Owner: coingaming
- Created: 2021-11-16T18:46:31.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2021-11-18T18:31:04.000Z (about 3 years ago)
- Last Synced: 2024-10-29T08:40:49.335Z (22 days ago)
- Language: Elixir
- Size: 7.81 KB
- Stars: 0
- Watchers: 6
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Castable
Protocol to aid casting unknown values to known value.
Example:
```elixir
defimpl Castable, for: MyStruct do
def cast(_to, %{"foo" => foo}, _opts) when is_bitstring(foo) do
%MyStruct{foo: foo}
enddef cast(_to, foo, _opts) when is_bitstring(foo) do
%MyStruct{foo: foo}
enddef cast(_to, _from, _opts) do
:error
end
endiex> Castable.cast(%MyStruct{}, %{"foo" => "bar"})
%MyStruct{foo: "bar"}iex> Castable.cast(%MyStruct{}, "bar")
%MyStruct{foo: "bar"}iex> Castable.cast(%MyStruct{}, 123)
:error
```## Installation
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `castable` to your list of dependencies in `mix.exs`:```elixir
def deps do
[
{:castable, "~> 0.1.1"}
]
end
```Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at [https://hexdocs.pm/castable](https://hexdocs.pm/castable).