Ash FrameworkAF
Ash Framework3y ago
23 replies
Vonagam

Is there a way to define dsl for new type of attribute?

I've implemented ULID type which is one of alternatives to UUID. I can use it without a problem right now by providing type and default to uuid_primary_key. But wanted to venture into making a small spark extension that adds ulid_primary_key.

With my naive approach I wrote something like that:
defmodule Ash.Ulid.Extension do
  @ulid_primary_key_schema Ash.Resource.Attribute.uuid_primary_key_schema()
    |> Spark.OptionsHelpers.set_default!(:type, Ash.Ulid)
    |> Spark.OptionsHelpers.set_default!(:default, &Ash.Ulid.generate/0)

  @ulid_primary_key %Spark.Dsl.Entity{
    name: :ulid_primary_key,
    examples: ["ulid_primary_key :id"],
    args: [:name],
    target: Ash.Resource.Attribute,
    schema: @ulid_primary_key_schema,
    auto_set_fields: [allow_nil?: false],
    transform: {Ash.Resource.Attribute, :transform, []}
  }

  @attributes_patch %Spark.Dsl.Patch.AddEntity{section_path: [:attributes], entity: @ulid_primary_key}

  use Spark.Dsl.Extension, dsl_patches: [@attributes_patch]
end


And then wanted to apply it to a resource with extensions: [AshAuthentication]. But got an error about attributes section not being patchable. I see that unless a section explicitly allows patching it is not allowed. Is it because of some security/performance considerations? So currently it is impossible to add new attribute shortcuts, right?
Was this page helpful?