AshMoney with Ash.Generator

Hi, I have a resource that has an AshMoney attribute. When trying to create to with Ash.Generator I'm getting the following error
** (RuntimeError) generator/1 unimplemented for AshMoney.Types.Money
stacktrace:
(ash_money 0.2.0) /home/shawn/yc/ycm/deps/ash/lib/ash/type/type.ex:2038: AshMoney.Types.Money.generator/1
(ash 3.5.6) lib/ash/generator/generator.ex:1017: Ash.Generator.attribute_generator/2
(ash 3.5.6) lib/ash/generator/generator.ex:999: anonymous fn/5 in Ash.Generator.generate_attributes/5
(elixir 1.18.1) lib/enum.ex:2546: Enum."-reduce/3-lists^foldl/2-0-"/3
(ash 3.5.6) lib/ash/generator/generator.ex:951: Ash.Generator.generate_attributes/5
(ash 3.5.6) lib/ash/generator/generator.ex:264: Ash.Generator.changeset_generator/3
test/ycm/outbound/target/processor_test.exs:19: Ycm.Outbound.Target.ProcessorTest.__ex_unit_setup_1/1
test/ycm/outbound/target/processor_test.exs:1: Ycm.Outbound.Target.ProcessorTest.__ex_unit__/2
** (RuntimeError) generator/1 unimplemented for AshMoney.Types.Money
stacktrace:
(ash_money 0.2.0) /home/shawn/yc/ycm/deps/ash/lib/ash/type/type.ex:2038: AshMoney.Types.Money.generator/1
(ash 3.5.6) lib/ash/generator/generator.ex:1017: Ash.Generator.attribute_generator/2
(ash 3.5.6) lib/ash/generator/generator.ex:999: anonymous fn/5 in Ash.Generator.generate_attributes/5
(elixir 1.18.1) lib/enum.ex:2546: Enum."-reduce/3-lists^foldl/2-0-"/3
(ash 3.5.6) lib/ash/generator/generator.ex:951: Ash.Generator.generate_attributes/5
(ash 3.5.6) lib/ash/generator/generator.ex:264: Ash.Generator.changeset_generator/3
test/ycm/outbound/target/processor_test.exs:19: Ycm.Outbound.Target.ProcessorTest.__ex_unit_setup_1/1
test/ycm/outbound/target/processor_test.exs:1: Ycm.Outbound.Target.ProcessorTest.__ex_unit__/2
I'm tried wrapping it in a StreamData function with no change. I can't see anything in the docs about custom types with Ash.Generator. Do I need to patch AshMoney to provide the function or is there a general way to handle custom types?
8 Replies
ZachDaniel
ZachDaniel4mo ago
You should be able to provide a custom generator or value for that particular field How are you trying to use it?
sanswork
sansworkOP4mo ago
def target_price(opts \\ []) do
changeset_generator(
Target.Price,
:create,
defaults: [
commission: Money.new(100, :USD)
billing_amount: Money.new(200, :AUD),
target_id: opts[:target_id]
],
overrides: opts,
authorize?: false
)
end
def target_price(opts \\ []) do
changeset_generator(
Target.Price,
:create,
defaults: [
commission: Money.new(100, :USD)
billing_amount: Money.new(200, :AUD),
target_id: opts[:target_id]
],
overrides: opts,
authorize?: false
)
end
ZachDaniel
ZachDaniel4mo ago
Hmmm...are there other money inputs for that action?
sanswork
sansworkOP4mo ago
there is a target resource which this is a child of which has the same two attributes.
ZachDaniel
ZachDaniel4mo ago
Are there calls in the action to manage relationships? Or anything like that?
sanswork
sansworkOP4mo ago
yes, the target is setup with direct_control on the prices
argument :prices, {:array, :map}
change manage_relationship(:prices, type: :direct_control)
argument :prices, {:array, :map}
change manage_relationship(:prices, type: :direct_control)
ZachDaniel
ZachDaniel4mo ago
Hmm...try setting prices to an empty list in defaults?
sanswork
sansworkOP4mo ago
That fixed it, thank you so much

Did you find this page helpful?