How do I use a schema macro in an extension?
I want to make an Ash extension that adds a
:uuid_v7_primary_key
attribute to a resource.
Adding an attribute via a macro:
This is an antipattern but it’s a small example.
The macro definition (Geo.Resources.Attributes.Id
): https://github.com/dev-guy/geo/blob/e6aaf95d63961b4e40df6f51ac382393840c010a/lib/geo/resources/attributes/id.ex
Using it: https://github.com/dev-guy/geo/blob/70baa42f3ea8a5b092345e478f61a552fb79d9fb/lib/geo/country/country.ex
Adding an attribute via an extension:
Macros are great but they are not Ashy.
As with the macro, can I reuse uuid_v7_primary_key
in an extension?
Claude tried and got syntax errors (below)
The extension definition: https://github.com/dev-guy/geo/blob/e6aaf95d63961b4e40df6f51ac382393840c010a/lib/geo/resources/extensions/id.ex
Using it: https://github.com/dev-guy/geo/blob/e6aaf95d63961b4e40df6f51ac382393840c010a/lib/geo/geography/country.ex
Do I have to essentially reimplement :uuid_v7_primary_key
?
Error:
3 Replies
I think I see where Claude messed up. It used
uuid_v7_primary_key
as an atom instead of using the macro.
This extension works: https://github.com/dev-guy/geo/blob/702c58a366687f383381dc9b69fd58c35432f0ac/lib/geo/resources/attributes/id.ex
but I was hoping to leverage the uuid_v7_primary_key
schema.
Excerpt:
Solution
YOu can do
Ash.Resource.Builder.add_new_attribute(dsl_state, ....)
or in your case if you just want to build it then
Ash.Resource.Builder.build_attribute/3
nothing does the uuidv7 schema but you can copy build_attribute
example to make your own for build_uuidv7_primary_key