Ash FrameworkAF
Ash Framework4mo ago
3 replies
absowoot

TypedStruct with array and default

I have a typed_struct with an {:array, OtherStruct} field. Passing data to new() works as expected but after adding default: [] to the field, the field only returns []. I'm not sure if this is a bug or intended.


Structs:
defmodule MyApp.Balance do
  use Ash.TypedStruct

  typed_struct do
    field :points_balance, :float
    field :rewards, {:array, MyApp.Reward}, default: []
  end
end

defmodule MyApp.Reward do
  use Ash.TypedStruct

  typed_struct do
    field :name, :string
  end
end


Data:
data = %{ "points_balance" => 150.0, "rewards" => [ %{"name" => "Free Coffee"}, %{"name" => "10% Discount"} ] }


With default: []
MyApp.Balance.new(data)
{:ok, %MyApp.Balance{points_balance: 150.0, rewards: []}}


Without default: []
MyApp.Balance.new(data)
{:ok,
 %MyApp.Balance{
   points_balance: 150.0,
   rewards: [
     %MyApp.Reward{name: "Free Coffee"},
     %MyApp.Reward{name: "10% Discount"}
   ]
 }}
image.png
image.png
Was this page helpful?