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
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"} ] }
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: []}}
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"}
]
}}
MyApp.Balance.new(data)
{:ok,
%MyApp.Balance{
points_balance: 150.0,
rewards: [
%MyApp.Reward{name: "Free Coffee"},
%MyApp.Reward{name: "10% Discount"}
]
}}
No description
No description
2 Replies
ZachDaniel
ZachDaniel•3d ago
Sounds like a bug to me 🙂
absowoot
absowootOP•2d ago
I'll go ahead and create an issue on GH

Did you find this page helpful?