ivanrublev / nestru Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
Having a struct defined like:
defmodule Kratos.Models.UIContainer do
@moduledoc false
@derive {Nestru.Decoder, hint: %{nodes: [Kratos.Models.UINode], messages: [Kratos.Models.UIText]}}
defstruct [
:action,
:method,
:nodes,
:messages
]
@type t :: %__MODULE__{
:action => String.t(),
:method => String.t(),
:nodes => [Kratos.Models.UINode.t()],
:messages => [Kratos.Models.UIText.t()] | nil
}
end
the messages
attribute can either be a list or nil
.
Currently, when messages
comes back as nil
from the API, I am expectedly getting a
{:error,
%{
get_in_keys: [#Function<8.65378251/3 in Access.key!/1>,
#Function<8.65378251/3 in Access.key!/1>],
message: %{message: "The first argument should be a list. Got nil instead."},
path: ["ui", :messages]
}}
response.
Is there a way to handle optional hints? Something like
@derive {Nestru.Decoder,
hint: %{
nodes: [Kratos.Models.UINode],
},
hint_optional: %{
messages: [Kratos.Models.UIText]
}}
maybe.
Hi, thanks for your work on this library. I have been using Nestru for a while to decode maps from JSON data into Elixir structs. In the last version of Nestru (0.3.2) the library is returning an error when decoding a map that has an empty list.
Supposing we have defined the following structs:
defmodule Order do
@derive {Nestru.Decoder, hint: %{items: [LineItem]}}
defstruct [:id, :items]
end
defmodule LineItem do
@derive Nestru.Decoder
defstruct [:amount]
end
If we provide a list with LineItem values for the items
field, everything works as expected:
map = %{
"id" => "A548",
"items" => [%{"amount" => 150}, %{"amount" => 350}]
}
{:ok, model} = Nestru.decode_from_map(map, Order)
{:ok, %Order{id: "A548", items: [%LineItem{amount: 150}, %LineItem{amount: 350}]}}
However, if we provide an empty list for the items
field, an error is raised:
map = %{
"id" => "A548",
"items" => [],
}
{:ok, model} = Nestru.decode_from_map(map, Order)
{:error, %{ get_in_keys: [#Function<8.75249601/3 in Access.key!/1>], message: %{message: "The first argument should be a list. Got [] instead."}, path: ["items"]}}
The map should be decoded correctly, creating an Order
struct with an empty list in the items
field:
{:ok, %Order{id: "A548", items: []}}
This new error when providing empty lists appeared in version 0.3.2 of Nestru. In version 0.3.1, the expected behavior shown above occurs instead.
Thanks for your library.
For polymorphic relationships I am comparing it to polymorphic_embed with Ecto.
Nestru seems lacking because it needs explicit type hint. Consider JSON case: there are no type hints usually, but nested types could have different sets of fields, so it should be possible to match on the fields, which is what polymorphic_embed is capable of, but Nestru isn't.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.