How to extend and overwrite a macro from another module in compile time

Hello, I am developing a lib that provides structs with validation and and Sanitizing; you can find it here Mishka developer tools. as you see I can define field and another module struct etc. but I can not be able to extend from a module and change same field or add new one

For example:

defmodule Example do
  use GuardedStruct

  guardedstruct authorized_fields: true do
    field(:username, String.t(),
      domain: "!auth.action=String[admin, user]::?auth.social=Atom[banned]",
      derive: "validate(string)"
    )

    sub_field(:auth, struct(), authorized_fields: true) do
      field(:action, String.t(), derive: "validate(not_empty)")
      field(:social, atom(), derive: "validate(atom)")
      field(:type, map(), derive: "validate(map)")
    end
    
    conditional_field(:activities, any(), structs: true) do
      field(:activities, struct(), struct: ExtrenalConditional, validator: {VAL, :is_map_data}, hint: "activities1")
      field(:activities, struct(), structs: ExtrenalConditional, validator: {VAL, :is_list_data}, hint: "activities2")
      field(:activities, String.t(), hint: "activities3", validator: {VAL, :is_string_data})
    end
  end
end

In another module I want to extend from Example and override :username field and keep the other macro same like original Example

It can be a huge improvement for the library let users prevent duplicate coding

I have no idea what should I do for this

Thank you in advance

I think that looking at Ash framework and Spark might be in the best of your interest.

My 2 cents would be not to dwelve too much into dsl languages if they don’t make sense in your business case, as it rarely it is worth to generalise such a thing, this is one of the reasons why we use programming languages to define buisness requirements in the first place.

Everything is not absolutely bad and there are good things. Yes, I agree that you should be very careful when using macros.
I have been facing many challenges in DSLs for some time and I solved them one by one. I also advanced my goal and migrated from my previous level to a new and better level in terms of knowledge and experience.
After working with Elixir for several years, this macro made me come back and strengthen my foundation
The goal of the project is to simplify the review for non-technical people, as well as to make available the general conditions and nested dependencies
I would be very happy to read the GuardedStruct link in your spare time

I have been following your journey all along.

You have done great progress, however the things you are trying to abstract are either too specific or implemented by others. The first rule of macros is not to write macros, I am guilty of doing it myself, and I realise my mistakes after coming months later and not having the ability to understand those DSLs.

Having said all of this, if you are finding success in your local business, it might be a path worth chasing.

1 Like