IvanR
The Nestru library can convert a map of any shape into a model of nested structs according to given hints. And It can convert any nested struct into a map.
The library’s primary purpose is to serialize between JSON map and an application’s model that is a struct having other structs nested in it.
It comes from the idea to have no DSL and less custom code to parse JSONs as an alternative to full-fledged Ecto with schemaless changesets.
Works good in Jason <-> Nestru pair.
There are several possible data validation options:
- with ex_json_schema applied to the map before passing to Nestru
- within Nestru
gather_fields_map/3andfrom_map_hint/3callbacks - with Ecto schemaless changesets applied to the struct
- with Domo applied to the struct to validate the struct conformance to its
@type t()and associated preconditions
See typical usage in Readme.md via:
Shortly it looks like:
defmodule Order do
@derive [
Nestru.Encoder,
{Nestru.Decoder, %{items: [LineItem], total: Total}}
]
# Gave a hint to Nestru on how to process the items and total fields
# others go to a struct as is.
defstruct [:id, :items, :total]
end
defmodule LineItem do
@derive [Nestru.Encoder, Nestru.Decoder]
defstruct [:amount]
end
defmodule Total do
@derive [Nestru.Encoder, Nestru.Decoder]
defstruct [:sum]
end
map = %{
"id" => "A548",
"items" => [%{"amount" => 150}, %{"amount" => 350}],
"total" => %{"sum" => 500}
}
{:ok, model} = Nestru.decode_from_map(map, Order)
returns:
{:ok,
%OrderA{
id: "A548",
items: [%LineItemA{amount: 150}, %LineItemA{amount: 350}],
total: %Total{sum: 500}
}}
And going back to the map is as simple as that:
map = Nestru.encode_to_map(model)
returns:
%{
id: "A548",
items: [%{amount: 150}, %{amount: 350}],
total: %{sum: 500}
}
More information here:
Trending in Announcing
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub.
Docs are at OpenaiEx User Gu...
New
WebAuthnLiveComponent WebAuthnComponents
See this post about renaming the package.
Passwordless authentication for Phoenix LiveView app...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New
Aloha gang,
There’s a new class of AI model out from TypeSafe. Jev is their flagship model. You can learn about it here:
...
New
Other Trending Topics
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
What IDE or editor are you using for Elixir development?
Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #performance










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
IvanR
0.1.1 - November 13, 2021
Add
has_key?/2andget/3map functions that lookup keys in maps both in a binary or an atom form.Add the link to open Readme.md in LiveBook
dimitarvp
I needed exactly this two weeks ago, thanks for making it.
dredison
This is 100% exactly the library I needed. Thank you! (and again the extra time you spent on the documentation).
IvanR
0.2.0 - August 16, 2022
decodeandencodeverbs to function names[Module]hint in the map returned fromfrom_map_hintto decode the list of structs%{one_key: :other_key}mapping configuration for thePreDecoderprotocol in@deriveattribute.Now, for most common decoding cases, it’s possible to configure decoding fully in a declarative way by providing param maps when deriving protocols like that:
so the decoding works like that:
IvanR
0.2.1 - August 20, 2022
decode_from_map(!)/2/3to return the error for not a map valueIvanR
0.3.0 - November 14, 2022
Nestru.PreDecoder.gather_fields_map/3togather_fields_from_map/3Nestru.Encoder.encode_to_map/1toNestru.Encoder.gather_fields_from_struct/2encode_to_map(!)/2work only with structs and addencode_to_list_of_maps(!)/2for listsencode_to_*functionsContexts can be helpful to specify
DecoderorEncoderstrategy for various senders or receivers of the map.That is the
Nestru.decode_from_map(map, Item, :mobile)call passes:mobilecontext todef from_map_hint(value, context, map)and theNestru.encode_to_map(struct, :mobile)call passes the:mobilecontext todef gather_fields_from_struct(%Item{} = struct, context)implementation for theItemstruct appropriately.So the application’s internal model can be translated into several inbound and outbound models depending on the context
IvanR
0.3.1 - November 22, 2022
:onlyand:exceptoptions for deriving ofNestru.Encoderprotocol (hommage forJason.Encoderapproach:translateoption for deriving ofNestru.PreDecoderprotocol:hintoption for deriving ofNestru.DecoderprotocolSee the documentation for examples.