AE
Ash Elixir•3y ago
moxley

Does attribute `default: _` not set the struct's default value?

It appears that when defining an attribute's default: value, it doesn't set the default on the generated struct definition. I would expect it would, like Ecto.Schema does. Is there a reason it doesn't? Or is there a way of accomplishing this that I couldn't find in the documentation?
defmodule GF.Ash.WebComponent do
use Ash.Resource,
data_layer: AshPostgres.DataLayer,
extensions: [AshGraphql.Resource]

alias GF.Ash.EmbeddedWebComponent

attributes do
attribute :attrs, {:array, EmbeddedWebComponent}, default: []
attribute :usage, :atom,
constraints: [one_of: [:attribute, :replacement]],
default: :attribute
end
end
defmodule GF.Ash.WebComponent do
use Ash.Resource,
data_layer: AshPostgres.DataLayer,
extensions: [AshGraphql.Resource]

alias GF.Ash.EmbeddedWebComponent

attributes do
attribute :attrs, {:array, EmbeddedWebComponent}, default: []
attribute :usage, :atom,
constraints: [one_of: [:attribute, :replacement]],
default: :attribute
end
end
component = %WebComponent{}
dbg(component.attrs) # nil
dbg(component.usage) # nil
component = %WebComponent{}
dbg(component.attrs) # nil
dbg(component.usage) # nil
3 Replies
ZachDaniel
ZachDaniel•3y ago
We don't do it, would just need to update this: https://github.com/ash-project/ash/blob/main/lib/ash/resource/schema.ex
GitHub
ash/schema.ex at main · ash-project/ash
A declarative and extensible framework for building Elixir applications. - ash/schema.ex at main · ash-project/ash
ZachDaniel
ZachDaniel•3y ago
we'd need to only set default if its not a function if you want to PR that I'd accept it 😄
moxley
moxleyOP•3y ago
Alright, cool. So it's unintentional, and open to PR. Sounds good!

Did you find this page helpful?