Giter Site home page Giter Site logo

nestru's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

nestru's Issues

Optional type hints

Environment

  • Elixir version (elixir -v): Elixir 1.14.4 (compiled with Erlang/OTP 25)
  • Nestru version (mix deps | grep nestru | head -1): nestru 0.3.3 (Hex package) (mix)

Actual behavior

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.

Expected behavior

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.

Error when decoding empty lists from maps

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.

Environment

  • Elixir version (elixir -v): 1.14.3
  • Nestru version (mix deps | grep nestru | head -1): 0.3.2

Actual behavior

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"]}}

Expected behavior

The map should be decoded correctly, creating an Order struct with an empty list in the items field:

{:ok, %Order{id: "A548", items: []}}

Additional notes

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.

Polymorphic field support could be better

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.